Skip to content

Commit 489bc4a

Browse files
committed
When mentioning lifetimes, put either the trait ref or the self type closer to the lifetimes
When mentioning lifetimes, only invert wording between the expected trait and the self type when the self type has the vid. This way, the lifetimes always stay close to the self type or trait ref that actually contains them.
1 parent 1730ad4 commit 489bc4a

8 files changed

+81
-44
lines changed

src/librustc/infer/error_reporting/nice_region_error/placeholder_error.rs

Lines changed: 63 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,16 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
251251
}
252252
});
253253

254-
let self_ty_has_vid = self
254+
let actual_self_ty_has_vid = self
255255
.tcx()
256256
.any_free_region_meets(&actual_trait_ref.self_ty(), |r| Some(r) == vid);
257257

258+
let expected_self_ty_has_vid = self
259+
.tcx()
260+
.any_free_region_meets(&expected_trait_ref.self_ty(), |r| Some(r) == vid);
261+
262+
let self_ty_has_vid = actual_self_ty_has_vid || expected_self_ty_has_vid;
263+
258264
debug!(
259265
"try_report_placeholders_trait: actual_has_vid={:?}",
260266
actual_has_vid
@@ -266,8 +272,12 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
266272
debug!("try_report_placeholders_trait: has_sub={:?}", has_sub);
267273
debug!("try_report_placeholders_trait: has_sup={:?}", has_sup);
268274
debug!(
269-
"try_report_placeholders_trait: self_ty_has_vid={:?}",
270-
self_ty_has_vid
275+
"try_report_placeholders_trait: actual_self_ty_has_vid={:?}",
276+
actual_self_ty_has_vid
277+
);
278+
debug!(
279+
"try_report_placeholders_trait: expected_self_ty_has_vid={:?}",
280+
expected_self_ty_has_vid
271281
);
272282

273283
// The weird thing here with the `maybe_highlighting_region` calls and the
@@ -289,23 +299,43 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
289299
RegionHighlightMode::maybe_highlighting_region(sup_placeholder, has_sup, || {
290300
match (has_sub, has_sup) {
291301
(Some(n1), Some(n2)) => {
292-
err.note(&format!(
293-
"`{}` would have to be implemented for the type `{}`, \
294-
for any two lifetimes `'{}` and `'{}`",
295-
expected_trait_ref,
296-
expected_trait_ref.self_ty(),
297-
std::cmp::min(n1, n2),
298-
std::cmp::max(n1, n2),
299-
));
302+
if self_ty_has_vid {
303+
err.note(&format!(
304+
"`{}` would have to be implemented for the type `{}`, \
305+
for any two lifetimes `'{}` and `'{}`",
306+
expected_trait_ref,
307+
expected_trait_ref.self_ty(),
308+
std::cmp::min(n1, n2),
309+
std::cmp::max(n1, n2),
310+
));
311+
} else {
312+
err.note(&format!(
313+
"`{}` must implement `{}`, \
314+
for any two lifetimes `'{}` and `'{}`",
315+
expected_trait_ref.self_ty(),
316+
expected_trait_ref,
317+
std::cmp::min(n1, n2),
318+
std::cmp::max(n1, n2),
319+
));
320+
}
300321
}
301322
(Some(n), _) | (_, Some(n)) => {
302-
err.note(&format!(
303-
"`{}` would have to be implemented for the type `{}`, \
304-
for any lifetime `'{}`",
305-
expected_trait_ref,
306-
expected_trait_ref.self_ty(),
307-
n,
308-
));
323+
if self_ty_has_vid {
324+
err.note(&format!(
325+
"`{}` would have to be implemented for the type `{}`, \
326+
for any lifetime `'{}`",
327+
expected_trait_ref,
328+
expected_trait_ref.self_ty(),
329+
n,
330+
));
331+
} else {
332+
err.note(&format!(
333+
"`{}` must implement `{}`, for any lifetime `'{}`",
334+
expected_trait_ref.self_ty(),
335+
expected_trait_ref,
336+
n,
337+
));
338+
}
309339
}
310340
(None, None) => RegionHighlightMode::maybe_highlighting_region(
311341
vid,
@@ -320,11 +350,19 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
320350
n,
321351
));
322352
} else {
323-
err.note(&format!(
324-
"`{}` would have to be implemented for the type `{}`",
325-
expected_trait_ref,
326-
expected_trait_ref.self_ty(),
327-
));
353+
if self_ty_has_vid {
354+
err.note(&format!(
355+
"`{}` would have to be implemented for the type `{}`",
356+
expected_trait_ref,
357+
expected_trait_ref.self_ty(),
358+
));
359+
} else {
360+
err.note(&format!(
361+
"`{}` must implement `{}`",
362+
expected_trait_ref.self_ty(),
363+
expected_trait_ref,
364+
));
365+
}
328366
}
329367
},
330368
),
@@ -347,10 +385,9 @@ impl NiceRegionError<'me, 'gcx, 'tcx> {
347385
));
348386
} else {
349387
err.note(&format!(
350-
"but `{}` is actually implemented for the type `{}`, \
351-
for some lifetime `'{}`",
352-
actual_trait_ref,
388+
"but `{}` actually implements `{}`, for some lifetime `'{}`",
353389
actual_trait_ref.self_ty(),
390+
actual_trait_ref,
354391
n
355392
));
356393
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ LL | tuple_one::<Tuple>();
4141
| ^^^^^^^^^^^^^^^^^^
4242
|
4343
= note: Due to a where-clause on `tuple_one`,
44-
= note: `TheTrait<(&'0 isize, &'1 isize)>` would have to be implemented for the type `Tuple`, for any two lifetimes `'0` and `'1`
45-
= note: but `TheTrait<(&'2 isize, &'2 isize)>` is actually implemented for the type `Tuple`, for some lifetime `'2`
44+
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`
45+
= note: but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some lifetime `'2`
4646

4747
error: implementation of `TheTrait` is not general enough
4848
--> $DIR/associated-types-eq-hr.rs:96:5
@@ -51,8 +51,8 @@ LL | tuple_two::<Tuple>();
5151
| ^^^^^^^^^^^^^^^^^^
5252
|
5353
= note: Due to a where-clause on `tuple_two`,
54-
= note: `TheTrait<(&'0 isize, &'1 isize)>` would have to be implemented for the type `Tuple`, for any two lifetimes `'0` and `'1`
55-
= note: but `TheTrait<(&'2 isize, &'2 isize)>` is actually implemented for the type `Tuple`, for some lifetime `'2`
54+
= note: `Tuple` must implement `TheTrait<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`
55+
= note: but `Tuple` actually implements `TheTrait<(&'2 isize, &'2 isize)>`, for some lifetime `'2`
5656

5757
error: implementation of `TheTrait` is not general enough
5858
--> $DIR/associated-types-eq-hr.rs:105:5
@@ -61,8 +61,8 @@ LL | tuple_four::<Tuple>();
6161
| ^^^^^^^^^^^^^^^^^^^
6262
|
6363
= note: Due to a where-clause on `tuple_four`,
64-
= note: `TheTrait<(&'0 isize, &'1 isize)>` would have to be implemented for the type `Tuple`, for any two lifetimes `'0` and `'1`
65-
= note: but `TheTrait<(&'2 isize, &'2 isize)>` is actually implemented for the type `Tuple`, for some lifetime `'2`
64+
= 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 lifetime `'2`
6666

6767
error: aborting due to 5 previous errors
6868

src/test/ui/hrtb/hrtb-cache-issue-54302.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: implementation of `Deserialize` is not general enough
44
LL | assert_deserialize_owned::<&'static str>(); //~ ERROR
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `Deserialize<'0>` would have to be implemented for the type `&'static str`, for any lifetime `'0`
8-
= note: but `Deserialize<'1>` is actually implemented for the type `&str`, for some lifetime `'1`
7+
= note: `&'static str` must implement `Deserialize<'0>`, for any lifetime `'0`
8+
= note: but `&str` actually implements `Deserialize<'1>`, for some lifetime `'1`
99

1010
error: aborting due to previous error
1111

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LL | fn b() { want_foo2::<SomeStruct>(); } //~ ERROR
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: Due to a where-clause on `want_foo2`,
8-
= note: `Foo<(&'0 isize, &'1 isize)>` would have to be implemented for the type `SomeStruct`, for any two lifetimes `'0` and `'1`
9-
= note: but `Foo<(&'2 isize, &'2 isize)>` is actually implemented for the type `SomeStruct`, for some lifetime `'2`
8+
= note: `SomeStruct` must implement `Foo<(&'0 isize, &'1 isize)>`, for any two lifetimes `'0` and `'1`
9+
= note: but `SomeStruct` actually implements `Foo<(&'2 isize, &'2 isize)>`, for some lifetime `'2`
1010

1111
error: aborting due to previous error
1212

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LL | foo::<()>(); //~ ERROR not general enough
55
| ^^^^^^^^^
66
|
77
= note: Due to a where-clause on `foo`,
8-
= note: `Trait<for<'b> fn(std::cell::Cell<&'b u32>)>` would have to be implemented for the type `()`
9-
= note: but `Trait<fn(std::cell::Cell<&'0 u32>)>` is actually implemented for the type `()`, for some lifetime `'0`
8+
= note: `()` must implement `Trait<for<'b> fn(std::cell::Cell<&'b u32>)>`
9+
= note: but `()` actually implements `Trait<fn(std::cell::Cell<&'0 u32>)>`, for some lifetime `'0`
1010

1111
error: aborting due to previous error
1212

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LL | want_hrtb::<StaticInt>() //~ ERROR
55
| ^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: Due to a where-clause on `want_hrtb`,
8-
= note: `Foo<&'0 isize>` would have to be implemented for the type `StaticInt`, for any lifetime `'0`
9-
= note: but `Foo<&'1 isize>` is actually implemented for the type `StaticInt`, for some lifetime `'1`
8+
= note: `StaticInt` must implement `Foo<&'0 isize>`, for any lifetime `'0`
9+
= note: but `StaticInt` actually implements `Foo<&'1 isize>`, for some lifetime `'1`
1010

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

src/test/ui/hrtb/hrtb-perfect-forwarding.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LL | foo_hrtb_bar_not(&mut t); //~ ERROR not general enough
55
| ^^^^^^^^^^^^^^^^
66
|
77
= note: Due to a where-clause on `foo_hrtb_bar_not`,
8-
= note: `Foo<&'0 isize>` would have to be implemented for the type `&mut T`, for any lifetime `'0`
9-
= note: but `Foo<&'1 isize>` is actually implemented for the type `&mut T`, for some lifetime `'1`
8+
= note: `&mut T` must implement `Foo<&'0 isize>`, for any lifetime `'0`
9+
= note: but `&mut T` actually implements `Foo<&'1 isize>`, for some lifetime `'1`
1010

1111
error: aborting due to previous error
1212

src/test/ui/issues/issue-54302.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ error: implementation of `Deserialize` is not general enough
44
LL | assert_deserialize_owned::<&'static str>();
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
= note: `Deserialize<'0>` would have to be implemented for the type `&'static str`, for any lifetime `'0`
8-
= note: but `Deserialize<'1>` is actually implemented for the type `&str`, for some lifetime `'1`
7+
= note: `&'static str` must implement `Deserialize<'0>`, for any lifetime `'0`
8+
= note: but `&str` actually implements `Deserialize<'1>`, for some lifetime `'1`
99

1010
error: aborting due to previous error
1111

0 commit comments

Comments
 (0)