Skip to content

Commit 746b8ca

Browse files
authored
Rollup merge of #72127 - jademcgough:long-error-explanation-E0228, r=petrochenkov
add long error explanation for E0228 Add long explanation for the E0228 error code Part of #61137 Let me know if this is wrong at all (or can be written more clearly), I'm still learning Rust.
2 parents 96caa25 + 5320bd9 commit 746b8ca

8 files changed

+48
-3
lines changed

src/librustc_error_codes/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ E0223: include_str!("./error_codes/E0223.md"),
120120
E0224: include_str!("./error_codes/E0224.md"),
121121
E0225: include_str!("./error_codes/E0225.md"),
122122
E0226: include_str!("./error_codes/E0226.md"),
123+
E0228: include_str!("./error_codes/E0228.md"),
123124
E0229: include_str!("./error_codes/E0229.md"),
124125
E0230: include_str!("./error_codes/E0230.md"),
125126
E0231: include_str!("./error_codes/E0231.md"),
@@ -482,7 +483,6 @@ E0753: include_str!("./error_codes/E0753.md"),
482483
// E0218, // no associated type defined
483484
// E0219, // associated type defined in higher-ranked supertrait
484485
E0227, // ambiguous lifetime bound, explicit lifetime bound required
485-
E0228, // explicit lifetime bound required
486486
// E0233,
487487
// E0234,
488488
// E0235, // structure constructor specifies a structure of type but
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

src/test/ui/object-lifetime/object-lifetime-default-ambiguous.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ LL | fn f(t: &Ref2<dyn Test>) {
1818

1919
error: aborting due to 3 previous errors
2020

21+
For more information about this error, try `rustc --explain E0228`.

src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic1.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0228`.

src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0228`.

src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic3.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ LL | fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() }
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0228`.

src/test/ui/suggestions/missing-lifetime-specifier.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,5 @@ LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, i32>>>>> = RefCell
252252

253253
error: aborting due to 28 previous errors
254254

255-
Some errors have detailed explanations: E0106, E0107.
255+
Some errors have detailed explanations: E0106, E0107, E0228.
256256
For more information about an error, try `rustc --explain E0106`.

src/test/ui/underscore-lifetime/dyn-trait-underscore-in-struct.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ LL | x: Box<dyn Debug + '_>,
1818

1919
error: aborting due to 2 previous errors
2020

21-
For more information about this error, try `rustc --explain E0106`.
21+
Some errors have detailed explanations: E0106, E0228.
22+
For more information about an error, try `rustc --explain E0106`.

0 commit comments

Comments
 (0)