Skip to content

Commit c525094

Browse files
committed
Add error explanation for E0491
1 parent 0ef85a6 commit c525094

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/librustc/diagnostics.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,40 @@ struct Prince<'kiss, 'SnowWhite: 'kiss> { // You say here that 'kiss must live
14541454
```
14551455
"##,
14561456

1457+
E0491: r##"
1458+
A reference has a longer lifetime than the data it references.
1459+
1460+
Erroneous code example:
1461+
1462+
```compile_fail,E0491
1463+
// struct containing a reference requires a lifetime parameter,
1464+
// because the data the reference points to must outlive the struct (see E0106)
1465+
struct Struct<'a> {
1466+
ref_i32: &'a i32,
1467+
}
1468+
1469+
// However, a nested struct like this, the signature itself does not tell
1470+
// whether 'a outlives 'b or the other way around.
1471+
// So it could be possible that 'b of reference outlives 'a of the data.
1472+
struct Nested<'a, 'b> {
1473+
ref_struct: &'b Struct<'a>, // compile error E0491
1474+
}
1475+
```
1476+
1477+
To fix this issue, you can specify a bound to the lifetime like below:
1478+
1479+
```
1480+
struct Struct<'a> {
1481+
ref_i32: &'a i32,
1482+
}
1483+
1484+
// 'a: 'b means 'a outlives 'b
1485+
struct Nested<'a: 'b, 'b> {
1486+
ref_struct: &'b Struct<'a>,
1487+
}
1488+
```
1489+
"##,
1490+
14571491
E0496: r##"
14581492
A lifetime name is shadowing another lifetime name. Erroneous code example:
14591493
@@ -1698,7 +1732,6 @@ register_diagnostics! {
16981732
E0488, // lifetime of variable does not enclose its declaration
16991733
E0489, // type/lifetime parameter not in scope here
17001734
E0490, // a value of type `..` is borrowed for too long
1701-
E0491, // in type `..`, reference has a longer lifetime than the data it...
17021735
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
17031736
E0566 // conflicting representation hints
17041737
}

0 commit comments

Comments
 (0)