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

!0u32/2 gives “error: attempted to divide with overflow in a constant expression [E0020]” #23968

Closed
andersk opened this Issue Apr 2, 2015 · 6 comments

Comments

Projects
None yet
4 participants
@andersk
Copy link
Contributor

andersk commented Apr 2, 2015

println!("{}", !0u32);  // 4294967295
println!("{}", !0u32/2);  // error: attempted to divide with overflow in a constant expression [E0020]
const A: u32 = !0;
println!("{}", A/2);  // error: attempted to divide with overflow in a constant expression [E0020]
let a: u32 = !0;
println!("{}", a/2);  // 2147483647
const B: u32 = 4294967295;
println!("{}", A == B);  // true
println!("{}", B/2);  // 2147483647

This affects the test suites for the num and rand crates.

$ rustc --version
rustc 1.0.0-dev (built 2015-04-02)
$ dpkg-query -W rust-nightly
rust-nightly    201504020407~2e3b0c0~vivid
@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

triage: I-wrong, P-high (1.0)

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

triage: P-high (1.0)

@rust-highfive rust-highfive added this to the 1.0 milestone Apr 2, 2015

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

@nrc I ended up adding the 1.0 milestone by hand; isn't the above note with triage: supposed to do it?

@pnkfelix pnkfelix added I-wrong and removed P-medium labels Apr 2, 2015

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

cc #23897

@pnkfelix pnkfelix added the P-medium label Apr 2, 2015

@nrc

This comment has been minimized.

Copy link
Member

nrc commented Apr 2, 2015

@pnkfelix the log above says that highfive did add this to the 1.0 milestone - it might just have been that your local view of the webpage needed refreshing?

@pnkfelix

This comment has been minimized.

Copy link
Member

pnkfelix commented Apr 2, 2015

@nrc yeah I guess so, I don't know what happened anymore. Maybe I should have given highfive a little more time to react.

bors added a commit that referenced this issue May 17, 2015

Auto merge of #25527 - inrustwetrust:const-not-overflow, r=alexcrichton
Fixes #23968.
Since the values are stored in a u64 internally, we need to be mask away the
high bits after applying the ! operator. Otherwise, these bits will be set to
one, causing overflow.

@bors bors closed this in #25527 May 18, 2015

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