Skip to content

Commit d77db2e

Browse files
authored
Rollup merge of rust-lang#57950 - QuietMisdreavus:lifetime-err-desc, 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
2 parents 3fe8b4c + a2b75ed commit d77db2e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/librustc/diagnostics.rs

+33
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ struct Foo1 { x: &bool }
362362
// ^ expected lifetime parameter
363363
struct Foo2<'a> { x: &'a bool } // correct
364364
365+
impl Foo2 {}
366+
// ^^^^ expected lifetime parameter
367+
impl<'a> Foo2<'a> {} // correct
368+
365369
struct Bar1 { x: Foo2 }
366370
// ^^^^ expected lifetime parameter
367371
struct Bar2<'a> { x: Foo2<'a> } // correct
@@ -766,11 +770,40 @@ struct Foo {
766770
These can be fixed by declaring lifetime parameters:
767771
768772
```
773+
struct Foo<'a> {
774+
x: &'a str,
775+
}
776+
769777
fn foo<'a>(x: &'a str) {}
778+
```
770779
780+
Impl blocks declare lifetime parameters separately. You need to add lifetime
781+
parameters to an impl block if you're implementing a type that has a lifetime
782+
parameter of its own.
783+
For example:
784+
785+
```compile_fail,E0261
771786
struct Foo<'a> {
772787
x: &'a str,
773788
}
789+
790+
// error, use of undeclared lifetime name `'a`
791+
impl Foo<'a> {
792+
fn foo<'a>(x: &'a str) {}
793+
}
794+
```
795+
796+
This is fixed by declaring the impl block like this:
797+
798+
```
799+
struct Foo<'a> {
800+
x: &'a str,
801+
}
802+
803+
// correct
804+
impl<'a> Foo<'a> {
805+
fn foo(x: &'a str) {}
806+
}
774807
```
775808
"##,
776809

0 commit comments

Comments
 (0)