Skip to content

Commit 83f6f22

Browse files
committed
Tweak wording and spans of 'static dyn Trait/impl Trait requirements
1 parent 6dcd744 commit 83f6f22

11 files changed

+91
-156
lines changed

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

+26-21
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,44 @@ 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-
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...");
30+
let param_info = self.find_param_with_region(sup_r, sub_r)?;
31+
err.span_label(param_info.param_ty_span, "data with this lifetime...");
32+
33+
// We try to make the output have fewer overlapping spans if possible.
34+
if (sp == sup_origin.span() || !return_sp.overlaps(sup_origin.span()))
35+
&& sup_origin.span() != return_sp
36+
{
37+
// FIXME: account for `async fn` like in `async-await/issues/issue-62097.rs`
38+
39+
// Customize the spans and labels depending on their relative order so
40+
// that split sentences flow correctly.
41+
if sup_origin.span().shrink_to_hi() <= return_sp.shrink_to_lo() {
42+
err.span_label(sup_origin.span(), "...is captured here...");
43+
err.span_label(return_sp, "...and required to be `'static` by this");
4344
} else {
44-
err.span_note(sup_origin.span(), "...but this borrow...");
45+
err.span_label(return_sp, "...is required to be `'static` by this...");
46+
err.span_label(sup_origin.span(), "...and is captured here");
4547
}
48+
} else {
49+
err.span_label(
50+
return_sp,
51+
"...is captured and required to be `'static` here",
52+
);
4653
}
4754

48-
let (lifetime, lt_sp_opt) = msg_span_from_free_region(self.tcx(), sup_r);
49-
if let Some(lifetime_sp) = lt_sp_opt {
50-
err.span_note(lifetime_sp, &format!("...can't outlive {}", lifetime));
51-
}
55+
let (lifetime, _) = msg_span_from_free_region(self.tcx(), sup_r);
5256

5357
let lifetime_name =
5458
if sup_r.has_name() { sup_r.to_string() } else { "'_".to_owned() };
5559
// only apply this suggestion onto functions with
5660
// explicit non-desugar'able return.
5761
if fn_return_span.desugaring_kind().is_none() {
5862
let msg = format!(
59-
"you can add a bound to the returned `{} Trait` to make it last less \
60-
than `'static` and match {}",
63+
"to permit non-static references in {} `{} Trait` value, you can add \
64+
an explicit bound for {}",
65+
if is_dyn { "a" } else { "an" },
6166
if is_dyn { "dyn" } else { "impl" },
62-
lifetime
67+
lifetime,
6368
);
6469
// FIXME: account for the need of parens in `&(dyn Trait + '_)`
6570
err.span_suggestion_verbose(

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/issue-62097.rs:12:31
33
|
44
LL | pub async fn run_dummy_fn(&self) {
5-
| ^^^^^ ...but this borrow...
5+
| ^^^^^
6+
| |
7+
| data with this lifetime...
8+
| ...is captured here...
69
LL | foo(|| self.bar()).await;
7-
| --- this is `'static`...
8-
|
9-
note: ...can't outlive the lifetime `'_` as defined on the method body at 12:31
10-
--> $DIR/issue-62097.rs:12:31
11-
|
12-
LL | pub async fn run_dummy_fn(&self) {
13-
| ^
10+
| --- ...and required to be `'static` by this
1411

1512
error: aborting due to previous error
1613

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

+15-27
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/must_outlive_least_region_or_bound.rs:3:35
33
|
44
LL | fn elided(x: &i32) -> impl Copy { x }
5-
| --------- ^ ...but this borrow...
6-
| |
7-
| this is `'static`...
5+
| ---- --------- ^ ...and is captured here
6+
| | |
7+
| | ...is required to be `'static` by this...
8+
| data with this lifetime...
89
|
9-
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
10-
--> $DIR/must_outlive_least_region_or_bound.rs:3:1
11-
|
12-
LL | fn elided(x: &i32) -> impl Copy { x }
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 3:1
10+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 3:1
1511
|
1612
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
1713
| ^^^^
@@ -20,16 +16,12 @@ error: cannot infer an appropriate lifetime
2016
--> $DIR/must_outlive_least_region_or_bound.rs:6:44
2117
|
2218
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
23-
| --------- ^ ...but this borrow...
24-
| |
25-
| this is `'static`...
26-
|
27-
note: ...can't outlive the lifetime `'a` as defined on the function body at 6:13
28-
--> $DIR/must_outlive_least_region_or_bound.rs:6:13
19+
| ------- --------- ^ ...and is captured here
20+
| | |
21+
| | ...is required to be `'static` by this...
22+
| data with this lifetime...
2923
|
30-
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
31-
| ^^
32-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the lifetime `'a` as defined on the function body at 6:13
24+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the lifetime `'a` as defined on the function body at 6:13
3325
|
3426
LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
3527
| ^^^^
@@ -38,16 +30,12 @@ error: cannot infer an appropriate lifetime
3830
--> $DIR/must_outlive_least_region_or_bound.rs:12:69
3931
|
4032
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
41-
| -------------------------------- ^ ...but this borrow...
42-
| |
43-
| this is `'static`...
44-
|
45-
note: ...can't outlive the lifetime `'a` as defined on the function body at 12:15
46-
--> $DIR/must_outlive_least_region_or_bound.rs:12:15
33+
| ------- -------------------------------- ^ ...and is captured here
34+
| | |
35+
| | ...is required to be `'static` by this...
36+
| data with this lifetime...
4737
|
48-
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
49-
| ^^
50-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the lifetime `'a` as defined on the function body at 12:15
38+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the lifetime `'a` as defined on the function body at 12:15
5139
|
5240
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static + 'a { x }
5341
| ^^^^

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

+10-18
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,15 @@ 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 is `'static`...
5+
| ----- ----------------------- ...is required to be `'static` by this...
6+
| |
7+
| data with this lifetime...
68
LL | self.x.iter().map(|a| a.0)
79
| ------ ^^^^
810
| |
9-
| ...but this borrow...
11+
| ...and is captured here
1012
|
11-
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 6:5
12-
--> $DIR/static-return-lifetime-infered.rs:6:5
13-
|
14-
LL | / fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
15-
LL | | self.x.iter().map(|a| a.0)
16-
LL | | }
17-
| |_____^
18-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the method body at 6:5
13+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the method body at 6:5
1914
|
2015
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
2116
| ^^^^
@@ -24,18 +19,15 @@ error: cannot infer an appropriate lifetime
2419
--> $DIR/static-return-lifetime-infered.rs:11:16
2520
|
2621
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
27-
| ----------------------- this is `'static`...
22+
| -------- ----------------------- ...is required to be `'static` by this...
23+
| |
24+
| data with this lifetime...
2825
LL | self.x.iter().map(|a| a.0)
2926
| ------ ^^^^
3027
| |
31-
| ...but this borrow...
32-
|
33-
note: ...can't outlive the lifetime `'a` as defined on the method body at 10:20
34-
--> $DIR/static-return-lifetime-infered.rs:10:20
28+
| ...and is captured here
3529
|
36-
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
37-
| ^^
38-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the lifetime `'a` as defined on the method body at 10:20
30+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the lifetime `'a` as defined on the method body at 10:20
3931
|
4032
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
4133
| ^^^^

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
error: cannot infer an appropriate lifetime
22
--> $DIR/issue-16922.rs:4:14
33
|
4+
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
5+
| -- data with this lifetime...
46
LL | Box::new(value) as Box<dyn Any>
57
| ---------^^^^^-
68
| | |
7-
| | ...but this borrow...
8-
| this is `'static`...
9+
| | ...and is captured here
10+
| ...is required to be `'static` by this...
911
|
10-
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 3:1
11-
--> $DIR/issue-16922.rs:3:1
12-
|
13-
LL | / fn foo<T: Any>(value: &T) -> Box<dyn Any> {
14-
LL | | Box::new(value) as Box<dyn Any>
15-
LL | |
16-
LL | | }
17-
| |_^
18-
help: you can add a bound to the returned `dyn Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 3:1
12+
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 3:1
1913
|
2014
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {
2115
| ^^^^

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

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
error: cannot infer an appropriate lifetime
22
--> $DIR/object-lifetime-default-from-box-error.rs:18:5
33
|
4+
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {
5+
| --------------- data with this lifetime...
6+
...
47
LL | ss.r
5-
| ^^^^ this needs to be `'static` but the borrow...
6-
|
7-
note: ...can't outlive the anonymous lifetime #2 defined on the function body at 14:1
8-
--> $DIR/object-lifetime-default-from-box-error.rs:14:1
8+
| ^^^^ ...is captured and required to be `'static` here
99
|
10-
LL | / fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {
11-
LL | | // `Box<SomeTrait>` defaults to a `'static` bound, so this return
12-
LL | | // is illegal.
13-
LL | |
14-
LL | | ss.r
15-
LL | | }
16-
| |_^
17-
help: you can add a bound to the returned `dyn Trait` to make it last less than `'static` and match the anonymous lifetime #2 defined on the function body at 14:1
10+
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #2 defined on the function body at 14:1
1811
|
1912
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait + '_> {
2013
| ^^^^

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

+6-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,16 @@ LL | Box::new(v)
1717
error: cannot infer an appropriate lifetime
1818
--> $DIR/region-object-lifetime-in-coercion.rs:20:14
1919
|
20+
LL | fn c(v: &[u8]) -> Box<dyn Foo> {
21+
| ----- data with this lifetime...
22+
...
2023
LL | Box::new(v)
2124
| ---------^-
2225
| | |
23-
| | ...but this borrow...
24-
| this is `'static`...
26+
| | ...and is captured here
27+
| ...is required to be `'static` by this...
2528
|
26-
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 17:1
27-
--> $DIR/region-object-lifetime-in-coercion.rs:17:1
28-
|
29-
LL | / fn c(v: &[u8]) -> Box<dyn Foo> {
30-
LL | | // same as previous case due to RFC 599
31-
LL | |
32-
LL | | Box::new(v)
33-
LL | | }
34-
| |_^
35-
help: you can add a bound to the returned `dyn Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 17:1
29+
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 17:1
3630
|
3731
LL | fn c(v: &[u8]) -> Box<dyn Foo + '_> {
3832
| ^^^^

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

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ 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 is `'static`...
6-
| |
7-
| ...but this borrow...
8-
|
9-
note: ...can't outlive the lifetime `'_` as defined on the method body at 8:26
10-
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:26
11-
|
12-
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
13-
| ^
5+
| ^^^^ ---------- ---------- ...and required to be `'static` by this
6+
| | |
7+
| | data with this lifetime...
8+
| ...is captured here...
149

1510
error: aborting due to previous error
1611

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait.stderr

+5-9
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:44
33
|
44
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
5-
| ---------- ^^^^ ...but this borrow...
6-
| |
7-
| this is `'static`...
5+
| ---------- ---------- ^^^^ ...and is captured here
6+
| | |
7+
| | ...is required to be `'static` by this...
8+
| data with this lifetime...
89
|
9-
note: ...can't outlive the anonymous lifetime #1 defined on the method body at 6:5
10-
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait.rs:6:5
11-
|
12-
LL | fn f(self: Pin<&Self>) -> impl Clone { self }
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the method body at 6:5
10+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the method body at 6:5
1511
|
1612
LL | fn f(self: Pin<&Self>) -> impl Clone + '_ { self }
1713
| ^^^^

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

+5-14
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,16 @@ 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 is `'static`...
13+
| ------ ------------- ...is required to be `'static` by this...
14+
| |
15+
| data with this lifetime...
1416
...
1517
LL | / move || {
1618
LL | | *dest = g.get();
1719
LL | | }
18-
| |_____^ ...but this borrow...
20+
| |_____^ ...and is captured here
1921
|
20-
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 15:1
21-
--> $DIR/missing-lifetimes-in-signature.rs:15:1
22-
|
23-
LL | / fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce()
24-
LL | | where
25-
LL | | G: Get<T>
26-
LL | | {
27-
... |
28-
LL | | }
29-
LL | | }
30-
| |_^
31-
help: you can add a bound to the returned `impl Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 15:1
22+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 15:1
3223
|
3324
LL | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
3425
| ^^^^

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

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
error: cannot infer an appropriate lifetime
22
--> $DIR/dyn-trait-underscore.rs:8:20
33
|
4+
LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
5+
| ---- data with this lifetime...
6+
LL | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
47
LL | Box::new(items.iter())
5-
| ---------------^^^^--- this is `'static`...
8+
| ---------------^^^^--- ...is captured and required to be `'static` here
69
|
7-
note: ...but this borrow...
8-
--> $DIR/dyn-trait-underscore.rs:8:14
9-
|
10-
LL | Box::new(items.iter())
11-
| ^^^^^
12-
note: ...can't outlive the anonymous lifetime #1 defined on the function body at 6:1
13-
--> $DIR/dyn-trait-underscore.rs:6:1
14-
|
15-
LL | / fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T>> {
16-
LL | | // ^^^^^^^^^^^^^^^^^^^^^ bound *here* defaults to `'static`
17-
LL | | Box::new(items.iter())
18-
LL | | }
19-
| |_^
20-
help: you can add a bound to the returned `dyn Trait` to make it last less than `'static` and match the anonymous lifetime #1 defined on the function body at 6:1
10+
help: to permit non-static references in a `dyn Trait` value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 6:1
2111
|
2212
LL | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item=&T> + '_> {
2313
| ^^^^

0 commit comments

Comments
 (0)