]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0309.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0309.md
1 A parameter type is missing an explicit lifetime bound and may not live long
2 enough.
3
4 Erroneous code example:
5
6 ```compile_fail,E0309
7 // This won't compile because the applicable impl of
8 // `SomeTrait` (below) requires that `T: 'a`, but the struct does
9 // not have a matching where-clause.
10 struct Foo<'a, T> {
11     foo: <T as SomeTrait<'a>>::Output,
12 }
13
14 trait SomeTrait<'a> {
15     type Output;
16 }
17
18 impl<'a, T> SomeTrait<'a> for T
19 where
20     T: 'a,
21 {
22     type Output = u32;
23 }
24 ```
25
26 The type definition contains some field whose type requires an outlives
27 annotation. Outlives annotations (e.g., `T: 'a`) are used to guarantee that all
28 the data in `T` is valid for at least the lifetime `'a`. This scenario most
29 commonly arises when the type contains an associated type reference like
30 `<T as SomeTrait<'a>>::Output`, as shown in the previous code.
31
32 There, the where clause `T: 'a` that appears on the impl is not known to be
33 satisfied on the struct. To make this example compile, you have to add a
34 where-clause like `T: 'a` to the struct definition:
35
36 ```
37 struct Foo<'a, T>
38 where
39     T: 'a,
40 {
41     foo: <T as SomeTrait<'a>>::Output
42 }
43
44 trait SomeTrait<'a> {
45     type Output;
46 }
47
48 impl<'a, T> SomeTrait<'a> for T
49 where
50     T: 'a,
51 {
52     type Output = u32;
53 }
54 ```