-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Rust unable to compare two numbers in --release mode (miscompile?) #141021
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
Comments
Your Presumably, this isn't intended, and you wanted to write |
The part where equal numbers are compared as unequal is probably a variant of #107975 |
I've simplified the code a bit more to: struct Unknown {}
impl Unknown {
fn id(&self) -> u64 {
&self as *const _ as u64
}
}
fn main() {
let u = Unknown {};
let (a, b) = (u.id(), u.id());
assert_eq!(a, b, "difference is: {} {}", a-b, b-a);
} Yeah, you're right. Removing the I'm not fully fluent in Rust undefined behaviour, but is my code (the original code) actually wrong? There's no warning, and it's not making sense. Edit: well, of course it's wrong, as you point out. But should Rust allow this nonsense code to compile and do the wrong thing? |
The original code doesn't have undefined behavior (although it does have a bug). Compiler warnings can't catch everything. The strange behavior where equal numbers compare as unequal is a known rustc bug (unrelated to the bug in your code) which can occur when the addresses of two dangling pointers are compared with each other. |
This code is only nonsense if you know what the programmer intent was. A lint in clippy for observing the address of locals might make sense, but I suspect the rate at which it catches bugs may not be very high as opposed to warning about weird but correct code. |
Fair enough. But I maintain that having two integer variables with the same value, whose difference is zero, simply must compare equal. Otherwise, how far does this go? If I This sounds like C++ UB, which I don't think is great. |
Yes, that part is a known rustc bug. |
@ThomasHabets if you're curious as to why this sort of compiler bug could even happen in the first place, I find this to be a good introduction. |
@pierzchalski thanks, very interesting article. I think it misses an important core point though: The example code is UB, right? So a lot of the discussion is about how one should correctly compile & optimize UB input? For C/C++ I think the ship has sailed, but for Rust I would not expect to trigger this without I'm glad this is recognized as a bug in rustc, and I guess I should follow #107975 for that? |
The example code does not have UB. It has a miscompilation. (UB means that the code is incorrect in such a way that a non-buggy compiler is allowed to compile the code into whatever it feels like.) Yes, if/when this bug ever gets fixed, #107975 will be updated. |
Oh, you may be right. A surprising (to me) allowance in the spec that you can compare pointers one (and only one) element past the end of an array against the (potentially) following object.
Thanks for the correction. |
In release mode
assert_eq!()
fails on two numbers that in fact are the same.I tried this code
I have a case where I create two entangled particles. They have some shared
Arc
unknown state. If you ask the state, or each particle for theirid
, they will all reply with the same number, generated by the entangled state.I expected to see this happen
The
assert_eq!
to succeed, since the numbers are definitely the same.Instead, this happened
Works in debug build, asserts in release build:
These numbers are the same. I was going crazy for a bit. I even changed the code to:
and then I get:
Note
the difference is: 0
.Meta
After a
rustup update
, problem remains:The text was updated successfully, but these errors were encountered: