remove error code description
This commit is contained in:
parent
9e4649ebf8
commit
4bbb58d429
@ -1351,74 +1351,6 @@ struct Foo<T: 'static> {
|
|||||||
```
|
```
|
||||||
"##,
|
"##,
|
||||||
|
|
||||||
E0312: r##"
|
|
||||||
A lifetime of reference outlives lifetime of borrowed content.
|
|
||||||
|
|
||||||
Erroneous code example:
|
|
||||||
|
|
||||||
```compile_fail,E0312
|
|
||||||
fn make_child<'tree, 'human>(
|
|
||||||
x: &'human i32,
|
|
||||||
y: &'tree i32
|
|
||||||
) -> &'human i32 {
|
|
||||||
if x > y
|
|
||||||
{ x }
|
|
||||||
else
|
|
||||||
{ y }
|
|
||||||
// error: lifetime of reference outlives lifetime of borrowed content
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
The function declares that it returns a reference with the `'human`
|
|
||||||
lifetime, but it may return data with the `'tree` lifetime. As neither
|
|
||||||
lifetime is declared longer than the other, this results in an
|
|
||||||
error. Sometimes, this error is because the function *body* is
|
|
||||||
incorrect -- that is, maybe you did not *mean* to return data from
|
|
||||||
`y`. In that case, you should fix the function body.
|
|
||||||
|
|
||||||
Often, however, the body is correct. In that case, the function
|
|
||||||
signature needs to be altered to match the body, so that the caller
|
|
||||||
understands that data from either `x` or `y` may be returned. The
|
|
||||||
simplest way to do this is to give both function parameters the *same*
|
|
||||||
named lifetime:
|
|
||||||
|
|
||||||
```
|
|
||||||
fn make_child<'human>(
|
|
||||||
x: &'human i32,
|
|
||||||
y: &'human i32
|
|
||||||
) -> &'human i32 {
|
|
||||||
if x > y
|
|
||||||
{ x }
|
|
||||||
else
|
|
||||||
{ y } // ok!
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
However, in some cases, you may prefer to explicitly declare that one lifetime
|
|
||||||
outlives another using a `where` clause:
|
|
||||||
|
|
||||||
```
|
|
||||||
fn make_child<'tree, 'human>(
|
|
||||||
x: &'human i32,
|
|
||||||
y: &'tree i32
|
|
||||||
) -> &'human i32
|
|
||||||
where
|
|
||||||
'tree: 'human
|
|
||||||
{
|
|
||||||
if x > y
|
|
||||||
{ x }
|
|
||||||
else
|
|
||||||
{ y } // ok!
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Here, the where clause `'tree: 'human` can be read as "the lifetime
|
|
||||||
'tree outlives the lifetime 'human" -- meaning, references with the
|
|
||||||
`'tree` lifetime live *at least as long as* references with the
|
|
||||||
`'human` lifetime. Therefore, it is safe to return data with lifetime
|
|
||||||
`'tree` when data with the lifetime `'human` is needed.
|
|
||||||
"##,
|
|
||||||
|
|
||||||
E0317: r##"
|
E0317: r##"
|
||||||
This error occurs when an `if` expression without an `else` block is used in a
|
This error occurs when an `if` expression without an `else` block is used in a
|
||||||
context where a type other than `()` is expected, for example a `let`
|
context where a type other than `()` is expected, for example a `let`
|
||||||
@ -2028,6 +1960,7 @@ register_diagnostics! {
|
|||||||
// E0304, // expected signed integer constant
|
// E0304, // expected signed integer constant
|
||||||
// E0305, // expected constant
|
// E0305, // expected constant
|
||||||
E0311, // thing may not live long enough
|
E0311, // thing may not live long enough
|
||||||
|
E0312, // lifetime of reference outlives lifetime of borrowed content
|
||||||
E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
|
E0313, // lifetime of borrowed pointer outlives lifetime of captured variable
|
||||||
E0314, // closure outlives stack frame
|
E0314, // closure outlives stack frame
|
||||||
E0315, // cannot invoke closure outside of its lifetime
|
E0315, // cannot invoke closure outside of its lifetime
|
||||||
|
Loading…
Reference in New Issue
Block a user