Skip to content

fix: trap on overflow in i32::checked_div#1168

Merged
mooori merged 1 commit into
nextfrom
mooori/fix-intrinsic-i32-checked-div
Jun 11, 2026
Merged

fix: trap on overflow in i32::checked_div#1168
mooori merged 1 commit into
nextfrom
mooori/fix-intrinsic-i32-checked-div

Conversation

@mooori

@mooori mooori commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Closes #1162

Stacked on top of #1160 to have access to intrinsic testing.


The intrinsic i32::checked_div should trap on i32::MIN/-1 because the result overflows i32. It did not trap because it didn't account for this edge case.

@mooori mooori linked an issue Jun 9, 2026 that may be closed by this pull request
// TODO(#1162): remove once #1162 is fixed and handle MIN/-1 in `i32_checked_div` above
/// This test reproduces #1162
#[test]
fn i32_checked_div_min_by_neg1_i1162_reproducer() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed as the test i32_checked_div now asserts that (i32::MIN, -1) causes a FailedAssertionOverflow trap.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is one weighted branch in a randomized prop_oneof!, so a proptest run can pass without executing the exact i32::MIN / -1 case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed weights are chosen such that the probability of each case being executed is "high enough". Though your comment made me take another look as this is relevant for all tests that use NumericStrategy. I've opened #1169 to address this. WDYT?

@mooori mooori marked this pull request as ready for review June 9, 2026 15:13
@mooori mooori requested review from bitwalker and greenhat June 9, 2026 15:13

@greenhat greenhat left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Please see my comment.

// TODO(#1162): remove once #1162 is fixed and handle MIN/-1 in `i32_checked_div` above
/// This test reproduces #1162
#[test]
fn i32_checked_div_min_by_neg1_i1162_reproducer() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is one weighted branch in a randomized prop_oneof!, so a proptest run can pass without executing the exact i32::MIN / -1 case.

Base automatically changed from mooori/intrinsic-testing to next June 10, 2026 11:51
@mooori mooori merged commit 9c643dd into next Jun 11, 2026
14 checks passed
@mooori mooori deleted the mooori/fix-intrinsic-i32-checked-div branch June 11, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect edge case handling in intrinsic i32::checked_div

3 participants