Skip to content

Commit 3237ded

Browse files
Add long error explanation for E0495
1 parent 42ec683 commit 3237ded

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
@@ -1580,6 +1580,47 @@ where
15801580
```
15811581
"##,
15821582

1583+
E0495: r##"
1584+
A lifetime cannot be determined in the given situation.
1585+
1586+
Erroneous code example:
1587+
1588+
```compile_fail,E0495
1589+
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
1590+
match (&t,) { // error!
1591+
((u,),) => u,
1592+
}
1593+
}
1594+
1595+
let y = Box::new((42,));
1596+
let x = transmute_lifetime(&y);
1597+
```
1598+
1599+
In this code, you have two ways to solve this issue:
1600+
1. Enforce that `'a` lives at least as long as `'b`.
1601+
2. Use the same lifetime requirement for both input and output values.
1602+
1603+
So for the first solution, you can do it by replacing `'a` with `'a: 'b`:
1604+
1605+
```
1606+
fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T {
1607+
match (&t,) { // ok!
1608+
((u,),) => u,
1609+
}
1610+
}
1611+
```
1612+
1613+
In the second you can do it by simply removing `'b` so they both use `'a`:
1614+
1615+
```
1616+
fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T {
1617+
match (&t,) { // ok!
1618+
((u,),) => u,
1619+
}
1620+
}
1621+
```
1622+
"##,
1623+
15831624
E0496: r##"
15841625
A lifetime name is shadowing another lifetime name. Erroneous code example:
15851626
@@ -2275,8 +2316,6 @@ rejected in your own crates.
22752316
E0488, // lifetime of variable does not enclose its declaration
22762317
E0489, // type/lifetime parameter not in scope here
22772318
E0490, // a value of type `..` is borrowed for too long
2278-
E0495, // cannot infer an appropriate lifetime due to conflicting
2279-
// requirements
22802319
E0566, // conflicting representation hints
22812320
E0623, // lifetime mismatch where both parameters are anonymous regions
22822321
E0628, // generators cannot have explicit parameters

0 commit comments

Comments
 (0)