Skip to content

Commit 60767b8

Browse files
committed
Improve diagnostics when a static lifetime is expected
1 parent 089a016 commit 60767b8

12 files changed

+60
-1
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/named_anon_conflict.rs

+5
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
123123
new_ty.to_string(),
124124
Applicability::Unspecified,
125125
);
126+
} else {
127+
diag.span_label(
128+
new_ty_span,
129+
&format!("{} does not have lifetime `'static'`", span_label_var),
130+
);
126131
}
127132

128133
Some(diag)

src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
33
|
4+
LL | fn foo(x: &()) {
5+
| --- the type of `x` does not have lifetime `'static'`
46
LL | bar(|| {
57
| ^^^ lifetime `'static` required
68

src/test/ui/generator/generator-region-requirements.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/generator-region-requirements.rs:12:51
33
|
4+
LL | fn dangle(x: &mut i32) -> &'static mut i32 {
5+
| -------- the type of `x` does not have lifetime `'static'`
6+
...
47
LL | GeneratorState::Complete(c) => return c,
58
| ^ lifetime `'static` required
69

src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/projection-type-lifetime-mismatch.rs:17:5
33
|
4+
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
5+
| ------------------------------- the type of `x` does not have lifetime `'static'`
46
LL | x.m()
57
| ^^^^^ lifetime `'static` required
68

79
error[E0621]: explicit lifetime required in the type of `x`
810
--> $DIR/projection-type-lifetime-mismatch.rs:22:5
911
|
12+
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
13+
| -- the type of `x` does not have lifetime `'static'`
1014
LL | x.m()
1115
| ^^^^^ lifetime `'static` required
1216

1317
error[E0621]: explicit lifetime required in the type of `x`
1418
--> $DIR/projection-type-lifetime-mismatch.rs:27:5
1519
|
20+
LL | fn h(x: &()) -> &'static () {
21+
| --- the type of `x` does not have lifetime `'static'`
1622
LL | x.m()
1723
| ^^^^^ lifetime `'static` required
1824

src/test/ui/issues/issue-46983.stderr src/test/ui/lifetimes/issue-46983-expected-return-static.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0621]: explicit lifetime required in the type of `x`
2-
--> $DIR/issue-46983.rs:2:5
2+
--> $DIR/issue-46983-expected-return-static.rs:2:5
33
|
4+
LL | fn foo(x: &u32) -> &'static u32 {
5+
| ---- the type of `x` does not have lifetime `'static'`
46
LL | &*x
57
| ^^^ lifetime `'static` required
68

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::cell::RefCell;
2+
use std::io::Read;
3+
4+
fn main() {}
5+
6+
fn inner(mut foo: &[u8]) {
7+
let refcell = RefCell::new(&mut foo);
8+
let read = &refcell as &RefCell<dyn Read>;
9+
10+
read_thing(read);
11+
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
12+
}
13+
14+
fn read_thing(refcell: &RefCell<dyn Read>) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0621]: explicit lifetime required in the type of `foo`
2+
--> $DIR/issue-90600-expected-return-static-indirect.rs:10:16
3+
|
4+
LL | fn inner(mut foo: &[u8]) {
5+
| ----- the type of `foo` does not have lifetime `'static'`
6+
...
7+
LL | read_thing(read);
8+
| ^^^^ lifetime `'static` required
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0621`.

src/test/ui/nll/closure-requirements/region-lbr-anon-does-not-outlive-static.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
error[E0621]: explicit lifetime required in the type of `x`
22
--> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5
33
|
4+
LL | fn foo(x: &u32) -> &'static u32 {
5+
| ---- the type of `x` does not have lifetime `'static'`
46
LL | &*x
57
| ^^^ lifetime `ReStatic` required
68

src/test/ui/nll/guarantor-issue-46974.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ LL | *x
1212
error[E0621]: explicit lifetime required in the type of `s`
1313
--> $DIR/guarantor-issue-46974.rs:15:5
1414
|
15+
LL | fn bar(s: &Box<(i32,)>) -> &'static i32 {
16+
| ------------ the type of `s` does not have lifetime `'static'`
17+
LL | // FIXME(#46983): error message should be better
1518
LL | &s.0
1619
| ^^^^ lifetime `'static` required
1720

src/test/ui/regions/regions-static-bound.migrate.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
1414
error[E0621]: explicit lifetime required in the type of `u`
1515
--> $DIR/regions-static-bound.rs:14:5
1616
|
17+
LL | fn error(u: &(), v: &()) {
18+
| --- the type of `u` does not have lifetime `'static'`
1719
LL | static_id(&u);
1820
| ^^^^^^^^^ lifetime `'static` required
1921

2022
error[E0621]: explicit lifetime required in the type of `v`
2123
--> $DIR/regions-static-bound.rs:16:5
2224
|
25+
LL | fn error(u: &(), v: &()) {
26+
| --- the type of `v` does not have lifetime `'static'`
27+
...
2328
LL | static_id_indirect(&v);
2429
| ^^^^^^^^^^^^^^^^^^ lifetime `'static` required
2530

src/test/ui/regions/regions-static-bound.nll.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ LL | t
99
error[E0621]: explicit lifetime required in the type of `u`
1010
--> $DIR/regions-static-bound.rs:14:5
1111
|
12+
LL | fn error(u: &(), v: &()) {
13+
| --- the type of `u` does not have lifetime `'static'`
1214
LL | static_id(&u);
1315
| ^^^^^^^^^^^^^ lifetime `'static` required
1416

1517
error[E0621]: explicit lifetime required in the type of `v`
1618
--> $DIR/regions-static-bound.rs:16:5
1719
|
20+
LL | fn error(u: &(), v: &()) {
21+
| --- the type of `v` does not have lifetime `'static'`
22+
...
1823
LL | static_id_indirect(&v);
1924
| ^^^^^^^^^^^^^^^^^^^^^^ lifetime `'static` required
2025

0 commit comments

Comments
 (0)