Skip to content

Commit 780e1fc

Browse files
committed
use From to convert scalars to immediates
1 parent b318bcf commit 780e1fc

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
@@ -47,11 +47,6 @@ impl<Tag> From<Scalar<Tag>> for Immediate<Tag> {
4747
}
4848

4949
impl<'tcx, Tag> Immediate<Tag> {
50-
#[inline]
51-
pub fn from_scalar(val: Scalar<Tag>) -> Self {
52-
Immediate::Scalar(ScalarMaybeUndef::Scalar(val))
53-
}
54-
5550
pub fn new_slice(
5651
val: Scalar<Tag>,
5752
len: u64,
@@ -196,7 +191,7 @@ impl<'tcx, Tag: Copy> ImmTy<'tcx, Tag>
196191
{
197192
#[inline]
198193
pub fn from_scalar(val: Scalar<Tag>, layout: TyLayout<'tcx>) -> Self {
199-
ImmTy { imm: Immediate::from_scalar(val), layout }
194+
ImmTy { imm: val.into(), layout }
200195
}
201196

202197
#[inline]
@@ -254,7 +249,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
254249
let ptr = match self.check_mplace_access(mplace, None)? {
255250
Some(ptr) => ptr,
256251
None => return Ok(Some(ImmTy { // zero-sized type
257-
imm: Immediate::Scalar(Scalar::zst().into()),
252+
imm: Scalar::zst().into(),
258253
layout: mplace.layout,
259254
})),
260255
};
@@ -265,7 +260,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
265260
.get(ptr.alloc_id)?
266261
.read_scalar(self, ptr, mplace.layout.size)?;
267262
Ok(Some(ImmTy {
268-
imm: Immediate::Scalar(scalar),
263+
imm: scalar.into(),
269264
layout: mplace.layout,
270265
}))
271266
}
@@ -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
};
@@ -570,7 +565,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
570565
Operand::Indirect(MemPlace::from_ptr(ptr, align))
571566
},
572567
ConstValue::Scalar(x) =>
573-
Operand::Immediate(Immediate::Scalar(tag_scalar(x).into())),
568+
Operand::Immediate(tag_scalar(x).into()),
574569
ConstValue::Slice { data, start, end } => {
575570
// We rely on mutability being set correctly in `data` to prevent writes
576571
// 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, 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> {
@@ -460,7 +460,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
460460
// Adjust receiver argument.
461461
args[0] = OpTy::from(ImmTy {
462462
layout: this_receiver_ptr,
463-
imm: Immediate::Scalar(receiver_place.ptr.into())
463+
imm: receiver_place.ptr.into()
464464
});
465465
trace!("Patched self operand to {:#?}", args[0]);
466466
// recurse with concrete function

0 commit comments

Comments
 (0)