Skip to content

Commit eb99fae

Browse files
committed
JumpThreading: fix bitwise not on non-booleans
1 parent f7c8928 commit eb99fae

File tree

5 files changed

+85
-12
lines changed

5 files changed

+85
-12
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ impl ScalarInt {
400400
}
401401
}
402402

403+
impl std::ops::Not for ScalarInt {
404+
type Output = ScalarInt;
405+
406+
fn not(self) -> Self::Output {
407+
ScalarInt { data: self.size().truncate(!self.data), size: self.size }
408+
}
409+
}
410+
403411
macro_rules! from_x_for_scalar_int {
404412
($($ty:ty),*) => {
405413
$(

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;
154+
157155
self
158156
}
159157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `not_int` before JumpThreading
2+
+ // MIR for `not_int` after JumpThreading
3+
4+
fn not_int() -> i32 {
5+
let mut _0: i32;
6+
let _1: i32;
7+
let mut _2: bool;
8+
let mut _3: i32;
9+
let mut _4: i32;
10+
scope 1 {
11+
debug a => _1;
12+
}
13+
14+
bb0: {
15+
StorageLive(_1);
16+
_1 = const 1_i32;
17+
StorageLive(_2);
18+
StorageLive(_3);
19+
StorageLive(_4);
20+
_4 = copy _1;
21+
_3 = Not(move _4);
22+
StorageDead(_4);
23+
_2 = Eq(move _3, const 0_i32);
24+
- switchInt(move _2) -> [0: bb2, otherwise: bb1];
25+
+ goto -> bb2;
26+
}
27+
28+
bb1: {
29+
StorageDead(_3);
30+
_0 = const 1_i32;
31+
goto -> bb3;
32+
}
33+
34+
bb2: {
35+
StorageDead(_3);
36+
_0 = const 0_i32;
37+
goto -> bb3;
38+
}
39+
40+
bb3: {
41+
StorageDead(_2);
42+
StorageDead(_1);
43+
return;
44+
}
45+
}
46+

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

+18
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,22 @@ fn floats() -> u32 {
531531
if x == 0.0 { 0 } else { 1 }
532532
}
533533

534+
fn not_int() -> i32 {
535+
// CHECK-LABEL: fn not_int(
536+
// CHECK: goto -> bb2
537+
538+
// CHECK-LABEL: bb1: {
539+
// _0 = const 1_i32;
540+
541+
// CHECK-LABEL: bb2: {
542+
// _0 = const 0_i32;
543+
544+
// Test for issue #131195, where !a == b is assumed to be equivalent to a != b
545+
// This is only the case for bools
546+
let a = 1;
547+
if !a == 0 { 1 } else { 0 }
548+
}
549+
534550
fn main() {
535551
// CHECK-LABEL: fn main(
536552
too_complex(Ok(0));
@@ -546,6 +562,7 @@ fn main() {
546562
aggregate(7);
547563
assume(7, false);
548564
floats();
565+
not_int();
549566
}
550567

551568
// EMIT_MIR jump_threading.too_complex.JumpThreading.diff
@@ -562,3 +579,4 @@ fn main() {
562579
// EMIT_MIR jump_threading.assume.JumpThreading.diff
563580
// EMIT_MIR jump_threading.aggregate_copy.JumpThreading.diff
564581
// EMIT_MIR jump_threading.floats.JumpThreading.diff
582+
// EMIT_MIR jump_threading.not_int.JumpThreading.diff

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)