Skip to content

if let should pre-drop wrt else #103108

Closed
Closed
@est31

Description

@est31

Consider this snippet:

struct Foo<'a>(&'a mut u32);

impl<'a> Drop for Foo<'a> {
    fn drop(&mut self) {
        *self.0 = 0;
    }
}

fn main() {
    let mut foo = 0;
    
    if let Foo(0) = Foo(&mut foo) { // Doesn't compile
    } else {
        *&mut foo = 1;
    }
    
    if matches!(Foo(&mut foo), Foo(0)) { // Compiles
    } else {
        *&mut foo = 1;
    }
}

Here, if let complains about an access of foo in the else branch, while if is okay with this.

This is due to if let dropping its temporaries after the else block. This is not neccessary, it could also drop them before.

Similar to #103107

cc @Nilstrieb

@rustbot label T-lang

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-if_let_guard`#![feature(if_let_guard)]`F-let_chains`#![feature(let_chains)]`T-langRelevant to the language 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