Skip to content

Commit 921f35f

Browse files
committed
Reduce verbosity of suggestion message and mention lifetime in label
1 parent 4e90f17 commit 921f35f

14 files changed

+95
-86
lines changed

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

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Error Reporting for static impl Traits.
22
3-
use crate::infer::error_reporting::msg_span_from_free_region;
43
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
54
use crate::infer::lexical_region_resolve::RegionResolutionError;
65
use rustc_errors::{Applicability, ErrorReported};
@@ -33,9 +32,17 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3332
let sp = var_origin.span();
3433
let return_sp = sub_origin.span();
3534
let param_info = self.find_param_with_region(sup_r, sub_r)?;
35+
let (lifetime_name, lifetime) = if sup_r.has_name() {
36+
(sup_r.to_string(), format!("lifetime `{}`", sup_r))
37+
} else {
38+
("'_".to_owned(), "the anonymous lifetime `'_`".to_string())
39+
};
3640
let mut err =
3741
self.tcx().sess.struct_span_err(sp, "cannot infer an appropriate lifetime");
38-
err.span_label(param_info.param_ty_span, "data with this lifetime...");
42+
err.span_label(
43+
param_info.param_ty_span,
44+
&format!("this data with {}...", lifetime),
45+
);
3946
debug!("try_report_static_impl_trait: param_info={:?}", param_info);
4047

4148
// We try to make the output have fewer overlapping spans if possible.
@@ -60,10 +67,6 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
6067
);
6168
}
6269

