Skip to content

Commit fd35e82

Browse files
committed
JumpThreading: fix bitwise Not on non-booleans
1 parent 417b55b commit fd35e82

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

Diff for: compiler/rustc_middle/src/ty/consts/int.rs

+5
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,11 @@ impl ScalarInt {
398398
pub fn to_f128(self) -> Quad {
399399
self.to_float()
400400
}
401+
402+
#[inline]
403+
pub fn bitwise_not(self) -> Self {
404+
Self { data: self.size().truncate(!self.data), size: self.size }
405+
}
401406
}
402407

403408
macro_rules! from_x_for_scalar_int {

Diff for: compiler/rustc_mir_transform/src/jump_threading.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ impl Condition {
150150
}
151151

152152
fn inv(mut self) -> Self {
153-
self.polarity = match self.polarity {
154-
Polarity::Eq => Polarity::Ne,
155-
Polarity::Ne => Polarity::Eq,
156-
};
153+
self.value = self.value.bitwise_not();
154+
157155
self
158156
}
159157
}

Diff for: tests/mir-opt/jump_threading.not_int.JumpThreading.panic-abort.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
StorageDead(_4);
2323
_2 = Eq(move _3, const 0_i32);
2424
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25-
+ goto -> bb1;
25+
+ goto -> bb2;
2626
}
2727

2828
bb1: {

Diff for: tests/mir-opt/jump_threading.not_int.JumpThreading.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
StorageDead(_4);
2323
_2 = Eq(move _3, const 0_i32);
2424
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25-
+ goto -> bb1;
25+
+ goto -> bb2;
2626
}
2727

2828
bb1: {

Diff for: tests/mir-opt/jump_threading.rs

+6
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,13 @@ fn floats() -> u32 {
533533

534534
fn not_int() -> i32 {
535535
// CHECK-LABEL: fn not_int(
536+
// CHECK: goto -> bb2
536537

538+
// CHECK-LABEL: bb1: {
539+
// _0 = const 1_i32;
540+
541+
// CHECK-LABEL: bb2: {
542+
// _0 = const 0_i32;
537543
// Test for issue #131195, where !a == b is assumed to be equivalent to a != b
538544
// This is only the case for bools
539545
let a = 1;

Diff for: tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir

+11-8
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
3333
StorageLive(_4);
3434
StorageLive(_3);
3535
_3 = Gt(copy _2, const 65535_usize);
36-
switchInt(move _3) -> [0: bb1, otherwise: bb5];
36+
switchInt(move _3) -> [0: bb1, otherwise: bb6];
3737
}
3838

3939
bb1: {
@@ -54,27 +54,30 @@ fn step_forward(_1: u16, _2: usize) -> u16 {
5454
bb3: {
5555
StorageDead(_5);
5656
StorageDead(_6);
57-
StorageDead(_7);
58-
goto -> bb7;
57+
goto -> bb5;
5958
}
6059

6160
bb4: {
6261
StorageDead(_5);
6362
StorageDead(_6);
64-
StorageDead(_7);
65-
goto -> bb6;
63+
goto -> bb5;
6664
}
6765

6866
bb5: {
69-
StorageDead(_3);
70-
goto -> bb6;
67+
StorageDead(_7);
68+
goto -> bb7;
7169
}
7270

7371
bb6: {
74-
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb7, unwind continue];
72+
StorageDead(_3);
73+
goto -> bb7;
7574
}
7675

7776
bb7: {
77+
assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::<impl u16>::MAX, const 1_u16) -> [success: bb8, unwind continue];
78+
}
79+
80+
bb8: {
7881
StorageLive(_8);
7982
_8 = copy _2 as u16 (IntToInt);
8083
_0 = Add(copy _1, copy _8);

0 commit comments

Comments
 (0)