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

enum discriminant should be picked to make enum NonZero #43537

Closed
RalfJung opened this issue Jul 29, 2017 · 2 comments
Closed

enum discriminant should be picked to make enum NonZero #43537

RalfJung opened this issue Jul 29, 2017 · 2 comments

Comments

@RalfJung
Copy link
Member

I would expect the following program to demonstrate that Test is NonZero:

#![allow(dead_code)]
use std::mem;

enum Test { A(i32), B(i32) }

fn main() {
    println!("{} {}", mem::size_of::<Test>(), mem::size_of::<Option<Test>>());
}

The reasoning is that Test itself can never use the NonZero discriminant optimization itself, so it should use discriminants 1 and 2 to guarantee that it is itself never all-zero. This should be backwards-compatible as AFAIK safe code cannot observe, and unsafe code is not allowed to rely on, the concrete values of the discriminants.

(Not sure if an issue is enough for this or if it needs an RFC, so I started with the simpler-to-do one. ;)

@kennytm
Copy link
Member

kennytm commented Jul 29, 2017

I don't think it should use the NonZero trick, but "compress nested enum variant IDs" i.e.

    Option<Test>
==  enum {
        Some(enum {
            A(i32),   // <- discriminant = 0
            B(i32),   // <- discriminant = 1
        }),
        None,  // <- discriminant = 2
    }

@eddyb
Copy link
Member

eddyb commented Jul 29, 2017

Closing as duplicate of rust-lang/rfcs#1230.

@eddyb eddyb marked this as a duplicate of rust-lang/rfcs#1230 Jul 29, 2017
@eddyb eddyb closed this as completed Jul 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants