NLL Regression: Conditional control flow returning from functions no longer works · Issue #51545 · rust-lang/rust (original) (raw)
If a match expression has one arm that returns a borrowed matched value from its pattern, and another arm that doesn't borrow directly from a matched value but instead borrows the variable the matched value borrows and returns it, compilation fails if the match statement is returning from a function, but not if it's assigning to a variable. This used to work (and iirc one of the goals of NLLs was to make this pattern work), but it broke between nightly-2018-05-17
and nightly-2018-05-19
. A nightly for May 18th doesn't appear to exist.
Examples:
This doesn't work now:
// Doesn't compile on nightly-2018-05-19 and later, but does on nightly-2018-05-17. fn borrow(o: &mut Option) -> Option<&mut i32> { match o.as_mut() { Some(i) => Some(i), None => o.as_mut() } }
This is similar to the other code, but does work both before and after nightly-2018-05-19
fn main() { let mut o: Option = Some(1i32);
// Compiles everywhere!
let x = match o.as_mut() {
Some(i) => Some(i),
None => o.as_mut()
};
}