Skip to content

Commit

Permalink
Rollup merge of rust-lang#57950 - QuietMisdreavus:lifetime-err-desc, …
Browse files Browse the repository at this point in the history
…r=estebank

Extend E0106, E0261

This is a reopening of rust-lang#57310 with review comments addressed because the original author has since deleted their fork.

From the author (@purple-ice):

> Added an example that points out hardly obvious mistake one could make when writing impl for a new type.

r? @rust-lang/docs
  • Loading branch information
Centril authored Jan 28, 2019
2 parents 3fe8b4c + a2b75ed commit d77db2e
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ struct Foo1 { x: &bool }
// ^ expected lifetime parameter
struct Foo2<'a> { x: &'a bool } // correct
impl Foo2 {}
// ^^^^ expected lifetime parameter
impl<'a> Foo2<'a> {} // correct
struct Bar1 { x: Foo2 }
// ^^^^ expected lifetime parameter
struct Bar2<'a> { x: Foo2<'a> } // correct
Expand Down Expand Up @@ -766,11 +770,40 @@ struct Foo {
These can be fixed by declaring lifetime parameters:
```
struct Foo<'a> {
x: &'a str,
}
fn foo<'a>(x: &'a str) {}
```
Impl blocks declare lifetime parameters separately. You need to add lifetime
parameters to an impl block if you're implementing a type that has a lifetime
parameter of its own.
For example:
```compile_fail,E0261
struct Foo<'a> {
x: &'a str,
}
// error, use of undeclared lifetime name `'a`
impl Foo<'a> {
fn foo<'a>(x: &'a str) {}
}
```
This is fixed by declaring the impl block like this:
```
struct Foo<'a> {
x: &'a str,
}
// correct
impl<'a> Foo<'a> {
fn foo(x: &'a str) {}
}
```
"##,

Expand Down

0 comments on commit d77db2e

Please sign in to comment.