-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Fix unsoundness in new is_power_of_two
fast path
#120546
Conversation
r? @cuviper (rustbot has picked a reviewer for you, use r? to override) |
The Miri subtree was changed cc @rust-lang/miri |
69b540e
to
0350d5d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same bug exists in library/core/src/num/int_macros.rs
. Can you fix that too while you are at it?
824521d
to
6808ec0
Compare
6808ec0
to
2b1d067
Compare
I think it is not as simple as assert_eq!(8u64.checked_pow(21), Some(1u64 << 63)); That sees |
@@ -288,6 +288,7 @@ macro_rules! int_module { | |||
assert_eq!(r.saturating_pow(2), 4 as $T); | |||
assert_eq!(r.saturating_pow(3), -8 as $T); | |||
assert_eq!(r.saturating_pow(0), 1 as $T); | |||
assert_eq!(2i64.checked_pow(64), None); // issue #120537 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are expanded for all integer types $T
, so it would be nice to keep it testing each type. In this signed case, above where r = 2 as $T
we can check exponent <$T>::BITS - 1
produces None
, and with r = -2
we can check that BITS - 1
is Some(<$T>::MIN)
and BITS
gets None
.
This is not the solution, and I don't want to go math golfing. I did want to use checked mul because of the rounding, but feared it would mess up the optimization. I'll file a revert so we can avoid having it in beta, and then figure out how yo correctly write this logic and add tests for it |
It does indeed mess up the optimization a little. While it makes the Rust code concise, LLVM does an extra comparison. It isn't the end of the world though. |
yeah, reverting the libs changes and doing them again very carefully sounds like a good idea |
fixes #120537