-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 #100747 - MatthewPeterKelly:mpk/add-long-error-messag…
…e-for-E0311, r=MatthewPeterKelly Add long description and test for E0311 Adds a long description and unit test for the E0311 compiler error. Fixes one line-item in #61137.
- Loading branch information
Showing
7 changed files
with
79 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
This error occurs when there is an unsatisfied outlives bound involving an | ||
elided region and a generic type parameter or associated type. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0311 | ||
fn no_restriction<T>(x: &()) -> &() { | ||
with_restriction::<T>(x) | ||
} | ||
fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { | ||
x | ||
} | ||
``` | ||
|
||
Why doesn't this code compile? It helps to look at the lifetime bounds that are | ||
automatically added by the compiler. For more details see the documentation for | ||
[lifetime elision]( https://doc.rust-lang.org/reference/lifetime-elision.html). | ||
|
||
The compiler elides the lifetime of `x` and the return type to some arbitrary | ||
lifetime `'anon` in `no_restriction()`. The only information available to the | ||
compiler is that `'anon` is valid for the duration of the function. When | ||
calling `with_restriction()`, the compiler requires the completely unrelated | ||
type parameter `T` to outlive `'anon` because of the `T: 'a` bound in | ||
`with_restriction()`. This causes an error because `T` is not required to | ||
outlive `'anon` in `no_restriction()`. | ||
|
||
If `no_restriction()` were to use `&T` instead of `&()` as an argument, the | ||
compiler would have added an implied bound, causing this to compile. | ||
|
||
This error can be resolved by explicitly naming the elided lifetime for `x` and | ||
then explicily requiring that the generic parameter `T` outlives that lifetime: | ||
|
||
``` | ||
fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { | ||
with_restriction::<T>(x) | ||
} | ||
fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { | ||
x | ||
} | ||
``` |
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,9 @@ | ||
fn no_restriction<T>(x: &()) -> &() { | ||
with_restriction::<T>(x) //~ ERROR E0311 | ||
} | ||
|
||
fn with_restriction<'a, T: 'a>(x: &'a ()) -> &'a () { | ||
x | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
error[E0311]: the parameter type `T` may not live long enough | ||
--> $DIR/E0311.rs:2:5 | ||
| | ||
LL | with_restriction::<T>(x) | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the parameter type `T` must be valid for the anonymous lifetime defined here... | ||
--> $DIR/E0311.rs:1:25 | ||
| | ||
LL | fn no_restriction<T>(x: &()) -> &() { | ||
| ^^^ | ||
note: ...so that the type `T` will meet its required lifetime bounds | ||
--> $DIR/E0311.rs:2:5 | ||
| | ||
LL | with_restriction::<T>(x) | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
help: consider adding an explicit lifetime bound... | ||
| | ||
LL | fn no_restriction<'a, T: 'a>(x: &()) -> &() { | ||
| +++ ++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0311`. |
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