-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
tracking issue for lifetime inference error work (E0495) #42516
Comments
Adding diagnostic code 0611 for lifetime errors with one named, one anonymous lifetime parameter This is a fix for rust-lang#42517 Note that this only handles the above case for **function declarations** and **traits**. `impl items` and `closures` will be handled in a later PR. Example ``` fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { if x > y { x } else { y } } ``` now displays the following error message. ui tests have been added for the same. ``` error[E0611]: explicit lifetime required in the type of `x` 11 | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { | ^ consider changing the type of `x` to `&'a i32` 12 | if x > y { x } else { y } | - lifetime `'a` required ``` rust-lang#42516 r? @nikomatsakis
Adding diagnostic code 0611 for lifetime errors with one named, one anonymous lifetime parameter This is a fix for rust-lang#42517 Note that this only handles the above case for **function declarations** and **traits**. `impl items` and `closures` will be handled in a later PR. Example ``` fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { if x > y { x } else { y } } ``` now displays the following error message. ui tests have been added for the same. ``` error[E0611]: explicit lifetime required in the type of `x` 11 | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { | ^ consider changing the type of `x` to `&'a i32` 12 | if x > y { x } else { y } | - lifetime `'a` required ``` rust-lang#42516 r? @nikomatsakis
Adding diagnostic code 0611 for lifetime errors with one named, one anonymous lifetime parameter This is a fix for rust-lang#42517 Note that this only handles the above case for **function declarations** and **traits**. `impl items` and `closures` will be handled in a later PR. Example ``` fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { if x > y { x } else { y } } ``` now displays the following error message. ui tests have been added for the same. ``` error[E0611]: explicit lifetime required in the type of `x` 11 | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { | ^ consider changing the type of `x` to `&'a i32` 12 | if x > y { x } else { y } | - lifetime `'a` required ``` rust-lang#42516 r? @nikomatsakis
Adding diagnostic code 0611 for lifetime errors with one named, one anonymous lifetime parameter This is a fix for rust-lang#42517 Note that this only handles the above case for **function declarations** and **traits**. `impl items` and `closures` will be handled in a later PR. Example ``` fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { if x > y { x } else { y } } ``` now displays the following error message. ui tests have been added for the same. ``` error[E0611]: explicit lifetime required in the type of `x` 11 | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { | ^ consider changing the type of `x` to `&'a i32` 12 | if x > y { x } else { y } | - lifetime `'a` required ``` rust-lang#42516 r? @nikomatsakis
Adding diagnostic code 0611 for lifetime errors with one named, one anonymous lifetime parameter This is a fix for rust-lang#42517 Note that this only handles the above case for **function declarations** and **traits**. `impl items` and `closures` will be handled in a later PR. Example ``` fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { if x > y { x } else { y } } ``` now displays the following error message. ui tests have been added for the same. ``` error[E0611]: explicit lifetime required in the type of `x` 11 | fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 { | ^ consider changing the type of `x` to `&'a i32` 12 | if x > y { x } else { y } | - lifetime `'a` required ``` rust-lang#42516 r? @nikomatsakis
I'd like to take at least one of these and help! |
I was referred here for a comment I made about how lifetime errors involving complex elision should print the fully elided parameters. By complex elision I mean elision that doesn't just result in the same lifetime for every param, so that's elision of some parameters when you have other parameters like
and
|
@Vurich it'd be good to see an example of a case where you think that'd be useful. We've been trying so far to make more targeted suggestions -- see the updated game plan above -- but I could see that it might be useful to show the "fully inferred" function definition in some cases. |
@nikomatsakis I think Vurich's original example was the following (taken from his comment) struct Bar<'a, T: 'a>(&'a mut T);
struct Foo<'a, T: 'a>(&'a mut Bar<'a, T>);
fn make_foo<'a, T>(m: &'a mut Bar<T>) -> Foo<'a, T> {
Foo(m)
} When I try to compile this, the error message already mentions an anonymous lifetime:
I think this would be the perfect place to show the fully inferred function definition ("
|
I had a problem similar to @Vurich 's example when I did my first experiments with trait objects. A simplified version of the code is the following: trait T {}
struct TT<'a> { x: &'a i8 }
impl<'a> T for TT<'a> {}
struct S { data: Vec<Box<T>> }
impl S {
fn new() -> Self { S{data: Vec::new()} }
fn push(&mut self, t: Box<T>) { self.data.push(t); }
}
fn boxed_trait_object() {
let i = 4;
let t = TT{x: &i};
let mut s = S::new();
s.push(Box::new(t)); // Error: `i` does not live long enough
// Note: borrowed value must be valid
// for the static lifetime...
} I started searching my code for the The only solution I can think of to make it clearer what happens in situations like this is to warn on default lifetimes for trait objects. I know that RFC rust-lang/rfcs#2115 proposes to warn on leaving off lifetime parameters on structs. Is a similar warning planned for leaving off lifetime bounds on trait objects? |
Thanks for working on this :) I think the lifetime errors are the biggest usability hurdle with Rust. /me subscribes. |
It would be good to try to provide a catalogue/summary of the different cases here, and perhaps check how each one changes, if at all, when you switch on "full" NLL mode (as opposed to the NLL migrate mode that is the current default in nightly for all editions). (One switches on "full" NLL via |
triage: realistically we are not (and cannot) prioritize this effort (beyond what we have already done that is). Downgrading from P-high to P-medium. |
Discussed at T-compiler backlog bonanza. We think C-tracking-issue is meant to track issues with some concrete feature or deliverable. This issue seems like its tracking something more abstract, a grab bag of diagnostic changes all related to lifetime inference. The listed work does look like something that should be done, but we don't think there's benefit in leaving this issue open, because there will always be ways to improve lifetime diagnostics, so its not clear what criteria we would use for saying that this is "done" |
This is a general tracking issue for the work on improving lifetime errors (in particular the "cannot infer lifetime" error E0495, but also including other errors). This is a rather large project, since there are many different classes of errors, so part of the work is finding scenarios and making more targeted error messages for them.
In general, I am trying to track errors and brainstorm here. There is also an etherpad where I have done some note-taking.
Current road-map
The plan is to have three major kinds of errors, delivered in order of preference. The basic work here is done, but there remain gaps to be covered. If you're interested in helping out, check out the issues associated with the unchecked check boxes!
self
very wellSubSup
-style errors and not just concrete failures (Improve case with one named, one anonymous lifetime parameter - SubSupConflict Errors #42701)Examples in need of analysis
&'static
argumentsFuture plans
foo(a)
puts the formal declarations offoo
in tension with the type ofa
). I'll try to write up these thoughts in a bit more detail later.Other issues
Here are various issues that I have subsumed into this one. These are a checklist because I want to go over them and extract any key examples into this issue and then close them. Feel free to do so (in the comments).
The text was updated successfully, but these errors were encountered: