Skip to content

Commit 76869b4

Browse files
committed
Deduplicate const scalar -> machine scalar conversion
1 parent 32b6f24 commit 76869b4

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1096,10 +1096,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10961096
.inner()
10971097
.read_scalar(self, alloc_range(Size::ZERO, layout.size), read_provenance)
10981098
.map_err(|err| err.to_interp_error(alloc_id))?;
1099-
Ok(match scalar {
1100-
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
1101-
Scalar::Int(int) => Scalar::Int(int),
1102-
})
1099+
self.adjust_const_scalar(scalar)
11031100
}
11041101

11051102
pub fn eval_mir_constant(

compiler/rustc_const_eval/src/interpret/operand.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,23 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
723723
Ok(op)
724724
}
725725

726+
pub(super) fn adjust_const_scalar(
727+
&self,
728+
scalar: Scalar,
729+
) -> InterpResult<'tcx, Scalar<M::Provenance>> {
730+
Ok(match scalar {
731+
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
732+
Scalar::Int(int) => Scalar::Int(int),
733+
})
734+
}
735+
726736
pub(crate) fn const_val_to_op(
727737
&self,
728738
val_val: mir::ConstValue<'tcx>,
729739
ty: Ty<'tcx>,
730740
layout: Option<TyAndLayout<'tcx>>,
731741
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
732742
// Other cases need layout.
733-
let adjust_scalar = |scalar| -> InterpResult<'tcx, _> {
734-
Ok(match scalar {
735-
Scalar::Ptr(ptr, size) => Scalar::Ptr(self.global_base_pointer(ptr)?, size),
736-
Scalar::Int(int) => Scalar::Int(int),
737-
})
738-
};
739743
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
740744
let imm = match val_val {
741745
mir::ConstValue::Indirect { alloc_id, offset } => {
@@ -744,7 +748,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
744748
let ptr = self.global_base_pointer(Pointer::new(alloc_id, offset))?;
745749
return Ok(self.ptr_to_mplace(ptr.into(), layout).into());
746750
}
747-
mir::ConstValue::Scalar(x) => adjust_scalar(x)?.into(),
751+
mir::ConstValue::Scalar(x) => self.adjust_const_scalar(x)?.into(),
748752
mir::ConstValue::ZeroSized => Immediate::Uninit,
749753
mir::ConstValue::Slice { data, meta } => {
750754
// We rely on mutability being set correctly in `data` to prevent writes

0 commit comments

Comments
 (0)