diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-04-28 22:56:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-28 22:56:46 +0200 |
| commit | 03da9dbea05a87b9cdf17668881cbc473dcaf237 (patch) | |
| tree | 2870392f1aae7bbf72f9d752a71f4155cf38d457 /compiler/rustc_const_eval | |
| parent | 37076ebbe51cd5fe3c6013dc47870ded9efdf497 (diff) | |
| parent | 586d17d330fa320deb1c2d738fb90d1235a7ade1 (diff) | |
| download | rust-03da9dbea05a87b9cdf17668881cbc473dcaf237.tar.gz rust-03da9dbea05a87b9cdf17668881cbc473dcaf237.zip | |
Rollup merge of #110944 - RalfJung:offset, r=compiler-errors
share BinOp::Offset between CTFE and Miri r? ``@oli-obk``
Diffstat (limited to 'compiler/rustc_const_eval')
| -rw-r--r-- | compiler/rustc_const_eval/src/const_eval/machine.rs | 17 | ||||
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/operator.rs | 26 |
2 files changed, 29 insertions, 14 deletions
diff --git a/compiler/rustc_const_eval/src/const_eval/machine.rs b/compiler/rustc_const_eval/src/const_eval/machine.rs index bfca58a15b3..814b67b46ec 100644 --- a/compiler/rustc_const_eval/src/const_eval/machine.rs +++ b/compiler/rustc_const_eval/src/const_eval/machine.rs @@ -559,20 +559,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir, } fn binary_ptr_op( - ecx: &InterpCx<'mir, 'tcx, Self>, - bin_op: mir::BinOp, - left: &ImmTy<'tcx>, - right: &ImmTy<'tcx>, + _ecx: &InterpCx<'mir, 'tcx, Self>, + _bin_op: mir::BinOp, + _left: &ImmTy<'tcx>, + _right: &ImmTy<'tcx>, ) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> { - if bin_op == mir::BinOp::Offset { - let ptr = left.to_scalar().to_pointer(ecx)?; - let offset_count = right.to_scalar().to_target_isize(ecx)?; - let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty; - - let offset_ptr = ecx.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?; - return Ok((Scalar::from_maybe_pointer(offset_ptr, ecx), false, left.layout.ty)); - } - throw_unsup_format!("pointer arithmetic or comparison is not supported at compile-time"); } diff --git a/compiler/rustc_const_eval/src/interpret/operator.rs b/compiler/rustc_const_eval/src/interpret/operator.rs index 4decfe863e6..7186148daf0 100644 --- a/compiler/rustc_const_eval/src/interpret/operator.rs +++ b/compiler/rustc_const_eval/src/interpret/operator.rs @@ -299,6 +299,30 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { Ok((val, false, ty)) } + fn binary_ptr_op( + &self, + bin_op: mir::BinOp, + left: &ImmTy<'tcx, M::Provenance>, + right: &ImmTy<'tcx, M::Provenance>, + ) -> InterpResult<'tcx, (Scalar<M::Provenance>, bool, Ty<'tcx>)> { + use rustc_middle::mir::BinOp::*; + + match bin_op { + // Pointer ops that are always supported. + Offset => { + let ptr = left.to_scalar().to_pointer(self)?; + let offset_count = right.to_scalar().to_target_isize(self)?; + let pointee_ty = left.layout.ty.builtin_deref(true).unwrap().ty; + + let offset_ptr = self.ptr_offset_inbounds(ptr, pointee_ty, offset_count)?; + Ok((Scalar::from_maybe_pointer(offset_ptr, self), false, left.layout.ty)) + } + + // Fall back to machine hook so Miri can support more pointer ops. + _ => M::binary_ptr_op(self, bin_op, left, right), + } + } + /// Returns the result of the specified operation, whether it overflowed, and /// the result type. pub fn overflowing_binary_op( @@ -368,7 +392,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> { right.layout.ty ); - M::binary_ptr_op(self, bin_op, left, right) + self.binary_ptr_op(bin_op, left, right) } _ => span_bug!( self.cur_span(), |
