-
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
Implicit return leads to "does not live long enough" error. #31439
Comments
This does seem like a bug. I can't quite remember, but I think there is a similar issue somewhere... |
This looks like a destruction order problem to me. |
Assigning the result to a local and then implicitly returning that local also solves this problem: fn lines() -> Vec<String> {
let stdin = io::stdin();
let handle = stdin.lock();
let result = handle.lines().map(|line| line.unwrap()).collect();
result
} Perhaps rustc sometimes incorrectly requires too many lifetimes in an implicit return expression to be valid until the end of the function, instead of looking only at the lifetimes mentioned in the result of the expression? |
Here, a minimal example: struct S{ n: u8 }
struct T<'a>{ s: &'a S }
impl<'a> Drop for T<'a> { fn drop(&mut self){} }
fn test_good() -> u8 { // this is fine
let s = S{ n: 5 };
return T{ s: &s }.s.n;
}
fn test_bad() -> u8 { // this is not
let s = S{ n: 5 };
T{ s: &s }.s.n
}
fn main(){} |
Seems related to #22252 |
This looks like a duplicate of #33490 - on MIR it would be a segfault. |
Here is a simple function that tries to read all lines from input:
As shown here, attempting to compile it (on stable 1.6, beta or nightly) gives a lifetime error:
Making the return explicit fixes this:
Is this the expected behavior? I would have thought explicit and implicit returns would give the same result...
The text was updated successfully, but these errors were encountered: