-
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
Confusing error involving lifetime elision #79879
Comments
Correct, you only need a backtrace if the compiler crashes. I don't know enough about lifetimes to know whether this should work, but the diagnostics are definitely weird. Here's a more minimal example: struct A<'a> {
backref: Option<&'a mut A<'a>>,
}
fn child_with_value<'a>(this: &'a mut A<'a>) -> A<'a> {
A {
backref: Some(this),
}
}
fn errors(a: &mut A) {
let mut b = child_with_value(a);
} Errors:
|
It seems the reason my minimal example fails to compile is that the compiler is not able to infer the lifetimes properly. This fixes my minimal example: struct A<'a> {
backref: Option<&'a mut A<'a>>,
}
fn child_with_value<'a>(this: &'a mut A<'a>) -> A<'a> {
A {
backref: Some(this),
}
}
fn errors<'a>(a: &'a mut A<'a>) {
let mut b = child_with_value(a);
} |
However, if I add explicit lifetimes to your original code, the compiler produces this error:
Now that I see the error, I think your code is actually incorrect. (Correct me if I'm wrong on that!) The diagnostics still need to be fixed though. |
Yes my apologies, my code is incorrect - the problem is the incorrect message :) |
@mbartlett21 That example appears to trigger a different issue. Please open a new issue so we can discuss it separately. |
This seems like it would be a good candidate to apply the language of #90179 to. Here's what I picture:
I think the key point that wasn't addressed in that PR is that there's an elided lifetime parameter on cc @Nilstrieb |
sounds like a good idea, I'll look into that |
Current output:
|
The following code:
Gives this error:
I expected to see this happen:
Given that the invocation should be legal, I expect this to compile without issues :)
Instead, this happened: Compiler error above. Note that the variable flows into itself, which seems strange
Meta
Tested both on stable and nightly:
rustc --version --verbose
:Backtrace not applicable I think?
The text was updated successfully, but these errors were encountered: