This might be a breaking change, although it may not affect too much code.
There is always someone talking about the question that if-let and while-let lives too long.
If we focus on if clause and if-let clause, a much strange things could happen:
// Works
if x.borrow().is_negative(){ *x.borrow_mut() = 0 }else{*x.borrow_mut() = 0}
// Panics
if let false=x.borrow().is_negative(){ *x.borrow_mut() = 0 }else{*x.borrow_mut() = 0}
It seems that, temporary variables' drop times should not be so inconsistent.
I met such problem a year ago, and finally found what I want. There are a bunch of people want to drop temporary variables as soon as possible, and others want to keep temporary variables until if-let and while-let finishes. Why not add some keyword to these two different action?
current:
let x = RefCell::new(0i32);
// Succeeds
match {let tmp = x.borrow().is_negative();tmp} {
_ => { *x.borrow_mut() = 0 },
};
// Panics
match x.borrow().is_negative() {
_ => { *x.borrow_mut() = 0 },
}
// Panics, too
if let flag=x.borrow().is_negative(){ *x.borrow_mut() = 0 }
method 1: add if move / while move / match move
// `move x=expr()` equals to `let x={let tmp=x.expr();tmp}` in if-let and while-let clauses
// `match move expr()` equals to `match {let tmp=x.expr();tmp}` in match-clauses.
match move x.borrow().is_negative() {
_ => { *x.borrow_mut() = 0 },
}
if move flag=x.borrow().is_negative(){ *x.borrow_mut() = 0 }
method 2, change the default behavior of lf-let and while-let (also match), use a new keyword (e.g., ref,extend, lazy, etc.) to delay the drop execution.
if let flag=x.borrow().is_negative(){
// x.borrow() drops as soon as possible, thus calling `x.borrow_mut()` is OK.
}
if lazy x.borrow().is_negative(){
// x.borrow would only drop after the whole block is executed.
// thus, it would panic by calling a x.borrow_mut().
}
match x.borrow().is_negative(){_=>{*x.borrow_mut() = 0}}//will not panic since `x.borrow()` dropped before the execution of `x.borrow_mut()`
match lazy x.borrow().is_negative(){_=>{
// here, x.borrow() still alive.
}}
I prefer method 2 since most of the cases, the temporary variables are unnecessary for programmers, but method 2 is a breaking change. If we do not want to modify a lot, method 1 might be acceptable.
Are there any disadvantages and limitations of such modification?