-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add long description and test for E0311 #100747
Changes from 13 commits
fa91980
08fa70e
63de1ec
a9cefd0
dbcc409
231e3a0
dd7c48e
fc02eee
4f194a7
de3e95b
deadf07
4a443df
eda2a40
24aab52
0d9c014
c0d32fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,44 @@ | ||||||||||
This error occurs when there is an unsatisfied outlives bound involving an | ||||||||||
elided region on 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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still relevant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missed this one! fixed. |
||||||||||
`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 [implied | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
(Remove the double "implied bound") Alternatively, remove the link for now, and then send a new PR when the reference PR lands. I don't think it's worth blocking on the reference PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had figured that updating the docs would be quick, but it looks like it's going to be a more involved process. For now I removed the implied bounds link and will plan to merge this PR. Once rust-lang/reference#1261 has been merged I'll plan to open a smaller PR to add the reference link into the |
||||||||||
bound](https://rust-lang.github.io/rfcs/2089-implied-bounds.html), causing this | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
oh wow, we really don't have a user facing doc for implied bounds xx that sucks the rfc is about implied trait bounds. implied bounds already existed before that and are stable, unlike this rfc. We should really have a page in the reference for that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opened rust-lang/reference#1261 so maybe we should wait for that to land so that you can point there There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for opening the PR to add the reference material. I added some comments on it and will hold this PR until that one lands. |
||||||||||
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 | ||||||||||
} | ||||||||||
``` |
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() {} |
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<T: 'a>(x: &()) -> &() { | ||
| ++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0311`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.