Skip to content

Commit 8debe64

Browse files
committed
use From to convert scalars to immediates
1 parent f92c3a3 commit 8debe64

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

src/librustc_mir/interpret/operand.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ impl<Tag> From<Scalar<Tag>> for Immediate<Tag> {
4848
}
4949

5050
impl<'tcx, Tag> Immediate<Tag> {
51-
#[inline]
52-
pub fn from_scalar(val: Scalar<Tag>) -> Self {
53-
Immediate::Scalar(ScalarMaybeUndef::Scalar(val))
54-
}
55-
5651
pub fn new_slice(
5752
val: Scalar<Tag>,
5853
len: u64,
@@ -197,7 +192,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag>
197192
{
198193
#[inline]
199194
pub fn from_scalar(val: Scalar<Tag>, layout: TyLayout<'tcx>) -> Self {
200-
ImmTy { imm: Immediate::from_scalar(val), layout }
195+
ImmTy { imm: val.into(), layout }
201196
}
202197

203198
#[inline]
@@ -255,7 +250,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
255250
let ptr = match self.check_mplace_access(mplace, None)? {
256251
Some(ptr) => ptr,
257252
None => return Ok(Some(ImmTy { // zero-sized type
258-
imm: Immediate::Scalar(Scalar::zst().into()),
253+
imm: Scalar::zst().into(),
259254
layout: mplace.layout,
260255
})),
261256
};
@@ -266,7 +261,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
266261
.get(ptr.alloc_id)?
267262
.read_scalar(self, ptr, mplace.layout.size)?;
268263
Ok(Some(ImmTy {
269-
imm: Immediate::Scalar(scalar),
264+
imm: scalar.into(),
270265
layout: mplace.layout,
271266
}))
272267
}
@@ -368,7 +363,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
368363
let field = field.try_into().unwrap();
369364
let field_layout = op.layout.field(self, field)?;
370365
if field_layout.is_zst() {
371-
let immediate = Immediate::Scalar(Scalar::zst().into());
366+
let immediate = Scalar::zst().into();
372367
return Ok(OpTy { op: Operand::Immediate(immediate), layout: field_layout });
373368
}
374369
let offset = op.layout.fields.offset(field);
@@ -378,7 +373,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
378373
// extract fields from types with `ScalarPair` ABI
379374
Immediate::ScalarPair(a, b) => {
380375
let val = if offset.bytes() == 0 { a } else { b };
381-
Immediate::Scalar(val)
376+
Immediate::from(val)
382377
},
383378
Immediate::Scalar(val) =>
384379
bug!("field access on non aggregate {:#?}, {:#?}", val, op.layout),
@@ -415,7 +410,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
415410
Deref => self.deref_operand(base)?.into(),
416411
Subslice { .. } | ConstantIndex { .. } | Index(_) => if base.layout.is_zst() {
417412
OpTy {
418-
op: Operand::Immediate(Immediate::Scalar(Scalar::zst().into())),
413+
op: Operand::Immediate(Scalar::zst().into()),
419414
// the actual index doesn't matter, so we just pick a convenient one like 0
420415
layout: base.layout.field(self, 0)?,
421416
}
@@ -439,7 +434,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
439434
let layout = self.layout_of_local(frame, local, layout)?;
440435
let op = if layout.is_zst() {
441436
// Do not read from ZST, they might not be initialized
442-
Operand::Immediate(Immediate::Scalar(Scalar::zst().into()))
437+
Operand::Immediate(Scalar::zst().into())
443438
} else {
444439
frame.locals[local].access()?
445440
};
@@ -567,7 +562,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
567562
Operand::Indirect(MemPlace::from_ptr(ptr, align))
568563
},
569564
ConstValue::Scalar(x) =>
570-
Operand::Immediate(Immediate::Scalar(tag_scalar(x).into())),
565+
Operand::Immediate(tag_scalar(x).into()),
571566
ConstValue::Slice { data, start, end } => {
572567
// We rely on mutability being set correctly in `data` to prevent writes
573568
// where none should happen.

src/librustc_mir/interpret/terminator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_target::spec::abi::Abi;
88

99
use super::{
1010
InterpResult, PointerArithmetic, InterpError, Scalar,
11-
InterpCx, Machine, Immediate, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
11+
InterpCx, Machine, OpTy, ImmTy, PlaceTy, MPlaceTy, StackPopCleanup, FnVal,
1212
};
1313

1414
impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
@@ -462,7 +462,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
462462
// Adjust receiver argument.
463463
args[0] = OpTy::from(ImmTy {
464464
layout: this_receiver_ptr,
465-
imm: Immediate::Scalar(receiver_place.ptr.into())
465+
imm: receiver_place.ptr.into()
466466
});
467467
trace!("Patched self operand to {:#?}", args[0]);
468468
// recurse with concrete function

0 commit comments

Comments
 (0)