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

Missed niche opportunity in simple nested enum #64335

Closed
glandium opened this issue Sep 10, 2019 · 1 comment

Comments

@glandium
Copy link
Contributor

commented Sep 10, 2019

pub enum Foo {
    A,
    B,
    C,
}
pub enum Bar {
    Foo(Foo),
    D,
    E,
}
pub enum Foo2<T> {
    A(T),
    B(T),
    C(T),
}
pub enum Bar2<T> {
    Foo(Foo2<T>),
    D(T),
    E(T),
}

fn main() {
    use std::mem::size_of;
    dbg!(size_of::<Bar>()); // = 1
    dbg!(size_of::<Bar2<()>>()); // = 1
    dbg!(size_of::<Bar2<u8>>()); // = 3
    unsafe {
        dbg!(transmute::<Bar2<()>, u8>(Bar2::Foo(Foo2::A(())))); // = 0
        dbg!(transmute::<Bar2<()>, u8>(Bar2::Foo(Foo2::B(())))); // = 1
        dbg!(transmute::<Bar2<()>, u8>(Bar2::Foo(Foo2::C(())))); // = 2
        dbg!(transmute::<Bar2<()>, u8>(Bar2::D(()))); // = 3
        dbg!(transmute::<Bar2<()>, u8>(Bar2::E(()))); // = 4
    }
}

So the compiler does know to "merge" the enum discriminants in the simple cases, and it seems to me it could do the same for the slightly less simple case above: {0, u8}, {1, u8}, {2, u8} could be both the representation of a Foo2<u8> and of a Bar2::Foo(Foo<u8>)

@moshg

This comment has been minimized.

Copy link

commented Sep 10, 2019

This seems to be a duplication of #46213

@Centril Centril closed this Sep 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.