Add long description and test for E0311
Adds a long description and unit test for the E0311 compiler error.
This commit is contained in:
parent
6c943bad02
commit
fa91980d2d
4 changed files with 113 additions and 1 deletions
49
compiler/rustc_error_codes/src/error_codes/E0311.md
Normal file
49
compiler/rustc_error_codes/src/error_codes/E0311.md
Normal file
|
@ -0,0 +1,49 @@
|
|||
E0311 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>, // missing lifetime specifier here --> compile fail
|
||||
{
|
||||
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, so it
|
||||
throws E0311. To fix the issue we can explicitly add lifetime specifiers to the
|
||||
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,
|
||||
{
|
||||
fn nested_borrow_mut(&'a mut self) -> &'a mut V {
|
||||
self.borrow_mut().borrow_mut()
|
||||
}
|
||||
}
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue