Skip to content

Commit fb80f81

Browse files
committed
Don't run ptr_op for pure integer values
1 parent e4212a6 commit fb80f81

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

miri/operator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator>
4848
// These work on anything
4949
Eq if left_kind == right_kind => {
5050
let result = match (left, right) {
51-
(PrimVal::Bytes(left), PrimVal::Bytes(right)) => left == right,
51+
(PrimVal::Bytes(_), PrimVal::Bytes(_)) => bug!("wrong usage of `ptr_op` by `interpret`"),
5252
(PrimVal::Ptr(left), PrimVal::Ptr(right)) => left == right,
5353
(PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
5454
_ => false,
@@ -57,7 +57,7 @@ impl<'a, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'tcx, super::Evaluator>
5757
}
5858
Ne if left_kind == right_kind => {
5959
let result = match (left, right) {
60-
(PrimVal::Bytes(left), PrimVal::Bytes(right)) => left != right,
60+
(PrimVal::Bytes(_), PrimVal::Bytes(_)) => bug!("wrong usage of `ptr_op` by `interpret`"),
6161
(PrimVal::Ptr(left), PrimVal::Ptr(right)) => left != right,
6262
(PrimVal::Undef, _) | (_, PrimVal::Undef) => return Err(EvalError::ReadUndefBytes),
6363
_ => true,

src/librustc_mir/interpret/machine.rs

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub trait Machine<'tcx>: Sized {
5555
/// Returns `None` if the operation should be handled by the integer
5656
/// op code
5757
///
58+
/// It is guaranteed that `left` and `right` are never both `PrimVal::Ptr`
59+
///
5860
/// Returns a (value, overflowed) pair otherwise
5961
fn ptr_op<'a>(
6062
ecx: &EvalContext<'a, 'tcx, Self>,

src/librustc_mir/interpret/operator.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ impl<'a, 'tcx, M: Machine<'tcx>> EvalContext<'a, 'tcx, M> {
153153

154154
// I: Handle operations that support pointers
155155
if !left_kind.is_float() && !right_kind.is_float() {
156-
if let Some(handled) = M::ptr_op(self, bin_op, left, left_ty, right, right_ty)? {
157-
return Ok(handled);
156+
if left.is_ptr() != right.is_ptr() {
157+
if let Some(handled) = M::ptr_op(self, bin_op, left, left_ty, right, right_ty)? {
158+
return Ok(handled);
159+
}
158160
}
159161
}
160162

0 commit comments

Comments
 (0)