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

Should C-style enums silently overflow? #23030

Closed
pnkfelix opened this issue Mar 4, 2015 · 8 comments · Fixed by #23863
Closed

Should C-style enums silently overflow? #23030

pnkfelix opened this issue Mar 4, 2015 · 8 comments · Fixed by #23863
Assignees
Milestone

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Mar 4, 2015

Spawned off of a note from PR #22532 on commit 1246d40

The following program:

#[repr(usize)]
enum Foo {
    A = 0xffffffffffffffff_usize,
    B,
    C
}

fn main() {
    println!("Hello World: {:x}", Foo::A as usize);
    println!("Hello World: {:x}", Foo::B as usize);
    println!("Hello World: {:x}", Foo::C as usize);
}

compiles and prints:

Hello World: ffffffffffffffff
Hello World: 0
Hello World: 1

My personal preference would be for a compiler error due to the implicit overflow, with a note saying that the programmer should explicitly assign some value to Foo::B. (And thus they can opt into the current behavior by assigning it B = 0,; then Foo::C would continue to be assigned 1 as today.)

@pnkfelix
Copy link
Member Author

pnkfelix commented Mar 4, 2015

triage: I-nominated

(I suggest P-backcompat-lang, I-needs-decision.)

@rust-highfive rust-highfive added this to the 1.0 beta milestone Mar 4, 2015
@pnkfelix
Copy link
Member Author

pnkfelix commented Mar 4, 2015

@nrc is it by design that highfive responded to the above comment by assigning the 1.0 beta milestone?

@pnkfelix pnkfelix removed this from the 1.0 beta milestone Mar 4, 2015
@nrc
Copy link
Member

nrc commented Mar 4, 2015

Hmm, no it is not, I guess it tried to find the milestone "I suggest P-backcompat-lang, I-needs-decision.", but I have no idea how it ended up with "1.0 beta". I'll look into it (also, I'll only check inside parens on the same line

@pnkfelix
Copy link
Member Author

pnkfelix commented Mar 5, 2015

1.0 beta, P-backcompat-lang.

@pnkfelix pnkfelix added this to the 1.0 beta milestone Mar 5, 2015
@pnkfelix pnkfelix self-assigned this Mar 5, 2015
@theemathas
Copy link
Contributor

Is this the same as #23235?

@pnkfelix
Copy link
Member Author

@theemathas I'd say they are duplicates, except that this bug (I think) is a little more general.

@pnkfelix
Copy link
Member Author

So, we do check for overflow in one case, namely for an overflow from u64::MAX wrapping around to 0_u64.

(This ends up being treated as equivalent to -1_i64 being incremented to 0_i64; see also #23221.)

That check is done here in ty.rs

I'm going to try to generalize it to handle the specific repr's properly. (That will hopefully help fix #23221.)

@pnkfelix
Copy link
Member Author

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
None yet
Projects
None yet
4 participants