-
Notifications
You must be signed in to change notification settings - Fork 13.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
Merged
bors
merged 16 commits into
rust-lang:master
from
MatthewPeterKelly:mpk/add-long-error-message-for-E0311
Sep 28, 2022
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fa91980
Add long description and test for E0311
MatthewPeterKelly 08fa70e
fix updated stderr outputs
MatthewPeterKelly 63de1ec
Apply suggestions from code review
MatthewPeterKelly a9cefd0
fix line lengths
MatthewPeterKelly dbcc409
Improve E0311.md description
MatthewPeterKelly 231e3a0
actually fix typo this time
MatthewPeterKelly dd7c48e
Improve description again
MatthewPeterKelly fc02eee
fix wrapping
MatthewPeterKelly 4f194a7
Fix rust-doc error
MatthewPeterKelly de3e95b
Review updates: simpler MWE and docs
MatthewPeterKelly deadf07
fix trailing `]`
MatthewPeterKelly 4a443df
review updates to E0311 description
MatthewPeterKelly eda2a40
Merge remote-tracking branch 'origin/master' into mpk/add-long-error-…
MatthewPeterKelly 24aab52
Merge remote-tracking branch 'origin/master' into mpk/add-long-error-…
MatthewPeterKelly 0d9c014
remove implied link bound per review
MatthewPeterKelly c0d32fd
review updates
MatthewPeterKelly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,49 @@ | ||||||
This error occurs when there is insufficient information for the rust compiler | ||||||
to prove that some time has a long enough lifetime. | ||||||
|
||||||
Erroneous code example: | ||||||
|
||||||
```compile_fail,E0311 | ||||||
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: missing lifetime specifier | ||||||
{ | ||||||
fn nested_borrow_mut(&mut self) -> &mut V { | ||||||
self.borrow_mut().borrow_mut() | ||||||
} | ||||||
} | ||||||
``` | ||||||
|
||||||
In this example we have a trait that borrows some inner data element of type `V` | ||||||
from an outer type `T`, through an intermediate type `U`. The compiler is unable | ||||||
to prove that the livetime of `U` is long enough to support the reference. To | ||||||
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. fixed. |
||||||
fix the issue we can explicitly add lifetime specifiers to the `NestedBorrowMut` | ||||||
trait, which link the lifetimes of the various data types and allow the code to | ||||||
compile. | ||||||
|
||||||
Working implementation of the `NestedBorrowMut` trait: | ||||||
|
||||||
``` | ||||||
use std::borrow::BorrowMut; | ||||||
|
||||||
trait NestedBorrowMut<'a, U, V> { | ||||||
fn nested_borrow_mut(& 'a mut self) -> &'a mut V; | ||||||
} | ||||||
|
||||||
impl<'a, T, U, V> NestedBorrowMut<'a, U, V> for T | ||||||
where | ||||||
T: BorrowMut<U>, | ||||||
U: BorrowMut<V> + 'a, // Adding lifetime specifier | ||||||
{ | ||||||
fn nested_borrow_mut(&'a mut self) -> &'a mut V { | ||||||
self.borrow_mut().borrow_mut() | ||||||
} | ||||||
} | ||||||
``` |
This file contains hidden or 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,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() {} |
This file contains hidden or 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,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`. |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.