From c738e6042d2925bbfb045982fabe1f902f8b7c72 Mon Sep 17 00:00:00 2001 From: purple-ice Date: Thu, 3 Jan 2019 20:15:01 +0200 Subject: [PATCH 1/2] Extend E0106, E0261 Added an example that points out hardly obvious mistake one could make when writing impl for a new type. --- src/librustc/diagnostics.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 4bc52e82f9be1..50beacf3e8cbb 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 @@ -772,6 +776,24 @@ struct Foo<'a> { x: &'a str, } ``` + +Implementations need their own lifetime declarations: + +``` +// error, undeclared lifetime +impl Foo<'a> { + ... +} +``` + +Which are declared like this: + +``` +// correct +impl<'a> Foo<'a> { + ... +} +``` "##, E0262: r##" From adadefd8741d84ebfb5884fbd12f3202cd6fdb4f Mon Sep 17 00:00:00 2001 From: purple-ice Date: Fri, 4 Jan 2019 13:00:26 +0200 Subject: [PATCH 2/2] Fix changes for E0106, E0261 Replaced impl block dock with suggested one and made sure that blocks compile. --- src/librustc/diagnostics.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/librustc/diagnostics.rs b/src/librustc/diagnostics.rs index 50beacf3e8cbb..dd5144b1568fb 100644 --- a/src/librustc/diagnostics.rs +++ b/src/librustc/diagnostics.rs @@ -362,9 +362,9 @@ struct Foo1 { x: &bool } // ^ expected lifetime parameter struct Foo2<'a> { x: &'a bool } // correct -impl Foo2 { ... } +impl Foo2 {} // ^ expected lifetime parameter -impl<'a> Foo2<'a> { ... } // correct +impl<'a> Foo2<'a> {} // correct struct Bar1 { x: Foo2 } // ^^^^ expected lifetime parameter @@ -777,21 +777,32 @@ struct Foo<'a> { } ``` -Implementations need their own lifetime declarations: +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: -``` -// error, undeclared lifetime +```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, } ``` -Which are declared like this: +This is fixed by declaring impl block like this: ``` // correct impl<'a> Foo<'a> { - ... + fn foo(x: &'a str) {} +} + +struct Foo<'a> { + x: &'a str, } ``` "##,