Skip to content

Conversation

Copy link

Copilot AI commented Dec 23, 2025

Wrapping arithmetic intrinsics (wrapping_add, wrapping_sub, wrapping_mul) incorrectly triggered compile-time overflow errors when MIR optimization level ≥2. Example:

fn main() {
    println!("{}", 1_u32.wrapping_sub(2_u32));  // Error: arithmetic operation will overflow
}

Root Cause

LowerIntrinsics converts wrapping intrinsics to plain BinOp::{Add,Sub,Mul}. Later, KnownPanicsLint called wrapping_to_overflowing() on all plain arithmetic operations to check overflow, incorrectly flagging wrapping operations.

Changes

  • compiler/rustc_mir_transform/src/known_panics_lint.rs: Modified check_binary_op() to only check operations already marked as overflow-checking (*WithOverflow variants). Removed the blanket wrapping_to_overflowing() conversion.

  • tests/ui/mir/mir-opt-wrapping-arithmetic.rs: Added regression test covering wrapping operations with -Z mir-opt-level=2, including const evaluation.

Behavior Preserved

  • Debug mode overflow checking: Regular arithmetic uses *WithOverflow variants (still checked)
  • Const/static overflow checking: Always uses *WithOverflow variants (still checked)
  • Release mode wrapping: Plain Add/Sub/Mul correctly wraps (not an error)
  • Wrapping intrinsics: Now correctly skip overflow checks
Original prompt

Fix a compiler bug where u32::wrapping_sub (and likely other wrapping arithmetic) incorrectly triggers an overflow error when compiling with -Z mir-opt-level=2 or above.

Repro:

fn main() {
    println!("{}", 1_u32.wrapping_sub(2_u32));
}

Expected: Compiles and runs, printing 4294967295.
Actual (with -Z mir-opt-level=2): Compile-time error:

error: this arithmetic operation will overflow
...
attempt to compute `1_u32 - 2_u32`, which would overflow

Requirements:

  • Make wrapping arithmetic calls (at least wrapping_sub) not be turned into checked/overflowing arithmetic during MIR optimizations.
  • Add regression test(s) that fail before the fix and pass after.
  • Ensure no new ICEs or behavior changes for non-wrapping arithmetic.
  • The PR must be opened against the Enselic/rust repository (a fork), not upstream.
  • Do not include links to the upstream issue in the PR description or commit messages.

Notes:

  • The original report used rustc nightly 1.52.0-nightly (2021-02-18) and -Z mir-opt-level=2.
  • Likely area: MIR constant propagation / SimplifyCfg / InstCombine / ConstProp optimizations incorrectly folding wrapping_sub into a plain subtraction and then applying overflow lint/diagnostics.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 23, 2025 21:37
- Modified known_panics_lint to not check plain Add/Sub/Mul for overflow
- Only overflow-checking variants (AddWithOverflow, etc.) are now checked
- Added regression test for wrapping arithmetic with mir-opt-level=2

Co-authored-by: Enselic <115040+Enselic@users.noreply.github.com>
- Fixed comment in test (i32 -> i8)
- Enhanced comment explaining why debug mode overflow checking still works

Co-authored-by: Enselic <115040+Enselic@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix compiler bug with wrapping arithmetic overflow Fix wrapping arithmetic triggering false overflow errors in MIR optimization Dec 23, 2025
Copilot AI requested a review from Enselic December 23, 2025 21:46
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.

2 participants