Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
andersk opened this issue Apr 2, 2015 · 6 comments · Fixed by #25527
Closed
Assignees
Labels
P-medium Medium priority
Milestone

Comments

@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
Copy link
Member

pnkfelix commented Apr 2, 2015

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

@pnkfelix
Copy link
Member

pnkfelix commented Apr 2, 2015

triage: P-high (1.0)

@rust-highfive rust-highfive added the P-medium Medium priority label Apr 2, 2015
@rust-highfive rust-highfive added this to the 1.0 milestone Apr 2, 2015
@pnkfelix
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 Medium priority labels Apr 2, 2015
@pnkfelix
Copy link
Member

pnkfelix commented Apr 2, 2015

cc #23897

@pnkfelix pnkfelix added the P-medium Medium priority label Apr 2, 2015
@nrc
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
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
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.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
P-medium Medium priority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants