-
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
Lifetimes: Shouldn't the compiler figure out and prolong lifetimes of temporary values where it makes sense? #49118
Comments
Hmm, I have a hunch the problem arises when the value is passed around, since there is no way to keep the values then. Still would like to see this working within the same function though. |
You're right that the compiler should be able to do this. It doesn't work on stable today due to the way AST borrowck works, but your example will happily compile on nightly with non-lexical lifetimes =). I'll add some tags so we can keep track of the issue, but I believe this is considered fixed. |
No, shadowed variables are dropped at the end of the enclosing block, as usual. Shadowing syntactically makes the old variable inaccessible, but has no semantic difference if I understand correctly. |
@bobtwinkles I believe the fix is not working for all cases. I would expect the compiler to also be able to infer that it has to extend the live of the MutexGuard for as long as there is a reference to whatever is inside: https://play.rust-lang.org/?gist=3af98ef6bf9a631eaf0185f90e8f1183&version=nightly |
@axos88 asked:
The compiler could be smart enough to do what you describe, but that does not mean that it should do so. The main argument that I have against such a change is that I want the user to be able to predict when a value will be dropped based on how the value itself is moved around, and not on how long its borrows survive. That argument's foundation rests upon my personal hypothesis: when thinking about when destructors run, it is significant easier for developers to reason solely about how values are moved around (and initialized and overwritten) than it would be for developers to also reason about what lifetimes the compiler infers for any borrows of those values.
|
Having said that, I think @nikomatsakis has at least one, and perhaps a whole series, of blog posts on this topic: http://smallcultfollowing.com/babysteps/blog/2012/09/15/rvalue-lifetimes/ http://smallcultfollowing.com/babysteps/blog/2014/01/09/rvalue-lifetimes-in-rust/ |
This was quite some time ago, so i'm not 100% sure, but as far as i remember there is no borrowing happening there, everything is moved |
We do not want to prolong the lifetimes of temporaries arbitarily. The underlying principle is that the runtime semantics of your code should not depend on borrow-check -- that is, you should not need to understand region inference to understand when a destructor will run. It is this precise principle that allows us to increase the precision of the system (e.g., do NLL) without it being a breaking change (since otherwise that could affect when destructors run, etc) There are some proposals to do better than we currently do when it comes to temporary lifetimes though. One such proposal is RFC #66, which was never fully implemented but would be backwards compatible. That RFC basically needs to be rewritten to be made actionable, I fear: I was in the midst of doing that but never finished. Anyway, the tracking issue for that is #15023. It might also be nice to revamp some of the current temporary lifetimes in non-backwards-compatible ways, specifically around the tails of expressions. There are some issues relating to some of the wacky cases that arise but I don't have time to hunt them down right now. In any case, I'm going to close this issue as the precise remedy it describes is not I think the direction we want to go. Thanks @axos88 for the bug report. =) |
Reading the book currently, and could not really grasp why it's not possible to create a cons list without Box<>, so I tried it...
The results were... not satisfactory for me... Shouldn't the compiler be smart enough and prolong the lifetimes of the temporary values in case of "error"? Or is that a not-as-easy-as-it-sounds problem?
I'm pretty sure there are other real-life scenarios where I wouldn't want to name all the intermediary values...
Btw, shouldn't the first case fail to compile as well, since the lifetime of the shadowed variable is supposed to end when it gets shadowed, doesn't it?
The text was updated successfully, but these errors were encountered: