Skip to content

Commit ba76f51

Browse files
Rollup merge of rust-lang#39091 - richard-imaoka:E0491-long-explanation, r=GuillaumeGomez
Add error explanation for E0491 Refs rust-lang#32777 Please let me know if any question or anything doesn't look right about this.
2 parents 7985ae0 + c525094 commit ba76f51

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/librustc/diagnostics.rs

Lines changed: 34 additions & 1 deletion
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
@@ -1697,7 +1731,6 @@ register_diagnostics! {
16971731
E0488, // lifetime of variable does not enclose its declaration
16981732
E0489, // type/lifetime parameter not in scope here
16991733
E0490, // a value of type `..` is borrowed for too long
1700-
E0491, // in type `..`, reference has a longer lifetime than the data it...
17011734
E0495, // cannot infer an appropriate lifetime due to conflicting requirements
17021735
E0566 // conflicting representation hints
17031736
}

0 commit comments

Comments
 (0)