Skip to content

Commit

Permalink
Rollup merge of rust-lang#90667 - rukai:improve_static_lifetime_diagn…
Browse files Browse the repository at this point in the history
…ostics, r=estebank

Improve diagnostics when a static lifetime is expected

Makes progress towards rust-lang#90600

The diagnostics here were previously entirely removed due to giving a misleading suggestion but if we instead provide an informative label in that same location it should better help the user understand the situation.

I included the example from the issue as it demonstrates an area where the diagnostics are still lacking.
Happy to remove that if its just adding noise atm.
  • Loading branch information
matthiaskrgr authored Nov 13, 2021
2 parents 5f0cd0a + 60767b8 commit ab2570d
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
new_ty.to_string(),
Applicability::Unspecified,
);
} else {
diag.span_label(
new_ty_span,
&format!("{} does not have lifetime `'static'`", span_label_var),
);
}

Some(diag)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5
|
LL | fn foo(x: &()) {
| --- the type of `x` does not have lifetime `'static'`
LL | bar(|| {
| ^^^ lifetime `'static` required

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/generator/generator-region-requirements.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/generator-region-requirements.rs:12:51
|
LL | fn dangle(x: &mut i32) -> &'static mut i32 {
| -------- the type of `x` does not have lifetime `'static'`
...
LL | GeneratorState::Complete(c) => return c,
| ^ lifetime `'static` required

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

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

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

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/issue-46983.rs:2:5
--> $DIR/issue-46983-expected-return-static.rs:2:5
|
LL | fn foo(x: &u32) -> &'static u32 {
| ---- the type of `x` does not have lifetime `'static'`
LL | &*x
| ^^^ lifetime `'static` required

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::cell::RefCell;
use std::io::Read;

fn main() {}

fn inner(mut foo: &[u8]) {
let refcell = RefCell::new(&mut foo);
let read = &refcell as &RefCell<dyn Read>;

read_thing(read);
//~^ ERROR explicit lifetime required in the type of `foo` [E0621]
}

fn read_thing(refcell: &RefCell<dyn Read>) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0621]: explicit lifetime required in the type of `foo`
--> $DIR/issue-90600-expected-return-static-indirect.rs:10:16
|
LL | fn inner(mut foo: &[u8]) {
| ----- the type of `foo` does not have lifetime `'static'`
...
LL | read_thing(read);
| ^^^^ lifetime `'static` required

error: aborting due to previous error

For more information about this error, try `rustc --explain E0621`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/region-lbr-anon-does-not-outlive-static.rs:9:5
|
LL | fn foo(x: &u32) -> &'static u32 {
| ---- the type of `x` does not have lifetime `'static'`
LL | &*x
| ^^^ lifetime `ReStatic` required

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/nll/guarantor-issue-46974.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ LL | *x
error[E0621]: explicit lifetime required in the type of `s`
--> $DIR/guarantor-issue-46974.rs:15:5
|
LL | fn bar(s: &Box<(i32,)>) -> &'static i32 {
| ------------ the type of `s` does not have lifetime `'static'`
LL | // FIXME(#46983): error message should be better
LL | &s.0
| ^^^^ lifetime `'static` required

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/regions/regions-static-bound.migrate.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ LL | fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
error[E0621]: explicit lifetime required in the type of `u`
--> $DIR/regions-static-bound.rs:14:5
|
LL | fn error(u: &(), v: &()) {
| --- the type of `u` does not have lifetime `'static'`
LL | static_id(&u);
| ^^^^^^^^^ lifetime `'static` required

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

Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/regions/regions-static-bound.nll.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ LL | t
error[E0621]: explicit lifetime required in the type of `u`
--> $DIR/regions-static-bound.rs:14:5
|
LL | fn error(u: &(), v: &()) {
| --- the type of `u` does not have lifetime `'static'`
LL | static_id(&u);
| ^^^^^^^^^^^^^ lifetime `'static` required

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

Expand Down

0 comments on commit ab2570d

Please sign in to comment.