]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0228.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0228.md
1 The lifetime bound for this object type cannot be deduced from context and must
2 be specified.
3
4 Erroneous code example:
5
6 ```compile_fail,E0228
7 trait Trait { }
8
9 struct TwoBounds<'a, 'b, T: Sized + 'a + 'b> {
10     x: &'a i32,
11     y: &'b i32,
12     z: T,
13 }
14
15 type Foo<'a, 'b> = TwoBounds<'a, 'b, dyn Trait>;
16 ```
17
18 When a trait object is used as a type argument of a generic type, Rust will try
19 to infer its lifetime if unspecified. However, this isn't possible when the
20 containing type has more than one lifetime bound.
21
22 The above example can be resolved by either reducing the number of lifetime
23 bounds to one or by making the trait object lifetime explicit, like so:
24
25 ```
26 trait Trait { }
27
28 struct TwoBounds<'a, 'b, T: Sized + 'a + 'b> {
29     x: &'a i32,
30     y: &'b i32,
31     z: T,
32 }
33
34 type Foo<'a, 'b> = TwoBounds<'a, 'b, dyn Trait + 'b>;
35 ```
36
37 For more information, see [RFC 599] and its amendment [RFC 1156].
38
39 [RFC 599]: https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
40 [RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md