Skip to content

Commit 144b845

Browse files
Add long error explanation for E0312
1 parent c9edc02 commit 144b845

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/librustc/error_codes.rs

+33-1
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,39 @@ struct Foo<T: 'static> {
13471347
```
13481348
"##,
13491349

1350+
E0312: r##"
1351+
Reference's lifetime of borrowed content doesn't match the expected lifetime.
1352+
1353+
Erroneous code example:
1354+
1355+
```compile_fail,E0312
1356+
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str {
1357+
if maybestr.is_none() {
1358+
"(none)"
1359+
} else {
1360+
let s: &'a str = maybestr.as_ref().unwrap();
1361+
s // Invalid lifetime!
1362+
}
1363+
}
1364+
```
1365+
1366+
To fix this error, either lessen the expected lifetime or find a way to not have
1367+
to use this reference outside of its current scope (by running the code directly
1368+
in the same block for example?):
1369+
1370+
```
1371+
// In this case, we can fix the issue by switching from "static" lifetime to 'a
1372+
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str {
1373+
if maybestr.is_none() {
1374+
"(none)"
1375+
} else {
1376+
let s: &'a str = maybestr.as_ref().unwrap();
1377+
s // Ok!
1378+
}
1379+
}
1380+
```
1381+
"##,
1382+
13501383
E0317: r##"
13511384
This error occurs when an `if` expression without an `else` block is used in a
13521385
context where a type other than `()` is expected, for example a `let`
@@ -2202,7 +2235,6 @@ static X: u32 = 42;
22022235
// E0304, // expected signed integer constant
22032236
// E0305, // expected constant
22042237
E0311, // thing may not live long enough
2205-
E0312, // lifetime of reference outlives lifetime of borrowed content
22062238
E0313, // lifetime of borrowed pointer outlives lifetime of captured
22072239
// variable
22082240
E0314, // closure outlives stack frame

0 commit comments

Comments
 (0)