Closed
Description
When trying the following code (playground), the result error message is incredibly unhelpful and confusing:
Code sample
#[derive(Clone)]
enum Foo<'a> {
Bar(&'a str),
}
impl<'a> Foo<'a> {
fn bar(&self, other: Foo) -> Foo {
match *self {
Foo::Bar(s) => {
if s == "test" {
other
} else {
self.clone()
}
}
}
}
}
Error message
error[E0308]: mismatched types
--> src/main.rs:11:21
|
11 | other
| ^^^^^ lifetime mismatch
|
= note: expected type `Foo<'_>`
found type `Foo<'_>`
note: the anonymous lifetime #2 defined on the method body at 7:5...
--> src/main.rs:7:5
|
7 | / fn bar(&self, other: Foo) -> Foo {
8 | | match *self {
9 | | Foo::Bar(s) => {
10 | | if s == "test" {
... |
16 | | }
17 | | }
| |_____^
note: ...does not necessarily outlive the anonymous lifetime #1 defined on the method body at 7:5
--> src/main.rs:7:5
|
7 | / fn bar(&self, other: Foo) -> Foo {
8 | | match *self {
9 | | Foo::Bar(s) => {
10 | | if s == "test" {
... |
16 | | }
17 | | }
| |_____^
The expected and found type are both displayed as Foo<'_>
, although I guess this is expected as the two '_
refer to different anonymous lifetimes. But then, it doesn't help that the spans for those two anonymous lifetimes are (visually) identical.