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

Can't implement Copy for C style enums with negative discriminants that overflow into unspecified positive discriminants #23221

Closed
mahkoh opened this issue Mar 9, 2015 · 8 comments · Fixed by #23863
Labels
A-FFI Area: Foreign function interface (FFI) A-type-system Area: Type system

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Mar 9, 2015

fn main() { }

#[repr(C)]
#[derive(Copy)]
enum X {
    A = -1,
    B,
}
test5.rs:7:5: 7:6 error: Discriminant overflowed!
test5.rs:7     B,
               ^
@mahkoh mahkoh changed the title Can't use derive with C style enums with negative discriminants Can't implement Copy for C style enums with negative discriminants Mar 9, 2015
@huonw
Copy link
Member

huonw commented Mar 9, 2015

Providing an explicit discriminant for the variant after the -1 works as a workaround:

fn main() { }

#[repr(C)]
#[derive(Copy)]
enum X {
    A = -1,
    B = 0,
}

@mahkoh
Copy link
Contributor Author

mahkoh commented Mar 9, 2015

The same happens without repr(C) or with repr(i32).

@pnkfelix
Copy link
Member

pnkfelix commented Mar 9, 2015

Also, -2 will work. I'll update the title.

@pnkfelix pnkfelix changed the title Can't implement Copy for C style enums with negative discriminants Can't implement Copy for C style enums with discriminant of -1 Mar 9, 2015
@mahkoh
Copy link
Contributor Author

mahkoh commented Mar 9, 2015

@pnkfelix: The error message shows that it's about overflowing discriminants. There is nothing special about -1.

@mahkoh mahkoh changed the title Can't implement Copy for C style enums with discriminant of -1 Can't implement Copy for C style enums with negative discriminants that overflow into unspecified positive discriminants Mar 9, 2015
@pnkfelix
Copy link
Member

pnkfelix commented Mar 9, 2015

@mahkoh my point is that the bug is not observed by all negative discriminants. But whatever, I'm not interested in fighting over how the title is phrased.

@mahkoh
Copy link
Contributor Author

mahkoh commented Mar 9, 2015

In any case the previous title was wrong because this works fine:

fn main() { }

//#[repr(C)]
enum _X {
    A = -1,
}

impl Copy for _X { }

@shepmaster
Copy link
Member

@pnkfelix I think that @mahkoh's point was that this usage of -2 does fail:

fn main() { }

#[derive(Copy)]
enum X {
    A = -2,
    B,
    C,
}

@pnkfelix
Copy link
Member

cc #23897

pnkfelix added a commit to pnkfelix/rust that referenced this issue Mar 31, 2015
…values.

Moved such overflow checking into one place (in `rustc::middle::ty`,
since it needs to be run on-demand during `const_eval` in some
scenarios), and revised `rustc_typeck` accordingly.

(Note that we only check for overflow if program did not provide a
discriminant value explicitly.)

Fix rust-lang#23030

Fix rust-lang#23221

Fix rust-lang#23235
pnkfelix added a commit to pnkfelix/rust that referenced this issue Apr 1, 2015
…values.

Moved such overflow checking into one place (in `rustc::middle::ty`,
since it needs to be run on-demand during `const_eval` in some
scenarios), and revised `rustc_typeck` accordingly.

(Note that we only check for overflow if program did not provide a
discriminant value explicitly.)

Fix rust-lang#23030

Fix rust-lang#23221

Fix rust-lang#23235
alexcrichton added a commit to alexcrichton/rust that referenced this issue Apr 1, 2015
const_eval : add overflow-checking for {`+`, `-`, `*`, `/`, `<<`, `>>`}.

One tricky detail here: There is some duplication of labor between `rustc::middle::const_eval` and `rustc_trans::trans::consts`. It might be good to explore ways to try to factor out the common structure to the two passes (by abstracting over the particular value-representation used in the compile-time interpreter).

----

Update: Rebased atop rust-lang#23841

Fix rust-lang#22531

Fix rust-lang#23030

Fix rust-lang#23221

Fix rust-lang#23235
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-FFI Area: Foreign function interface (FFI) A-type-system Area: Type system
Projects
None yet
5 participants