Sign-extend SwitchInt targets of signed discriminants - #246
Merged
Conversation
MIR gives each SwitchInt case as the discriminant type's bit pattern zero-extended into a u128, so a negative case of a signed discriminant arrived as a large positive number: the -1 arm of an i32 match was pinned to 4294967295. The discriminant operand is modeled as its true value, so that arm was verified under a path assumption nothing satisfies, silently accepting programs that panic, while a comparison against a negative literal was rejected as unreachable — which also made i32::signum's spec unusable. An i64 discriminant hit the try_into().unwrap() instead. Fixes #132 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HLjd5vthcN4kfL1qs8D3pc
coord-e
force-pushed
the
claude/issue-132-8chwi1
branch
from
August 26, 2026 13:47
120f35e to
d2540bb
Compare
coord-e
marked this pull request as ready for review
August 26, 2026 13:48
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The distinct negative i64 regression remains untested despite the width-sensitive fix.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Corrects SwitchInt path assumptions for negative signed discriminants.
Changes:
- Sign-extends integer targets using MIR width and signedness.
- Adds pass/fail regression tests for negative
i32targets.
File summaries
| File | Description |
|---|---|
src/analyze/basic_block.rs |
Corrects signed target conversion. |
tests/ui/pass/switch_int_negative.rs |
Adds valid negative-match coverage. |
tests/ui/fail/switch_int_negative.rs |
Adds unsound-branch rejection coverage. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #132.
mir::SwitchTargets::iter()yields each case as the discriminant type's bit pattern zero-extended into au128, andtype_switch_intconverted it withval.try_into::<i64>().unwrap(). For a signed discriminant a negative case therefore arrived as a large positive number — the-1arm of ani32match was pinned to4294967295. The discriminant operand itself is modeled as its true value, so such an arm was verified under a path assumption nothing satisfies (silently accepting programs that panic), while a comparison against a negative literal was rejected as unreachable. Ani64discriminant hit thetry_into().unwrap()instead.The
Intcase now takes the width and signedness from the discriminant's MIR type (Ty::int_size_and_signed) and sign-extends the bit pattern (Size::sign_extend) before building the literal.Behavior
match x { -1 => assert!(x > 0), _ => {} }(panics at runtime)Unsatlet x: i32 = -1; assert!(x == -1)Unsata.signum(),a < 0 => s == -1Unsati64TryFromIntErrorpanicmatch x { 1 => assert!(x > 1), _ => {} }(positive control)UnsatUnsatTests
tests/ui/{pass,fail}/switch_int_negative.rs. Before the change thepassfile is reportedUnsatand thefailfile is accepted; after it, the reverse — so the pair pins both faces of the bug. Fullcargo test(330 UI tests) passes, as docargo fmt --checkandcargo clippy --all-targets.