-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #90667 - rukai:improve_static_lifetime_diagnostics, r…
…=estebank Improve diagnostics when a static lifetime is expected Makes progress towards #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
Showing
26 changed files
with
269 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 15 additions & 3 deletions
18
src/test/ui/closures/closure-bounds-static-cant-capture-borrowed.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
error[E0621]: explicit lifetime required in the type of `x` | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:9 | ||
| | ||
LL | fn foo(x: &()) { | ||
| --- this data with an anonymous lifetime `'_`... | ||
LL | bar(|| { | ||
| _________^ | ||
LL | | | ||
LL | | let _ = x; | ||
LL | | }) | ||
| |_____^ ...is captured here... | ||
| | ||
note: ...and is required to live as long as `'static` here | ||
--> $DIR/closure-bounds-static-cant-capture-borrowed.rs:5:5 | ||
| | ||
LL | bar(|| { | ||
| ^^^ lifetime `'static` required | ||
| ^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0621`. | ||
For more information about this error, try `rustc --explain E0759`. |
11 changes: 11 additions & 0 deletions
11
src/test/ui/generator/generator-region-requirements.nll.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/generator-region-requirements.rs:13:51 | ||
| | ||
LL | fn dangle(x: &mut i32) -> &'static mut i32 { | ||
| - let's call the lifetime of this reference `'1` | ||
... | ||
LL | GeneratorState::Complete(c) => return c, | ||
| ^ returning this value requires that `'1` must outlive `'static` | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 10 additions & 4 deletions
14
src/test/ui/generator/generator-region-requirements.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
error[E0621]: explicit lifetime required in the type of `x` | ||
--> $DIR/generator-region-requirements.rs:12:51 | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/generator-region-requirements.rs:8:9 | ||
| | ||
LL | fn dangle(x: &mut i32) -> &'static mut i32 { | ||
| -------- this data with an anonymous lifetime `'_`... | ||
... | ||
LL | x | ||
| ^ ...is captured here... | ||
... | ||
LL | GeneratorState::Complete(c) => return c, | ||
| ^ lifetime `'static` required | ||
| - ...and is required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0621`. | ||
For more information about this error, try `rustc --explain E0759`. |
26 changes: 26 additions & 0 deletions
26
src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.nll.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/projection-type-lifetime-mismatch.rs:17:5 | ||
| | ||
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | x.m() | ||
| ^^^^^ returning this value requires that `'1` must outlive `'static` | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/projection-type-lifetime-mismatch.rs:22:5 | ||
| | ||
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | x.m() | ||
| ^^^^^ returning this value requires that `'1` must outlive `'static` | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/projection-type-lifetime-mismatch.rs:27:5 | ||
| | ||
LL | fn h(x: &()) -> &'static () { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | x.m() | ||
| ^^^^^ returning this value requires that `'1` must outlive `'static` | ||
|
||
error: aborting due to 3 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 16 additions & 10 deletions
26
src/test/ui/generic-associated-types/projection-type-lifetime-mismatch.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
error[E0621]: explicit lifetime required in the type of `x` | ||
--> $DIR/projection-type-lifetime-mismatch.rs:17:5 | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/projection-type-lifetime-mismatch.rs:17:7 | ||
| | ||
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () { | ||
| ------------------------------- this data with an anonymous lifetime `'_`... | ||
LL | x.m() | ||
| ^^^^^ lifetime `'static` required | ||
| --^-- ...is captured and required to live as long as `'static` here | ||
|
||
error[E0621]: explicit lifetime required in the type of `x` | ||
--> $DIR/projection-type-lifetime-mismatch.rs:22:5 | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/projection-type-lifetime-mismatch.rs:22:7 | ||
| | ||
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () { | ||
| -- this data with an anonymous lifetime `'_`... | ||
LL | x.m() | ||
| ^^^^^ lifetime `'static` required | ||
| --^-- ...is captured and required to live as long as `'static` here | ||
|
||
error[E0621]: explicit lifetime required in the type of `x` | ||
--> $DIR/projection-type-lifetime-mismatch.rs:27:5 | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/projection-type-lifetime-mismatch.rs:27:7 | ||
| | ||
LL | fn h(x: &()) -> &'static () { | ||
| --- this data with an anonymous lifetime `'_`... | ||
LL | x.m() | ||
| ^^^^^ lifetime `'static` required | ||
| --^-- ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0621`. | ||
For more information about this error, try `rustc --explain E0759`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/issue-46983.rs:2:5 | ||
| | ||
LL | fn foo(x: &u32) -> &'static u32 { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | &*x | ||
| ^^^ returning this value requires that `'1` must outlive `'static` | ||
|
||
error: aborting due to previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
fn foo(x: &u32) -> &'static u32 { | ||
&*x | ||
//~^ ERROR explicit lifetime required in the type of `x` [E0621] | ||
//~^ ERROR `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
error[E0621]: explicit lifetime required in the type of `x` | ||
error[E0759]: `x` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/issue-46983.rs:2:5 | ||
| | ||
LL | fn foo(x: &u32) -> &'static u32 { | ||
| ---- this data with an anonymous lifetime `'_`... | ||
LL | &*x | ||
| ^^^ lifetime `'static` required | ||
| ^^^ ...is captured and required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0621`. | ||
For more information about this error, try `rustc --explain E0759`. |
24 changes: 24 additions & 0 deletions
24
src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.nll.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0597]: `foo` does not live long enough | ||
--> $DIR/issue-90600-expected-return-static-indirect.rs:7:32 | ||
| | ||
LL | let refcell = RefCell::new(&mut foo); | ||
| ^^^^^^^^ borrowed value does not live long enough | ||
LL | | ||
LL | let read = &refcell as &RefCell<dyn Read>; | ||
| -------- cast requires that `foo` is borrowed for `'static` | ||
... | ||
LL | } | ||
| - `foo` dropped here while still borrowed | ||
|
||
error: lifetime may not live long enough | ||
--> $DIR/issue-90600-expected-return-static-indirect.rs:9:16 | ||
| | ||
LL | fn inner(mut foo: &[u8]) { | ||
| - let's call the lifetime of this reference `'1` | ||
... | ||
LL | let read = &refcell as &RefCell<dyn Read>; | ||
| ^^^^^^^^ cast requires that `'1` must outlive `'static` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0597`. |
14 changes: 14 additions & 0 deletions
14
src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
//~^ ERROR `foo` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement [E0759] | ||
let read = &refcell as &RefCell<dyn Read>; | ||
|
||
read_thing(read); | ||
} | ||
|
||
fn read_thing(refcell: &RefCell<dyn Read>) {} |
14 changes: 14 additions & 0 deletions
14
src/test/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
error[E0759]: `foo` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement | ||
--> $DIR/issue-90600-expected-return-static-indirect.rs:7:32 | ||
| | ||
LL | fn inner(mut foo: &[u8]) { | ||
| ----- this data with an anonymous lifetime `'_`... | ||
LL | let refcell = RefCell::new(&mut foo); | ||
| ^^^^^^^^ ...is captured here... | ||
... | ||
LL | read_thing(read); | ||
| ---- ...and is required to live as long as `'static` here | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0759`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.