Skip to content

Commit 19bb589

Browse files
committed
Tweak wording and add error code
1 parent 17951e2 commit 19bb589

14 files changed

+97
-94
lines changed

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
44
use crate::infer::lexical_region_resolve::RegionResolutionError;
5-
use rustc_errors::{Applicability, ErrorReported};
5+
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
66
use rustc_hir::{GenericBound, ItemKind, Lifetime, LifetimeName, TyKind};
77
use rustc_middle::ty::RegionKind;
88

@@ -35,10 +35,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3535
let (lifetime_name, lifetime) = if sup_r.has_name() {
3636
(sup_r.to_string(), format!("lifetime `{}`", sup_r))
3737
} else {
38-
("'_".to_owned(), "the anonymous lifetime `'_`".to_string())
38+
("'_".to_owned(), "an anonymous lifetime `'_`".to_string())
3939
};
40-
let mut err =
41-
self.tcx().sess.struct_span_err(sp, "cannot infer an appropriate lifetime");
40+
let mut err = struct_span_err!(
41+
self.tcx().sess,
42+
sp,
43+
E0758,
44+
"cannot infer an appropriate lifetime"
45+
);
4246
err.span_label(
4347
param_info.param_ty_span,
4448
&format!("this data with {}...", lifetime),
@@ -61,36 +65,35 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
6165
// |
6266
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
6367
// | ---- ---------^-
64-
// | | | |
65-
// | | | ...and is captured here
66-
// | | ...is required to be `'static` by this...
67-
// | this data with the anonymous lifetime `'_`...
6868
//
6969
// and instead show:
7070
//
7171
// error: cannot infer an appropriate lifetime
7272
// --> $DIR/must_outlive_least_region_or_bound.rs:18:50
7373
// |
7474
// LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
75-
// | ---- ^ ...is captured here with a `'static` requirement
76-
// | |
77-
// | this data with the anonymous lifetime `'_`...
78-
// |
75+
// | ---- ^
7976
err.span_label(
8077
sup_origin.span(),
81-
"...is captured here with a `'static` requirement",
78+
"...is captured here requiring it to live as long as `'static`",
8279
);
8380
} else if sup_origin.span() <= return_sp {
8481
err.span_label(sup_origin.span(), "...is captured here...");
85-
err.span_label(return_sp, "...and required to be `'static` by this");
82+
err.span_label(
83+
return_sp,
84+
"...and required to live as long as `'static` by this",
85+
);
8686
} else {
87-
err.span_label(return_sp, "...is required to be `'static` by this...");
87+
err.span_label(
88+
return_sp,
89+
"...is required to live as long as `'static` by this...",
90+
);
8891
err.span_label(sup_origin.span(), "...and is captured here");
8992
}
9093
} else {
9194
err.span_label(
9295
return_sp,
93-
"...is captured and required to be `'static` here",
96+
"...is captured and required live as long as `'static` here",
9497
);
9598
}
9699

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: cannot infer an appropriate lifetime
22
--> $DIR/issue-62097.rs:12:31
33
|
44
LL | pub async fn run_dummy_fn(&self) {
55
| ^^^^^
66
| |
7-
| this data with the anonymous lifetime `'_`...
7+
| this data with an anonymous lifetime `'_`...
88
| ...is captured here...
99
LL | foo(|| self.bar()).await;
10-
| --- ...and required to be `'static` by this
10+
| --- ...and required to live as long as `'static` by this
1111

1212
error: aborting due to previous error
1313

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: 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 }
55
| ---- --------- ^ ...and is captured here
66
| | |
7-
| | ...is required to be `'static` by this...
8-
| this data with the anonymous lifetime `'_`...
7+
| | ...is required to live as long as `'static` by this...
8+
| this data with an 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 `'_`
10+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for an anonymous lifetime `'_`
1111
|
1212
LL | fn elided(x: &i32) -> impl Copy + '_ { x }
1313
| ^^^^
1414

15-
error: cannot infer an appropriate lifetime
15+
error[E0758]: cannot infer an appropriate lifetime
1616
--> $DIR/must_outlive_least_region_or_bound.rs:6:44
1717
|
1818
LL | fn explicit<'a>(x: &'a i32) -> impl Copy { x }
1919
| ------- --------- ^ ...and is captured here
2020
| | |
21-
| | ...is required to be `'static` by this...
21+
| | ...is required to live as long as `'static` by this...
2222
| this data with lifetime `'a`...
2323
|
2424
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
| ^^^^
2828

29-
error: cannot infer an appropriate lifetime
29+
error[E0758]: cannot infer an appropriate lifetime
3030
--> $DIR/must_outlive_least_region_or_bound.rs:9:46
3131
|
3232
LL | fn elided2(x: &i32) -> impl Copy + 'static { x }
3333
| ---- ------------------- ^ ...and is captured here
3434
| | |
35-
| | ...is required to be `'static` by this...
36-
| this data with the anonymous lifetime `'_`...
35+
| | ...is required to live as long as `'static` by this...
36+
| this data with an anonymous lifetime `'_`...
3737
|
38-
help: consider changing the `impl Trait`'s explicit `'static` bound to the anonymous lifetime `'_`
38+
help: consider changing the `impl Trait`'s explicit `'static` bound to an anonymous lifetime `'_`
3939
|
4040
LL | fn elided2(x: &i32) -> impl Copy + '_ { x }
4141
| ^^
@@ -44,13 +44,13 @@ help: alternatively, set an explicit `'static` lifetime to this parameter
4444
LL | fn elided2(x: &'static i32) -> impl Copy + 'static { x }
4545
| ^^^^^^^^^^^^
4646

47-
error: cannot infer an appropriate lifetime
47+
error[E0758]: cannot infer an appropriate lifetime
4848
--> $DIR/must_outlive_least_region_or_bound.rs:12:55
4949
|
5050
LL | fn explicit2<'a>(x: &'a i32) -> impl Copy + 'static { x }
5151
| ------- ------------------- ^ ...and is captured here
5252
| | |
53-
| | ...is required to be `'static` by this...
53+
| | ...is required to live as long as `'static` by this...
5454
| this data with lifetime `'a`...
5555
|
5656
help: consider changing the `impl Trait`'s explicit `'static` bound to lifetime `'a`
@@ -70,13 +70,13 @@ LL | fn foo<'a>(x: &i32) -> impl Copy + 'a { x }
7070
| |
7171
| help: add explicit lifetime `'a` to the type of `x`: `&'a i32`
7272

73-
error: cannot infer an appropriate lifetime
73+
error[E0758]: cannot infer an appropriate lifetime
7474
--> $DIR/must_outlive_least_region_or_bound.rs:33:69
7575
|
7676
LL | fn with_bound<'a>(x: &'a i32) -> impl LifetimeTrait<'a> + 'static { x }
7777
| ------- -------------------------------- ^ ...and is captured here
7878
| | |
79-
| | ...is required to be `'static` by this...
79+
| | ...is required to live as long as `'static` by this...
8080
| this data with lifetime `'a`...
8181
|
8282
help: consider changing the `impl Trait`'s explicit `'static` bound to lifetime `'a`
@@ -105,24 +105,24 @@ LL | fn ty_param_wont_outlive_static<T:Debug>(x: T) -> impl Debug + 'static {
105105
| |
106106
| help: consider adding an explicit lifetime bound...: `T: 'static +`
107107

