Skip to content

Commit c09e84d

Browse files
committed
handle negative integers in patterns
1 parent 663df28 commit c09e84d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,13 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
758758
rustc_middle::thir::ExprKind::Adt(value_adt) => scope
759759
.match_arms
760760
.target_for_value(u128::from(value_adt.variant_index.as_u32())),
761-
rustc_middle::thir::ExprKind::Literal { lit, neg: _ } => match lit.node {
762-
LitKind::Int(pu, _) => scope.match_arms.target_for_value(pu.get()),
761+
rustc_middle::thir::ExprKind::Literal { lit, neg } => match lit.node {
762+
LitKind::Int(pu, _) => {
763+
// apply the sign
764+
let raw = pu.get() as i128;
765+
let signed = if *neg { -raw } else { raw };
766+
scope.match_arms.target_for_value(recast as u128)
767+
}
763768
_ => todo!(),
764769
},
765770
other => todo!("{other:?}"),

tests/ui/match/loop-match-integer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
'a: loop {
99
state = 'blk: {
1010
match state {
11-
1 => {
11+
-1 => {
1212
if true {
1313
#[const_continue]
1414
break 'blk 2;
@@ -20,7 +20,7 @@ fn main() {
2020
}
2121
0 => {
2222
#[const_continue]
23-
break 'blk 1;
23+
break 'blk -1;
2424
}
2525
2 => break 'a,
2626
_ => break 'a,

0 commit comments

Comments
 (0)