Skip to content

Commit daab6db

Browse files
committed
Avoid repeating self type in placeholder error
1 parent c2066cf commit daab6db

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ impl NiceRegionError<'me, 'tcx> {
370370
value: trait_ref,
371371
};
372372

373+
let same_self_type = actual_trait_ref.self_ty() == expected_trait_ref.self_ty();
374+
373375
let mut expected_trait_ref = highlight_trait_ref(expected_trait_ref);
374376
expected_trait_ref.highlight.maybe_highlighting_region(sub_placeholder, has_sub);
375377
expected_trait_ref.highlight.maybe_highlighting_region(sup_placeholder, has_sup);
@@ -385,7 +387,16 @@ impl NiceRegionError<'me, 'tcx> {
385387
}
386388
};
387389

388-
let mut note = if passive_voice {
390+
let mut note = if same_self_type {
391+
let mut self_ty = expected_trait_ref.map(|tr| tr.self_ty());
392+
self_ty.highlight.maybe_highlighting_region(vid, actual_has_vid);
393+
format!(
394+
"{}`{}` must implement `{}`",
395+
if leading_ellipsis { "..." } else { "" },
396+
self_ty,
397+
expected_trait_ref.map(|tr| tr.print_only_trait_path()),
398+
)
399+
} else if passive_voice {
389400
format!(
390401
"{}`{}` would have to be implemented for the type `{}`",
391402
if leading_ellipsis { "..." } else { "" },
@@ -431,7 +442,12 @@ impl NiceRegionError<'me, 'tcx> {
431442
None => true,
432443
};
433444

434-
let mut note = if passive_voice {
445+
let mut note = if same_self_type {
446+
format!(
447+
"...but it actually implements `{}`",
448+
actual_trait_ref.map(|tr| tr.print_only_trait_path()),
449+
)
450+
} else if passive_voice {
435451
format!(
436452
"...but `{}` is actually implemented for the type `{}`",
437453
actual_trait_ref.map(|tr| tr.print_only_trait_path()),

src/test/ui/associated-types/associated-types-eq-hr.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ LL | tuple_one::<Tuple>();
3535
| ^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
3636
|
3737
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
38-
= note: ...but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
38+
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
3939

4040
error: implementation of `TheTrait` is not general enough
4141
--> $DIR/associated-types-eq-hr.rs:96:5
@@ -44,7 +44,7 @@ LL | tuple_one::<Tuple>();
4444
| ^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
4545
|
4646
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
47-
= note: ...but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
47+
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
4848

4949
error: implementation of `TheTrait` is not general enough
5050
--> $DIR/associated-types-eq-hr.rs:102:5
@@ -53,7 +53,7 @@ LL | tuple_two::<Tuple>();
5353
| ^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
5454
|
5555
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
56-
= note: ...but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
56+
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
5757

5858
error: implementation of `TheTrait` is not general enough
5959
--> $DIR/associated-types-eq-hr.rs:102:5
@@ -62,7 +62,7 @@ LL | tuple_two::<Tuple>();
6262
| ^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
6363
|
6464
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
65-
= note: ...but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
65+
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
6666

6767
error: implementation of `TheTrait` is not general enough
6868
--> $DIR/associated-types-eq-hr.rs:112:5
@@ -71,7 +71,7 @@ LL | tuple_four::<Tuple>();
7171
| ^^^^^^^^^^^^^^^^^^^ implementation of `TheTrait` is not general enough
7272
|
7373
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
74-
= note: ...but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
74+
= note: ...but it actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
7575

7676
error: aborting due to 7 previous errors
7777

src/test/ui/hrtb/hrtb-conflate-regions.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn b() { want_foo2::<SomeStruct>(); }
55
| ^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
66
|
77
= note: `SomeStruct` must implement `Foo<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`...
8-
= note: ...but `SomeStruct` actually implements `Foo<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
8+
= note: ...but it actually implements `Foo<(&'2 isize, &'2 isize)>`, for some specific lifetime `'2`
99

1010
error: aborting due to previous error
1111

src/test/ui/hrtb/hrtb-exists-forall-trait-contravariant.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | foo::<()>();
55
| ^^^^^^^^^ implementation of `Trait` is not general enough
66
|
77
= note: `()` must implement `Trait<for<'b> fn(&'b u32)>`
8-
= note: ...but `()` actually implements `Trait<fn(&'0 u32)>`, for some specific lifetime `'0`
8+
= note: ...but it actually implements `Trait<fn(&'0 u32)>`, for some specific lifetime `'0`
99

1010
error: aborting due to previous error
1111

src/test/ui/hrtb/hrtb-exists-forall-trait-invariant.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | foo::<()>();
55
| ^^^^^^^^^ implementation of `Trait` is not general enough
66
|
77
= note: `()` must implement `Trait<for<'b> fn(Cell<&'b u32>)>`
8-
= note: ...but `()` actually implements `Trait<fn(Cell<&'0 u32>)>`, for some specific lifetime `'0`
8+
= note: ...but it actually implements `Trait<fn(Cell<&'0 u32>)>`, for some specific lifetime `'0`
99

1010
error: aborting due to previous error
1111

src/test/ui/hrtb/hrtb-just-for-static.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | want_hrtb::<StaticInt>()
55
| ^^^^^^^^^^^^^^^^^^^^^^ implementation of `Foo` is not general enough
66
|
77
= note: `StaticInt` must implement `Foo<&'0 isize>`, for any lifetime `'0`...
8-
= note: ...but `StaticInt` actually implements `Foo<&'1 isize>`, for some specific lifetime `'1`
8+
= note: ...but it actually implements `Foo<&'1 isize>`, for some specific lifetime `'1`
99

1010
error: implementation of `Foo` is not general enough
1111
--> $DIR/hrtb-just-for-static.rs:30:5

0 commit comments

Comments
 (0)