108-
error: cannot infer an appropriate lifetime
108+
error[E0758]: cannot infer an appropriate lifetime
109109
--> $DIR/must_outlive_least_region_or_bound.rs:18:50
110110
|
111111
LL | fn elided3(x: &i32) -> Box<dyn Debug> { Box::new(x) }
112-
| ---- ^ ...is captured here with a `'static` requirement
112+
| ---- ^ ...is captured here requiring it to live as long as `'static`
113113
| |
114-
| this data with the anonymous lifetime `'_`...
114+
| this data with an anonymous lifetime `'_`...
115115
|
116-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
116+
help: to permit non-static references in a trait object value, you can add an explicit bound for an anonymous lifetime `'_`
117117
|
118118
LL | fn elided3(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
119119
| ^^^^
120120

121-
error: cannot infer an appropriate lifetime
121+
error[E0758]: cannot infer an appropriate lifetime
122122
--> $DIR/must_outlive_least_region_or_bound.rs:21:59
123123
|
124124
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug> { Box::new(x) }
125-
| ------- ^ ...is captured here with a `'static` requirement
125+
| ------- ^ ...is captured here requiring it to live as long as `'static`
126126
| |
127127
| this data with lifetime `'a`...
128128
|
@@ -131,15 +131,15 @@ help: to permit non-static references in a trait object value, you can add an ex
131131
LL | fn explicit3<'a>(x: &'a i32) -> Box<dyn Debug + 'a> { Box::new(x) }
132132
| ^^^^
133133

134-
error: cannot infer an appropriate lifetime
134+
error[E0758]: cannot infer an appropriate lifetime
135135
--> $DIR/must_outlive_least_region_or_bound.rs:24:60
136136
|
137137
LL | fn elided4(x: &i32) -> Box<dyn Debug + 'static> { Box::new(x) }
138-
| ---- ^ ...is captured here with a `'static` requirement
138+
| ---- ^ ...is captured here requiring it to live as long as `'static`
139139
| |
140-
| this data with the anonymous lifetime `'_`...
140+
| this data with an anonymous lifetime `'_`...
141141
|
142-
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
142+
help: consider changing the trait object's explicit `'static` bound to an anonymous lifetime `'_`
143143
|
144144
LL | fn elided4(x: &i32) -> Box<dyn Debug + '_> { Box::new(x) }
145145
| ^^
@@ -148,11 +148,11 @@ help: alternatively, set an explicit `'static` lifetime in this parameter
148148
LL | fn elided4(x: &'static i32) -> Box<dyn Debug + 'static> { Box::new(x) }
149149
| ^^^^^^^^^^^^
150150

151-
error: cannot infer an appropriate lifetime
151+
error[E0758]: cannot infer an appropriate lifetime
152152
--> $DIR/must_outlive_least_region_or_bound.rs:27:69
153153
|
154154
LL | fn explicit4<'a>(x: &'a i32) -> Box<dyn Debug + 'static> { Box::new(x) }
155-
| ------- this data with lifetime `'a`... ^ ...is captured here with a `'static` requirement
155+
| ------- this data with lifetime `'a`... ^ ...is captured here requiring it to live as long as `'static`
156156
|
157157
help: consider changing the trait object's explicit `'static` bound to lifetime `'a`
158158
|

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: 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-
| ----- ----------------------- ...is required to be `'static` by this...
5+
| ----- ----------------------- ...is required to live as long as `'static` by this...
66
| |
7-
| this data with the anonymous lifetime `'_`...
7+
| this data with an 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 `'_`
13+
help: to permit non-static references in an `impl Trait` value, you can add an explicit bound for an anonymous lifetime `'_`
1414
|
1515
LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + '_ {
1616
| ^^^^
1717

18-
error: cannot infer an appropriate lifetime
18+
error[E0758]: cannot infer an appropriate lifetime
1919
--> $DIR/static-return-lifetime-infered.rs:11:16
2020
|
2121
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
22-
| -------- ----------------------- ...is required to be `'static` by this...
22+
| -------- ----------------------- ...is required to live as long as `'static` by this...
2323
| |
2424
| this data with lifetime `'a`...
2525
LL | self.x.iter().map(|a| a.0)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: 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-
| -- this data with the anonymous lifetime `'_`...
5+
| -- this data with an anonymous lifetime `'_`...
66
LL | Box::new(value) as Box<dyn Any>
7-
| ^^^^^ ...is captured here with a `'static` requirement
7+
| ^^^^^ ...is captured here requiring it to live as long as `'static`
88
|
9-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
9+
help: to permit non-static references in a trait object value, you can add an explicit bound for an anonymous lifetime `'_`
1010
|
1111
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any + '_> {
1212
| ^^^^

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: 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-
| --------------- this data with the anonymous lifetime `'_`...
5+
| --------------- this data with an anonymous lifetime `'_`...
66
...
77
LL | ss.r
8-
| ^^^^ ...is captured and required to be `'static` here
8+
| ^^^^ ...is captured and required live as long as `'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 `'_`
10+
help: to permit non-static references in a trait object value, you can add an explicit bound for an anonymous lifetime `'_`
1111
|
1212
LL | fn load(ss: &mut SomeStruct) -> Box<dyn SomeTrait + '_> {
1313
| ^^^^

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: cannot infer an appropriate lifetime
1+
error[E0758]: cannot infer an appropriate lifetime
22
--> $DIR/region-object-lifetime-in-coercion.rs:8:46
33
|
44
LL | fn a(v: &[u8]) -> Box<dyn Foo + 'static> {
5-
| ----- this data with the anonymous lifetime `'_`...
5+
| ----- this data with an anonymous lifetime `'_`...
66
LL | let x: Box<dyn Foo + 'static> = Box::new(v);
7-
| ^ ...is captured here with a `'static` requirement
7+
| ^ ...is captured here requiring it to live as long as `'static`
88
|
9-
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
9+
help: consider changing the trait object's explicit `'static` bound to an anonymous lifetime `'_`
1010
|
1111
LL | fn a(v: &[u8]) -> Box<dyn Foo + '_> {
1212
| ^^
@@ -15,15 +15,15 @@ help: alternatively, set an explicit `'static` lifetime in this parameter
1515
LL | fn a(v: &'static [u8]) -> Box<dyn Foo + 'static> {
1616
| ^^^^^^^^^^^^^
1717

18-
error: cannot infer an appropriate lifetime
18+
error[E0758]: cannot infer an appropriate lifetime
1919
--> $DIR/region-object-lifetime-in-coercion.rs:13:14
2020
|
2121
LL | fn b(v: &[u8]) -> Box<dyn Foo + 'static> {
22-
| ----- this data with the anonymous lifetime `'_`...
22+
| ----- this data with an anonymous lifetime `'_`...
2323
LL | Box::new(v)
24-
| ^ ...is captured here with a `'static` requirement
24+
| ^ ...is captured here requiring it to live as long as `'static`
2525
|
26-
help: consider changing the trait object's explicit `'static` bound to the anonymous lifetime `'_`
26+
help: consider changing the trait object's explicit `'static` bound to an anonymous lifetime `'_`
2727
|
2828
LL | fn b(v: &[u8]) -> Box<dyn Foo + '_> {
2929
| ^^
@@ -32,16 +32,16 @@ help: alternatively, set an explicit `'static` lifetime in this parameter
3232
LL | fn b(v: &'static [u8]) -> Box<dyn Foo + 'static> {
3333
| ^^^^^^^^^^^^^
3434

35-
error: cannot infer an appropriate lifetime
35+
error[E0758]: cannot infer an appropriate lifetime
3636
--> $DIR/region-object-lifetime-in-coercion.rs:19:14
3737
|
3838
LL | fn c(v: &[u8]) -> Box<dyn Foo> {
39-
| ----- this data with the anonymous lifetime `'_`...
39+
| ----- this data with an anonymous lifetime `'_`...
4040
...
4141
LL | Box::new(v)
42-
| ^ ...is captured here with a `'static` requirement
42+
| ^ ...is captured here requiring it to live as long as `'static`
4343
|
44-
help: to permit non-static references in a trait object value, you can add an explicit bound for the anonymous lifetime `'_`
44+
help: to permit non-static references in a trait object value, you can add an explicit bound for an anonymous lifetime `'_`
4545
|
4646
LL | fn c(v: &[u8]) -> Box<dyn Foo + '_> {
4747
| ^^^^

0 commit comments

Comments
 (0)