Skip to content

Commit c0184c1

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

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

compiler/rustc_mir_build/src/builder/scope.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,16 @@ 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(n, _) => {
763+
let n = if *neg {
764+
(n.get() as i128).overflowing_neg().0 as u128
765+
} else {
766+
n.get()
767+
};
768+
let result = state_ty.primitive_size(self.tcx).truncate(n);
769+
scope.match_arms.target_for_value(result)
770+
}
763771
_ => todo!(),
764772
},
765773
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)