diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-01-28 22:25:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-28 22:25:49 +0100 |
| commit | d77db2e99ebc54c73af60223466e9d95a36fe0f1 (patch) | |
| tree | a6995e1eb1f22d67fe3ef79ef8e41fb9db6f4147 | |
| parent | 3fe8b4c043065fccb551bf968168097798769b72 (diff) | |
| parent | a2b75eda955a46ec643b8277b1fb41c4fed94f4e (diff) | |
Rollup merge of #57950 - QuietMisdreavus:lifetime-err-desc, r=estebank
Extend E0106, E0261 This is a reopening of https://github.com/rust-lang/rust/pull/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
| -rw-r--r-- | src/librustc/diagnostics.rs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 287a45a21ea..1e10329bb55 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -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 @@ -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) {} +} ``` "##, |
