Skip to content

borrowck cares about field names with disjoint enum downcasts #27889

Closed
@arielb1

Description

@arielb1

STR

This compiles:

pub enum Foo {
    X { foo: u32 },
    Y { bar: u32 }
}

pub fn foo(mut x: Foo) {
    let mut y = None;
    let mut z = None;
    if let Foo::X { ref foo } = x {
        z = Some(foo);
    }
    if let Foo::Y { ref mut bar } = x {
        y = Some(bar);
    }
    drop((y, z));
}

fn main() {}

If you change the fields to have identical names, it doesn't:

pub enum Foo {
    X { foo: u32 },
    Y { foo: u32 }
}

pub fn foo(mut x: Foo) {
    let mut y = None;
    let mut z = None;
    if let Foo::X { ref foo } = x {
        z = Some(foo); //~ NOTE previous borrow of `x.foo` occurs here
    }
    if let Foo::Y { ref mut foo } = x {
        y = Some(foo); //~ ERROR cannot borrow `x.foo` as mutable
    }
    drop((y, z));
}

fn main() {}

Expected

Field names aren't supposed to be significant between different variants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions