Skip to content

Commit

Permalink
Deduplicate const scalar -> machine scalar conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 17, 2023
1 parent 32b6f24 commit 76869b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
5 changes: 1 addition & 4 deletions compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
.inner()
.read_scalar(self, alloc_range(Size::ZERO, layout.size), read_provenance)
.map_err(|err| err.to_interp_error(alloc_id))?;
Ok(match scalar {
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
Scalar::Int(int) => Scalar::Int(int),
})
self.adjust_const_scalar(scalar)
}

pub fn eval_mir_constant(
Expand Down
18 changes: 11 additions & 7 deletions compiler/rustc_const_eval/src/interpret/operand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,19 +723,23 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
Ok(op)
}

pub(super) fn adjust_const_scalar(
&self,
scalar: Scalar,
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
Ok(match scalar {
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
Scalar::Int(int) => Scalar::Int(int),
})
}

pub(crate) fn const_val_to_op(
&self,
val_val: mir::ConstValue<'tcx>,
ty: Ty<'tcx>,
layout: Option<TyAndLayout<'tcx>>,
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
// Other cases need layout.
let adjust_scalar = |scalar| -> InterpResult<'tcx, _> {
Ok(match scalar {
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
Scalar::Int(int) => Scalar::Int(int),
})
};
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
let imm = match val_val {
mir::ConstValue::Indirect { alloc_id, offset } => {
Expand All @@ -744,7 +748,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
let ptr = self.global_base_pointer(Pointer::new(alloc_id, offset))?;
return Ok(self.ptr_to_mplace(ptr.into(), layout).into());
}
mir::ConstValue::Scalar(x) => adjust_scalar(x)?.into(),
mir::ConstValue::Scalar(x) => self.adjust_const_scalar(x)?.into(),
mir::ConstValue::ZeroSized => Immediate::Uninit,
mir::ConstValue::Slice { data, meta } => {
// We rely on mutability being set correctly in `data` to prevent writes
Expand Down

0 comments on commit 76869b4

Please sign in to comment.