Skip to content

Commit a717327

Browse files
Disable jump threading UnOp::Not for non-bool
1 parent 9c91a4e commit a717327

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,16 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
494494
}
495495
// Transfer the conditions on the copy rhs, after inversing polarity.
496496
Rvalue::UnaryOp(UnOp::Not, Operand::Move(place) | Operand::Copy(place)) => {
497+
if !place.ty(self.body, self.tcx).ty.is_bool() {
498+
// Constructing the conditions by inverting the polarity
499+
// of equality is only correct for bools. That is to say,
500+
// `!a == b` is not `a != b` for integers greater than 1 bit.
501+
return;
502+
}
497503
let Some(conditions) = state.try_get_idx(lhs, &self.map) else { return };
498504
let Some(place) = self.map.find(place.as_ref()) else { return };
505+
// FIXME: I think This could be generalized to not bool if we
506+
// actually perform a logical not on the condition's value.
499507
let conds = conditions.map(self.arena, Condition::inv);
500508
state.insert_value_idx(place, conds, &self.map);
501509
}

Diff for: 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)