Skip to content

Commit 28b0e1d

Browse files
Add long error explanation for E0495
1 parent e3cb9ea commit 28b0e1d

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/librustc/error_codes.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,47 @@ where
15201520
```
15211521
"##,
15221522

1523+
E0495: r##"
1524+
A lifetime cannot be determined in the given situation.
1525+
1526+
Erroneous code example:
1527+
1528+
```compile_fail,E0495
1529+
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
1530+
match (&t,) { // error!
1531+
((u,),) => u,
1532+
}
1533+
}
1534+
1535+
let y = Box::new((42,));
1536+
let x = transmute_lifetime(&y);
1537+
```
1538+
1539+
In this code, you have two ways to solve this issue:
1540+
1. Enforce that `'a` lives at least as long as `'b`.
1541+
2. Use the same lifetime requirement for both input and output values.
1542+
1543+
So for the first solution, you can do it by replacing `'a` with `'a: 'b`:
1544+
1545+
```
1546+
fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T {
1547+
match (&t,) { // ok!
1548+
((u,),) => u,
1549+
}
1550+
}
1551+
```
1552+
1553+
In the second you can do it by simply removing `'b` so they both use `'a`:
1554+
1555+
```
1556+
fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T {
1557+
match (&t,) { // ok!
1558+
((u,),) => u,
1559+
}
1560+
}
1561+
```
1562+
"##,
1563+
15231564
E0496: r##"
15241565
A lifetime name is shadowing another lifetime name. Erroneous code example:
15251566
@@ -2116,8 +2157,6 @@ rejected in your own crates.
21162157
E0488, // lifetime of variable does not enclose its declaration
21172158
E0489, // type/lifetime parameter not in scope here
21182159
E0490, // a value of type `..` is borrowed for too long
2119-
E0495, // cannot infer an appropriate lifetime due to conflicting
2120-
// requirements
21212160
E0623, // lifetime mismatch where both parameters are anonymous regions
21222161
E0628, // generators cannot have explicit parameters
21232162
E0631, // type mismatch in closure arguments

0 commit comments

Comments
 (0)