Skip to content

NLL: if let not detecting proper lifetime #51826

Open
@Ekleog

Description

@Ekleog

Hello,

I have recently been pointed to this example:

#![feature(nll)]

fn find_best(vec: &mut Vec<i32>) -> Option<&mut i32> {
    vec.get_mut(0)
}

fn foo(vec: &mut Vec<i32>) -> &mut i32 {
    if let Some(best) = find_best(vec) {
        best
    } else {
        &mut vec[1]
    }
}

fn main() {
    let mut x = vec![1, 2, 3];
    *foo(&mut x) = 42;
}

To me, the code looks sane, and looks like it should compile with NLL enabled (as the borrow to vec ends at the end of the if block, and shouldn't propagate to the else block), yet it looks like it doesn't.

Hope that helps, and as always thank you for your work on rustc!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-NLLArea: Non-lexical lifetimes (NLL)NLL-poloniusIssues related for using Polonius in the borrow checkerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions