Skip to content

Commit 03da9db

Browse files
authored
Rollup merge of #110944 - RalfJung:offset, r=compiler-errors
share BinOp::Offset between CTFE and Miri r? ``@oli-obk``
2 parents 37076eb + 586d17d commit 03da9db

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

compiler/rustc_const_eval/src/const_eval/machine.rs

+4-13
Original file line numberDiff line numberDiff line change
@@ -559,20 +559,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
559559
}
560560

561561
fn binary_ptr_op(
562-
ecx: &InterpCx<'mir, 'tcx, Self>,
563-
bin_op: mir::BinOp,
564-
left: &ImmTy<'tcx>,
565-
right: &ImmTy<'tcx>,
562+
_ecx: &InterpCx<'mir, 'tcx, Self>,
563+
_bin_op: mir::BinOp,
564+
_left: &ImmTy<'tcx>,
565+
_right: &ImmTy<'tcx>,
566566
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
567-
if bin_op == mir::BinOp::Offset {
568-
let ptr = left.to_scalar().to_pointer(ecx)?;
569-
let offset_count = right.to_scalar().to_target_isize(ecx)?;
570-
let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty;
571-
572-
let offset_ptr = ecx.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
573-
return Ok((Scalar::from_maybe_pointer(offset_ptr, ecx), false, left.layout.ty));
574-
}
575-
576567
throw_unsup_format!("pointer arithmetic or comparison is not supported at compile-time");
577568
}
578569

compiler/rustc_const_eval/src/interpret/operator.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,30 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
299299
Ok((val, false, ty))
300300
}
301301

302+
fn binary_ptr_op(
303+
&self,
304+
bin_op: mir::BinOp,
305+
left: &ImmTy<'tcx, M::Provenance>,
306+
right: &ImmTy<'tcx, M::Provenance>,
307+
) -> InterpResult<'tcx, (Scalar<M::Provenance>, bool, Ty<'tcx>)> {
308+
use rustc_middle::mir::BinOp::*;
309+
310+
match bin_op {
311+
// Pointer ops that are always supported.
312+
Offset => {
313+
let ptr = left.to_scalar().to_pointer(self)?;
314+
let offset_count = right.to_scalar().to_target_isize(self)?;
315+
let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty;
316+
317+
let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?;
318+
Ok((Scalar::from_maybe_pointer(offset_ptr, self), false, left.layout.ty))
319+
}
320+
321+
// Fall back to machine hook so Miri can support more pointer ops.
322+
_ => M::binary_ptr_op(self, bin_op, left, right),
323+
}
324+
}
325+
302326
/// Returns the result of the specified operation, whether it overflowed, and
303327
/// the result type.
304328
pub fn overflowing_binary_op(
@@ -368,7 +392,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
368392
right.layout.ty
369393
);
370394

371-
M::binary_ptr_op(self, bin_op, left, right)
395+
self.binary_ptr_op(bin_op, left, right)
372396
}
373397
_ => span_bug!(
374398
self.cur_span(),

src/tools/miri/src/operator.rs

-11
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,6 @@ impl<'mir, 'tcx> EvalContextExt<'tcx> for super::MiriInterpCx<'mir, 'tcx> {
5353
(Scalar::from_bool(res), false, self.tcx.types.bool)
5454
}
5555

56-
Offset => {
57-
assert!(left.layout.ty.is_unsafe_ptr());
58-
let ptr = left.to_scalar().to_pointer(self)?;
59-
let offset = right.to_scalar().to_target_isize(self)?;
60-
61-
let pointee_ty =
62-
left.layout.ty.builtin_deref(true).expect("Offset called on non-ptr type").ty;
63-
let ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset)?;
64-
(Scalar::from_maybe_pointer(ptr, self), false, left.layout.ty)
65-
}
66-
6756
// Some more operations are possible with atomics.
6857
// The return value always has the provenance of the *left* operand.
6958
Add | Sub | BitOr | BitAnd | BitXor => {

0 commit comments

Comments
 (0)