Skip to content

Idea: change the desugaring of if let to improve borrow checking #2068

@mark-i-m

Description

@mark-i-m

Related: rust-lang/rust#28449

Currently if let desugars to a match with guards. Using the example from the related post:

let mut f = Some(1);
if let Some(a) = f {
    bar();
} else if f.as_mut().is_some() {
    baz();
}

Desugars to

match f {
    Some(a) => {bar();}
    _ if f.as_mut().is_some() {baz();}
    _ => {}
}

However, this leads to f being borrowed in the else if which is truly annoying. Instead, I propose the following desugaring:

    if match f {Some(_) => true, _ => false } {
         let Some(a) = f;
         { bar(); } 
    } else if f.as_mut().is_some() {
        baz();
    }

Which (I believe) does not suffer from the same problem.

One unanswered question is: does non-lexical lifetimes solve this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions