Skip to content

NLL Regression: Conditional control flow returning from functions no longer works #51545

Open
@Osspial

Description

@Osspial

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<i32>) -> 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<i32> = Some(1i32);

    // Compiles everywhere!
    let x = match o.as_mut() {
        Some(i) => Some(i),
        None => o.as_mut()
    };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-poloniusCompiling with `-Zpolonius` fixes this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions