Commit 270f75b
authored
Rollup merge of #158314 - yilin0518:fix-unchecked_div_exact, r=jhpratt
Fix incorrect unsafe debug assertion in unchecked_div_exact
This PR fixes a bug in the `assert_unsafe_precondition!` check for the unstable `unchecked_div_exact` method in signed integers (`int_macros.rs`).
### What is the problem?
Currently, the precondition check for signed integers asserts that `rhs > 0`. This causes a panic in debug builds when performing a perfectly valid exact division with a negative divisor (e.g., `-10i32 / -2i32`).
The following code snippet will panic in debug mode, but successfully build within release mode:
```rust
#![feature(exact_div)]
fn main() {
let lhs: i32 = -10;
let rhs: i32 = -2;
unsafe {
let result = lhs.unchecked_div_exact(rhs);
}
}
```
Furthermore, this erroneous `rhs > 0` check renders the overflow prevention logic `(lhs != <$SelfT>::MIN || rhs != -1)` completely unreachable (dead code), since `rhs` could never be `-1` if it is strictly greater than `0`.
It appears that it should be `rhs != 0`.
### What does this PR do?
- Changes `rhs > 0` to `rhs != 0` in `library/core/src/num/int_macros.rs` to allow valid negative divisors.
Tracking issue: #1399111 file changed
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1181 | 1181 | | |
1182 | 1182 | | |
1183 | 1183 | | |
1184 | | - | |
| 1184 | + | |
1185 | 1185 | | |
1186 | 1186 | | |
1187 | 1187 | | |
| |||
0 commit comments