Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

borrowck cares about field names with disjoint enum downcasts #27889

Closed
arielb1 opened this issue Aug 18, 2015 · 2 comments
Closed

borrowck cares about field names with disjoint enum downcasts #27889

arielb1 opened this issue Aug 18, 2015 · 2 comments
Labels
A-borrow-checker Area: The borrow checker

Comments

@arielb1
Copy link
Contributor

arielb1 commented Aug 18, 2015

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.

@pnkfelix
Copy link
Member

that is amusing

bors added a commit that referenced this issue Dec 12, 2015
Fix for issue #27889: same field names in enum variants
@bluss
Copy link
Member

bluss commented Aug 22, 2016

Fixed in #27929, which includes a test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker
Projects
None yet
Development

No branches or pull requests

3 participants