63-
let (lifetime, _) = msg_span_from_free_region(self.tcx(), sup_r);
64-
65-
let lifetime_name =
66-
if sup_r.has_name() { sup_r.to_string() } else { "'_".to_owned() };
6770
// only apply this suggestion onto functions with
6871
// explicit non-desugar'able return.
6972
if fn_return.span.desugaring_kind().is_none() {
@@ -93,8 +96,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
9396
{
9497
err.span_suggestion_verbose(
9598
span,
96-
"consider changing the `impl Trait`'s explicit \
97-
`'static` bound",
99+
&format!(
100+
"consider changing the `impl Trait`'s explicit \
101+
`'static` bound to {}",
102+
lifetime,
103+
),
98104
lifetime_name,
99105
Applicability::MaybeIncorrect,
100106
);
@@ -118,40 +124,41 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
118124
);
119125
};
120126
}
121-
TyKind::TraitObject(_, lt) => {
122-
match lt.name {
123-
LifetimeName::ImplicitObjectLifetimeDefault => {
124-
err.span_suggestion_verbose(
125-
fn_return.span.shrink_to_hi(),
126-
&format!(
127-
"to permit non-static references in a trait object \
128-
value, you can add an explicit bound for {}",
129-
lifetime,
130-
),
131-
format!(" + {}", lifetime_name),
132-
Applicability::MaybeIncorrect,
133-
);
134-
}
135-
_ => {
136-
err.span_suggestion_verbose(
137-
lt.span,
127+
TyKind::TraitObject(_, lt) => match lt.name {
128+
LifetimeName::ImplicitObjectLifetimeDefault => {
129+
err.span_suggestion_verbose(
130+
fn_return.span.shrink_to_hi(),
131+
&format!(
132+
"to permit non-static references in a trait object \
133+
value, you can add an explicit bound for {}",
134+
lifetime,
135+
),
136+
format!(" + {}", lifetime_name),
137+
Applicability::MaybeIncorrect,
138+
);
139+
}
140+
_ => {
141+
err.span_suggestion_verbose(
142+
lt.span,
143+
&format!(
138144
"consider changing the trait object's explicit \
139-
`'static` bound",
140-
lifetime_name,
141-
Applicability::MaybeIncorrect,
142-
);
143-
err.span_suggestion_verbose(
144-
param_info.param_ty_span,
145-
&format!(
146-
"alternatively, set an explicit `'static` lifetime \
147-
in this parameter",
148-
),
149-
param_info.param_ty.to_string(),
150-
Applicability::MaybeIncorrect,
151-
);
152-
}
145+
`'static` bound to {}",
146+
lifetime,
147+
),
148+
lifetime_name,
149+
Applicability::MaybeIncorrect,
150+
);
151+
err.span_suggestion_verbose(
152+
param_info.param_ty_span,
153+
&format!(
154+
"alternatively, set an explicit `'static` lifetime \
155+
in this parameter",
156+
),
157+
param_info.param_ty.to_string(),
158+
Applicability::MaybeIncorrect,
159+
);
153160
}
154-
}
161+
},
155162
_ => {}
156163
}
157164
}

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
| ^^^^^
66
| |
7-
| data with this lifetime...
7+
| this data with the anonymous lifetime `'_`...
88
| ...is captured here...
99
LL | foo(|| self.bar()).await;
1010
| --- ...and required to be `'static` by this

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ LL | fn elided(x: &i32) -> impl Copy { x }
55
| ---- --------- ^ ...and is captured here
66
| | |
77
| | ...is required to be `'static` by this...
8-
| data with this lifetime...
8+
| this data with the anonymous lifetime `'_`...
99
|
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
10+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime `'_`
1111
|
1212
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
1313
| ^^^^
@@ -19,9 +19,9 @@ LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
1919
| ------- --------- ^ ...and is captured here
2020
| | |
2121
| | ...is required to be `'static` by this...
22-
| data with this lifetime...
22+
| this data with lifetime `'a`...
2323
|
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
24+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for lifetime `'a`
2525
|
2626
LL | fn explicit<'a>(x: &'a i32) -> impl Copy + 'a { x }
2727
| ^^^^
@@ -33,9 +33,9 @@ LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
3333
| ---- ------------------- ^ ...and is captured here
3434
| | |
3535
| | ...is required to be `'static` by this...
36-
| data with this lifetime...
36+
| this data with the anonymous lifetime `'_`...
3737
|
38-
help: consider changing the `impl Trait`'s explicit `'static` bound
38+
help: consider changing the `impl Trait`'s explicit `'static` bound to the anonymous lifetime `'_`
3939
|
4040
LL | fn elided2(x: &i32) -> impl Copy + '_ { x }
4141
| ^^
@@ -51,9 +51,9 @@ LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
5151
| ------- ------------------- ^ ...and is captured here
5252
| | |
5353
| | ...is required to be `'static` by this...
54-
| data with this lifetime...
54+
| this data with lifetime `'a`...
5555
|
56-
help: consider changing the `impl Trait`'s explicit `'static` bound
56+
help: consider changing the `impl Trait`'s explicit `'static` bound to lifetime `'a`
5757
|
5858
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'a { x }
5959
| ^^
@@ -77,9 +77,9 @@ LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
7777
| ------- -------------------------------- ^ ...and is captured here
7878
| | |
7979
| | ...is required to be `'static` by this...
80-
| data with this lifetime...
80+
| this data with lifetime `'a`...
8181
|
82-
help: consider changing the `impl Trait`'s explicit `'static` bound
82+
help: consider changing the `impl Trait`'s explicit `'static` bound to lifetime `'a`
8383
|
8484
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'a { x }
8585
| ^^
@@ -113,9 +113,9 @@ LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
113113
| | | |
114114
| | | ...and is captured here
115115
| | ...is required to be `'static` by this...
116-
| data with this lifetime...
116+
| this data with the anonymous lifetime `'_`...
117117
|
118-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 18:1
118+
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
119119
|
120120
LL | fn elided3(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
121121
| ^^^^
@@ -128,9 +128,9 @@ LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
128128
| | | |
129129
| | | ...and is captured here
130130
| | ...is required to be `'static` by this...
131-
| data with this lifetime...
131+
| this data with lifetime `'a`...
132132
|
133-
help: to permit non-static references in a trait object value, you can add an explicit bound for the lifetime `'a` as defined on the function body at 21:14
133+
help: to permit non-static references in a trait object value, you can add an explicit bound for lifetime `'a`
134134
|
135135
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }
136136
| ^^^^
@@ -142,9 +142,10 @@ LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
142142
| ---- ---------^-
143143
| | | |
144144
| | | ...and is captured here
145-
| data with this lifetime... ...is required to be `'static` by this...
145+
| | ...is required to be `'static` by this...
146+
| this data with the anonymous lifetime `'_`...
146147
|
147-
help: consider changing the trait object's explicit `'static` bound
148+
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
148149
|
149150
LL | fn elided4(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
150151
| ^^
@@ -160,9 +161,10 @@ LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
160161
| ------- ---------^-
161162
| | | |
162163
| | | ...and is captured here
163-
| data with this lifetime... ...is required to be `'static` by this...
164+
| | ...is required to be `'static` by this...
165+
| this data with lifetime `'a`...
164166
|
165-
help: consider changing the trait object's explicit `'static` bound
167+
help: consider changing the trait object's explicit `'static` bound to lifetime `'a`
166168
|
167169
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }
168170
| ^^

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error: cannot infer an appropriate lifetime
44
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> {
55
| ----- ----------------------- ...is required to be `'static` by this...
66
| |
7-
| data with this lifetime...
7+
| this data with the anonymous lifetime `'_`...
88
LL | self.x.iter().map(|a| a.0)
99
| ------ ^^^^
1010
| |
1111
| ...and is captured here
1212
|
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
13+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for the anonymous lifetime `'_`
1414
|
1515
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
1616
| ^^^^
@@ -21,13 +21,13 @@ error: cannot infer an appropriate lifetime
2121
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
2222
| -------- ----------------------- ...is required to be `'static` by this...
2323
| |
24-
| data with this lifetime...
24+
| this data with lifetime `'a`...
2525
LL | self.x.iter().map(|a| a.0)
2626
| ------ ^^^^
2727
| |
2828
| ...and is captured here
2929
|
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
30+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for lifetime `'a`
3131
|
3232
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
3333
| ^^^^

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/issue-16922.rs:4:14
33
|
44
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
5-
| -- data with this lifetime...
5+
| -- this data with the anonymous lifetime `'_`...
66
LL | Box::new(value) as Box<dyn Any>
77
| ---------^^^^^-
88
| | |
99
| | ...and is captured here
1010
| ...is required to be `'static` by this...
1111
|
12-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime #1 defined on the function body at 3:1
12+
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
1313
|
1414
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {
1515
| ^^^^

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ error: cannot infer an appropriate lifetime
22
--> $DIR/object-lifetime-default-from-box-error.rs:18:5
33
|
44
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait> {
5-
| --------------- data with this lifetime...
5+
| --------------- this data with the anonymous lifetime `'_`...
66
...
77
LL | ss.r
88
| ^^^^ ...is captured and required to be `'static` here
99
|
10-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime #2 defined on the function body at 14:1
10+
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
1111
|
1212
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait + '_> {
1313
| ^^^^

0 commit comments

Comments
 (0)