-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Unhelpful error message for E0308 #44684
Comments
The presented code is equivalent to #[derive(Clone)]
enum Foo<'a> {
Bar(&'a str),
}
impl<'a> Foo<'a> {
fn bar(&self, other: Foo) -> Foo<'a> {
match *self {
Foo::Bar(s) => {
if s == "test" {
other
} else {
self.clone()
}
}
}
}
} which fails with a similar error, but slightly clearer:
The proper code is #[derive(Clone)]
enum Foo<'a> {
Bar(&'a str),
}
impl<'a> Foo<'a> {
fn bar(&self, other: Foo<'a>) -> Foo<'a> {
// ^^^^ note the explicit lifetime here
match *self {
Foo::Bar(s) => {
if s == "test" {
other
} else {
self.clone()
}
}
}
}
} Once you specify a lifetime for We do need to improve the output of this error message. I think this case falls under the same situation as #42703. Once the PR fixing that lands, I'll verify wether it covers this case. |
Current output for the original code is much improved:
A suggestion to specify a lifetime for the argument is still missing (as the PR handles borrows, not ADTs with borrows), and gives the old diagnostic when the method has the following signature: fn bar(&self, other: Foo) -> Foo<'a> That being said, at least in this case it makes a bit more sense, as there's a reference to
|
Handle anon lifetime arg being returned with named lifetime return type When there's a lifetime mismatch between an argument with an anonymous lifetime being returned in a method with a return type that has a named lifetime, show specialized lifetime error pointing at argument with a hint to give it an explicit lifetime matching the return type. ``` error[E0621]: explicit lifetime required in the type of `other` --> file2.rs:21:21 | 17 | fn bar(&self, other: Foo) -> Foo<'a> { | ----- consider changing the type of `other` to `Foo<'a>` ... 21 | other | ^^^^^ lifetime `'a` required ``` Follow up to rust-lang#44124 and rust-lang#42669. Fix rust-lang#44684.
When trying the following code (playground), the result error message is incredibly unhelpful and confusing:
Code sample
Error message
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.The text was updated successfully, but these errors were encountered: