Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend E0106, E0261 #57310

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -768,6 +772,35 @@ These can be fixed by declaring lifetime parameters:
```
fn foo<'a>(x: &'a str) {}

struct 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
// error, use of undeclared lifetime name `'a`
impl Foo<'a> {
fn foo<'a>(x: &'a str) {}
}

struct Foo<'a> {
x: &'a str,
}
```

This is fixed by declaring impl block like this:

```
// correct
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These code samples are tested, so please include the struct definition in this sample to make sure it properly compiles.

impl<'a> Foo<'a> {
fn foo(x: &'a str) {}
}

struct Foo<'a> {
x: &'a str,
}