]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0491.md
Auto merge of #66396 - smmalis37:pythontest, r=alexcrichton
[rust.git] / src / librustc_error_codes / error_codes / E0491.md
1 A reference has a longer lifetime than the data it references.
2
3 Erroneous code example:
4
5 ```compile_fail,E0491
6 trait SomeTrait<'a> {
7     type Output;
8 }
9
10 impl<'a, T> SomeTrait<'a> for T {
11     type Output = &'a T; // compile error E0491
12 }
13 ```
14
15 Here, the problem is that a reference type like `&'a T` is only valid
16 if all the data in T outlives the lifetime `'a`. But this impl as written
17 is applicable to any lifetime `'a` and any type `T` -- we have no guarantee
18 that `T` outlives `'a`. To fix this, you can add a where clause like
19 `where T: 'a`.
20
21 ```
22 trait SomeTrait<'a> {
23     type Output;
24 }
25
26 impl<'a, T> SomeTrait<'a> for T
27 where
28     T: 'a,
29 {
30     type Output = &'a T; // compile error E0491
31 }
32 ```