-
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
RefCell value does not live long enough in if let
#22449
Comments
if let
if let
if let
if let
if let
if let
This is not specific to |
Yes, but in this case, its a bit awkward because the problem is only arising due to our special rules for how long temporaries last for the last expression in a block. If you turn that last expression in the block (i.e. function body) above into a statement, the code will compile fine. Like so: use std::cell::RefCell;
fn main() {
let b = RefCell::new(Some(5));
if let Some(x) = b.borrow().clone() {
println!("x: {}", x);
}; // <--- note semicolon here, turning the expression into a statement.
} |
Also consider the fact that use std::cell::{RefCell, BorrowState};
fn main() {
let b = RefCell::new(Some(5));
if let Some(x) = b.borrow().clone() {
assert_eq!(b.borrow_state(), BorrowState::Reading);
};
} |
Triage: no change. |
I think I've hit a similar case to this. I don't know if it's the same bug becausue I'm new to Rust, but it seems like it, so I wanted to drop it in the bug notes.
I get:
|
(also, I'm not sure exactly what decision @Mark-Simulacrum tagged this for. Is it just about whether to close this as a duplicate? and/or confirm that it is a duplicate?) |
I think the decision tag was "is this a bug" - that is, intentional limitation/feature or incorrect checking. |
After PR #54782 lands, the error diagnostic here will be:
The reason the compiler cannot insert the semi-colon automatically: The language has a set of rules determining when temporaries are dropped. Adding the semi-colon changes the relative order of destruction, and thus as a policy that (semantically significant) change should be reflected in the source code. |
My main point is that this appears to be a duplicate of #21114 or perhaps #46413 (which should probably be themselves merged as duplicates once NLL is the default after the 2018 edition ships). The problem here is a consequence of our rules for when r-value temporaries are dropped. My current inclination is to try to improve our diagnostics and documentation so that developers understand the rules and their implications, but not actually change the rules themselves.
Thus, closing as duplicate. |
And the error is:
The text was updated successfully, but these errors were encountered: