-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add long error explanation for E0495 #64404
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1520,6 +1520,47 @@ where | |
``` | ||
"##, | ||
|
||
E0495: r##" | ||
A lifetime cannot be determined in the given situation. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0495 | ||
fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T { | ||
match (&t,) { // error! | ||
((u,),) => u, | ||
} | ||
} | ||
|
||
let y = Box::new((42,)); | ||
let x = transmute_lifetime(&y); | ||
``` | ||
|
||
In this code, you have two ways to solve this issue: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether this example is representative of the majority of places where users encounter this error-- if users are hitting an error due to mismatched lifetimes, IME it's rare that it's easily solvable by just adding a bound in one location. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have another example in mind by any chance? Maybe another example would be the best thing to do to avoid confusing users. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have an easy one that would be representative offhand, sorry. There are a number of things I've seen cause this error, and I don't have a good idea which is the most important to cover, nor what broader categories issues fall into. |
||
1. Enforce that `'a` lives at least as long as `'b`. | ||
2. Use the same lifetime requirement for both input and output values. | ||
|
||
So for the first solution, you can do it by replacing `'a` with `'a: 'b`: | ||
|
||
``` | ||
fn transmute_lifetime<'a: 'b, 'b, T>(t: &'a (T,)) -> &'b T { | ||
match (&t,) { // ok! | ||
((u,),) => u, | ||
} | ||
} | ||
``` | ||
|
||
In the second you can do it by simply removing `'b` so they both use `'a`: | ||
|
||
``` | ||
fn transmute_lifetime<'a, T>(t: &'a (T,)) -> &'a T { | ||
match (&t,) { // ok! | ||
((u,),) => u, | ||
} | ||
} | ||
``` | ||
"##, | ||
|
||
E0496: r##" | ||
A lifetime name is shadowing another lifetime name. Erroneous code example: | ||
|
||
|
@@ -2116,8 +2157,6 @@ rejected in your own crates. | |
E0488, // lifetime of variable does not enclose its declaration | ||
E0489, // type/lifetime parameter not in scope here | ||
E0490, // a value of type `..` is borrowed for too long | ||
E0495, // cannot infer an appropriate lifetime due to conflicting | ||
// requirements | ||
E0623, // lifetime mismatch where both parameters are anonymous regions | ||
E0628, // generators cannot have explicit parameters | ||
E0631, // type mismatch in closure arguments | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the single-field tuple necessary here? and the
match
(could it be alet
)? It seems like they're adding extra confusionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right, I was too focused on simplifying the lifetime change.