-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Add long description and test for E0311 #100747
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
Changes from 6 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,70 @@ | ||||||||
| This error occurs when there is insufficient information for the rust compiler | ||||||||
| to prove that a type has a long enough lifetime. | ||||||||
|
||||||||
| This error occurs when there is insufficient information for the rust compiler | |
| to prove that a type has a long enough lifetime. | |
| This error occurs when there is an unsatisfied outlives bound on a generic type parameter or associated type. |
something like this maybe?
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.
Yeah - that's much better.
Outdated
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.
This isn't quite right either: T is a type and cannot have a lifetime. An object of type T, such as self can have a lifetime.
Really what we have is more like:
- suppose that
selfhas a lifetime of'b - the output object of type
Vhas a lifetime of'c
If we split up the lines in the example we get:
let u_ref = self.borrow_mut(); // u_ref has lifetime `'b` matching `self`
u_ref.borrow_mut() // u_ref has lifetime of `'c` matching the output
The compiler problem then boils to do failing to prove that 'c will be longer than 'b.
Perhaps the right approach here is to copy the failing example above and then add the lifetime annotations that the compiler attempts to add implicitly, similar to how they are shown in the examples in https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html.
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.
I rewrote this to be more clear and correct.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| use std::borrow::BorrowMut; | ||
|
|
||
| trait NestedBorrowMut<U, V> { | ||
| fn nested_borrow_mut(&mut self) -> &mut V; | ||
| } | ||
|
|
||
| impl<T, U, V> NestedBorrowMut<U, V> for T | ||
| where | ||
| T: BorrowMut<U>, | ||
| U: BorrowMut<V>, // Error is caused by missing lifetime here | ||
| { | ||
| fn nested_borrow_mut(&mut self) -> &mut V { | ||
| let u_ref = self.borrow_mut(); //~ ERROR E0311 | ||
| u_ref.borrow_mut() //~ ERROR E0311 | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| error[E0311]: the parameter type `U` may not live long enough | ||
| --> $DIR/E0311.rs:13:21 | ||
| | | ||
| LL | let u_ref = self.borrow_mut(); | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| | | ||
| note: the parameter type `U` must be valid for the anonymous lifetime defined here... | ||
| --> $DIR/E0311.rs:12:26 | ||
| | | ||
| LL | fn nested_borrow_mut(&mut self) -> &mut V { | ||
| | ^^^^^^^^^ | ||
| note: ...so that the type `U` will meet its required lifetime bounds | ||
| --> $DIR/E0311.rs:13:21 | ||
| | | ||
| LL | let u_ref = self.borrow_mut(); | ||
| | ^^^^^^^^^^^^^^^^^ | ||
| help: consider adding an explicit lifetime bound... | ||
| | | ||
| LL | U: BorrowMut<V> + 'a, // Error is caused by missing lifetime here | ||
| | ++++ | ||
|
|
||
| error[E0311]: the parameter type `U` may not live long enough | ||
| --> $DIR/E0311.rs:14:9 | ||
| | | ||
| LL | u_ref.borrow_mut() | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| note: the parameter type `U` must be valid for the anonymous lifetime defined here... | ||
| --> $DIR/E0311.rs:12:26 | ||
| | | ||
| LL | fn nested_borrow_mut(&mut self) -> &mut V { | ||
| | ^^^^^^^^^ | ||
| note: ...so that the type `U` will meet its required lifetime bounds | ||
| --> $DIR/E0311.rs:14:9 | ||
| | | ||
| LL | u_ref.borrow_mut() | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| help: consider adding an explicit lifetime bound... | ||
| | | ||
| LL | U: BorrowMut<V> + 'a, // Error is caused by missing lifetime here | ||
| | ++++ | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| 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.
I think that
typewould be more correct asparameter.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.