Skip to content

Commit

Permalink
Rollup merge of rust-lang#72127 - jademcgough:long-error-explanation-…
Browse files Browse the repository at this point in the history
…E0228, r=petrochenkov

add long error explanation for E0228

Add long explanation for the E0228 error code
Part of rust-lang#61137

Let me know if this is wrong at all (or can be written more clearly), I'm still learning Rust.
  • Loading branch information
Dylan-DPC committed May 14, 2020
2 parents 96caa25 + 5320bd9 commit 746b8ca
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/librustc_error_codes/error_codes.rs
Expand Up @@ -120,6 +120,7 @@ E0223: include_str!("./error_codes/E0223.md"),
E0224: include_str!("./error_codes/E0224.md"),
E0225: include_str!("./error_codes/E0225.md"),
E0226: include_str!("./error_codes/E0226.md"),
E0228: include_str!("./error_codes/E0228.md"),
E0229: include_str!("./error_codes/E0229.md"),
E0230: include_str!("./error_codes/E0230.md"),
E0231: include_str!("./error_codes/E0231.md"),
Expand Down Expand Up @@ -482,7 +483,6 @@ E0753: include_str!("./error_codes/E0753.md"),
// E0218, // no associated type defined
// E0219, // associated type defined in higher-ranked supertrait
E0227, // ambiguous lifetime bound, explicit lifetime bound required
E0228, // explicit lifetime bound required
// E0233,
// E0234,
// E0235, // structure constructor specifies a structure of type but
Expand Down
40 changes: 40 additions & 0 deletions src/librustc_error_codes/error_codes/E0228.md
@@ -0,0 +1,40 @@
The lifetime bound for this object type cannot be deduced from context and must
be specified.

Erroneous code example:

```compile_fail,E0228
trait Trait { }
struct TwoBounds<'a, 'b, T: Sized + 'a + 'b> {
x: &'a i32,
y: &'b i32,
z: T,
}
type Foo<'a, 'b> = TwoBounds<'a, 'b, dyn Trait>;
```

When a trait object is used as a type argument of a generic type, Rust will try
to infer its lifetime if unspecified. However, this isn't possible when the
containing type has more than one lifetime bound.

The above example can be resolved by either reducing the number of lifetime
bounds to one or by making the trait object lifetime explicit, like so:

```
trait Trait { }
struct TwoBounds<'a, 'b, T: Sized + 'a + 'b> {
x: &'a i32,
y: &'b i32,
z: T,
}
type Foo<'a, 'b> = TwoBounds<'a, 'b, dyn Trait + 'b>;
```

For more information, see [RFC 599] and its amendment [RFC 1156].

[RFC 599]: https://github.com/rust-lang/rfcs/blob/master/text/0599-default-object-bound.md
[RFC 1156]: https://github.com/rust-lang/rfcs/blob/master/text/1156-adjust-default-object-bounds.md
Expand Up @@ -18,3 +18,4 @@ LL | fn f(t: &Ref2<dyn Test>) {

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0228`.
Expand Up @@ -6,3 +6,4 @@ LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0228`.
Expand Up @@ -6,3 +6,4 @@ LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0228`.
Expand Up @@ -6,3 +6,4 @@ LL | fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() }

error: aborting due to previous error

For more information about this error, try `rustc --explain E0228`.
2 changes: 1 addition & 1 deletion src/test/ui/suggestions/missing-lifetime-specifier.stderr
Expand Up @@ -252,5 +252,5 @@ LL | static f: RefCell<HashMap<i32, Vec<Vec<&Tar<'static, i32>>>>> = RefCell

error: aborting due to 28 previous errors

Some errors have detailed explanations: E0106, E0107.
Some errors have detailed explanations: E0106, E0107, E0228.
For more information about an error, try `rustc --explain E0106`.
Expand Up @@ -18,4 +18,5 @@ LL | x: Box<dyn Debug + '_>,

error: aborting due to 2 previous errors

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

0 comments on commit 746b8ca

Please sign in to comment.