Skip to content

Commit 731ea85

Browse files
committed
review comment: tweak wording and account for span overlap
1 parent 65f492b commit 731ea85

12 files changed

+38
-25
lines changed

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,8 +1806,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
18061806
|err: &mut DiagnosticBuilder<'tcx>,
18071807
type_param_span: Option<(Span, bool, bool)>,
18081808
bound_kind: GenericKind<'tcx>| {
1809-
let msg = "consider introducing an explicit lifetime bound to unify the type \
1810-
parameter and the output";
1809+
let msg = "consider introducing an explicit lifetime bound";
18111810
if let Some((sp, has_lifetimes, is_impl_trait)) = type_param_span {
18121811
let suggestion = if is_impl_trait {
18131812
(sp.shrink_to_hi(), format!(" + {}", new_lt))

src/librustc_infer/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,23 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
2727
let return_sp = sub_origin.span();
2828
let mut err =
2929
self.tcx().sess.struct_span_err(sp, "cannot infer an appropriate lifetime");
30-
err.span_label(return_sp, "this evaluates to the `'static` lifetime...");
31-
err.span_label(sup_origin.span(), "...but this borrow...");
30+
if sp == sup_origin.span() && return_sp == sp {
31+
// Example: `ui/object-lifetime/object-lifetime-default-from-box-error.rs`
32+
err.span_label(
33+
sup_origin.span(),
34+
"this needs to be `'static` but the borrow...",
35+
);
36+
} else {
37+
err.span_label(return_sp, "this is `'static`...");
38+
// We try to make the output have fewer overlapping spans if possible.
39+
if sp == sup_origin.span() || !return_sp.overlaps(sup_origin.span()) {
40+
// When `sp == sup_origin` we already have overlapping spans in the
41+
// main diagnostic output, so we don't split this into its own note.
42+
err.span_label(sup_origin.span(), "...but this borrow...");
43+
} else {
44+
err.span_note(sup_origin.span(), "...but this borrow...");
45+
}
46+
}
3247

3348
let (lifetime, lt_sp_opt) = msg_span_from_free_region(self.tcx(), sup_r);
3449
if let Some(lifetime_sp) = lt_sp_opt {

src/test/ui/async-await/issues/issue-62097.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
44
LL | pub async fn run_dummy_fn(&self) {
55
| ^^^^^ ...but this borrow...
66
LL | foo(|| self.bar()).await;
7-
| --- this evaluates to the `'static` lifetime...
7+
| --- this is `'static`...
88
|
99
note: ...can't outlive the lifetime `'_` as defined on the method body at 12:31
1010
--> $DIR/issue-62097.rs:12:31

src/test/ui/impl-trait/must_outlive_least_region_or_bound.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
44
LL | fn elided(x: &i32) -> impl Copy { x }
55
| --------- ^ ...but this borrow...
66
| |
7-
| this evaluates to the `'static` lifetime...
7+
| this is `'static`...
88
|
99
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
1010
--> $DIR/must_outlive_least_region_or_bound.rs:3:1
@@ -22,7 +22,7 @@ error: cannot infer an appropriate lifetime
2222
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
2323
| --------- ^ ...but this borrow...
2424
| |
25-
| this evaluates to the `'static` lifetime...
25+
| this is `'static`...
2626
|
2727
note: ...can't outlive the lifetime `'a` as defined on the function body at 6:13
2828
--> $DIR/must_outlive_least_region_or_bound.rs:6:13
@@ -40,7 +40,7 @@ error: cannot infer an appropriate lifetime
4040
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
4141
| -------------------------------- ^ ...but this borrow...
4242
| |
43-
| this evaluates to the `'static` lifetime...
43+
| this is `'static`...
4444
|
4545
note: ...can't outlive the lifetime `'a` as defined on the function body at 12:15
4646
--> $DIR/must_outlive_least_region_or_bound.rs:12:15

src/test/ui/impl-trait/static-return-lifetime-infered.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/static-return-lifetime-infered.rs:7:16
33
|
44
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
5-
| ----------------------- this evaluates to the `'static` lifetime...
5+
| ----------------------- this is `'static`...
66
LL | self.x.iter().map(|a| a.0)
77
| ------ ^^^^
88
| |
@@ -24,7 +24,7 @@ error: cannot infer an appropriate lifetime
2424
--> $DIR/static-return-lifetime-infered.rs:11:16
2525
|
2626
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
27-
| ----------------------- this evaluates to the `'static` lifetime...
27+
| ----------------------- this is `'static`...
2828
LL | self.x.iter().map(|a| a.0)
2929
| ------ ^^^^
3030
| |

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | Box::new(value) as Box<dyn Any>
55
| ---------^^^^^-
66
| | |
77
| | ...but this borrow...
8-
| this evaluates to the `'static` lifetime...
8+
| this is `'static`...
99
|
1010
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
1111
--> $DIR/issue-16922.rs:3:1

src/test/ui/object-lifetime/object-lifetime-default-from-box-error.stderr

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/object-lifetime-default-from-box-error.rs:18:5
33
|
44
LL | ss.r
5-
| ^^^^
6-
| |
7-
| this evaluates to the `'static` lifetime...
8-
| ...but this borrow...
5+
| ^^^^ this needs to be `'static` but the borrow...
96
|
107
note: ...can't outlive the anonymous lifetime #2 defined on the function body at 14:1
118
--> $DIR/object-lifetime-default-from-box-error.rs:14:1

src/test/ui/regions/region-object-lifetime-in-coercion.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL | Box::new(v)
2121
| ---------^-
2222
| | |
2323
| | ...but this borrow...
24-
| this evaluates to the `'static` lifetime...
24+
| this is `'static`...
2525
|
2626
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 17:1
2727
--> $DIR/region-object-lifetime-in-coercion.rs:17:1

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:16
33
|
44
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
5-
| ^^^^ ---------- this evaluates to the `'static` lifetime...
5+
| ^^^^ ---------- this is `'static`...
66
| |
77
| ...but this borrow...
88
|

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: cannot infer an appropriate lifetime
44
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
55
| ---------- ^^^^ ...but this borrow...
66
| |
7-
| this evaluates to the `'static` lifetime...
7+
| this is `'static`...
88
|
99
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 6:5
1010
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:5

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: cannot infer an appropriate lifetime
1010
--> $DIR/missing-lifetimes-in-signature.rs:19:5
1111
|
1212
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
13-
| ------------- this evaluates to the `'static` lifetime...
13+
| ------------- this is `'static`...
1414
...
1515
LL | / move || {
1616
LL | | *dest = g.get();
@@ -55,7 +55,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:30:5:
5555
|
5656
LL | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
5757
| ^^^^^^^^^^^^^^^^^^
58-
help: consider introducing an explicit lifetime bound to unify the type parameter and the output
58+
help: consider introducing an explicit lifetime bound
5959
|
6060
LL | fn bar<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
6161
| ^^^^^ ^^^^
@@ -82,7 +82,7 @@ note: ...so that the type `[closure@$DIR/missing-lifetimes-in-signature.rs:52:5:
8282
|
8383
LL | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
8484
| ^^^^^^^^^^^^^^^^^^
85-
help: consider introducing an explicit lifetime bound to unify the type parameter and the output
85+
help: consider introducing an explicit lifetime bound
8686
|
8787
LL | fn qux<'b, 'a, G: 'b + 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'b
8888
| ^^^ ^^^^^^^ ^^^^

src/test/ui/underscore-lifetime/dyn-trait-underscore.stderr

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/dyn-trait-underscore.rs:8:20
33
|
44
LL | Box::new(items.iter())
5-
| ---------------^^^^---
6-
| | |
7-
| | ...but this borrow...
8-
| this evaluates to the `'static` lifetime...
5+
| ---------------^^^^--- this is `'static`...
96
|
7+
note: ...but this borrow...
8+
--> $DIR/dyn-trait-underscore.rs:8:14
9+
|
10+
LL | Box::new(items.iter())
11+
| ^^^^^
1012
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 6:1
1113
--> $DIR/dyn-trait-underscore.rs:6:1
1214
|

0 commit comments

Comments
 (0)