Skip to content

Commit 0fb2b24

Browse files
compiler-errorscuviper
authored andcommitted
Disable jump threading UnOp::Not for non-bool
(cherry picked from commit f0bfba2)
1 parent 692821d commit 0fb2b24

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

compiler/rustc_mir_transform/src/jump_threading.rs

+8
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,16 @@ impl<'tcx, 'a> TOFinder<'tcx, 'a> {
492492
}
493493
// Transfer the conditions on the copy rhs, after inversing polarity.
494494
Rvalue::UnaryOp(UnOp::Not, Operand::Move(place) | Operand::Copy(place)) => {
495+
if !place.ty(self.body, self.tcx).ty.is_bool() {
496+
// Constructing the conditions by inverting the polarity
497+
// of equality is only correct for bools. That is to say,
498+
// `!a == b` is not `a != b` for integers greater than 1 bit.
499+
return;
500+
}
495501
let Some(conditions) = state.try_get_idx(lhs, self.map) else { return };
496502
let Some(place) = self.map.find(place.as_ref()) else { return };
503+
// FIXME: I think This could be generalized to not bool if we
504+
// actually perform a logical not on the condition's value.
497505
let conds = conditions.map(self.arena, Condition::inv);
498506
state.insert_value_idx(place, conds, self.map);
499507
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
- // MIR for `bitwise_not` before JumpThreading
2+
+ // MIR for `bitwise_not` after JumpThreading
3+
4+
fn bitwise_not() -> i32 {
5+
let mut _0: i32;
6+
let mut _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 0_i32;
17+
_1 = const 1_i32;
18+
StorageLive(_2);
19+
StorageLive(_3);
20+
StorageLive(_4);
21+
_4 = copy _1;
22+
_3 = Not(move _4);
23+
StorageDead(_4);
24+
_2 = Eq(move _3, const 0_i32);
25+
switchInt(move _2) -> [0: bb2, otherwise: bb1];
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+

tests/mir-opt/jump_threading.bitwise_not.JumpThreading.panic-unwind.diff

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

2928
bb1: {

0 commit comments

Comments
 (0)