Skip to content

Commit cde17d9

Browse files
committed
Add From instances for Pointer -> ScalarMaybeUndef and Pointer -> Immediate
1 parent a8eea62 commit cde17d9

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,13 @@ impl<Tag> From<Scalar<Tag>> for ScalarMaybeUndef<Tag> {
471471
}
472472
}
473473

474+
impl<Tag> From<Pointer<Tag>> for ScalarMaybeUndef<Tag> {
475+
#[inline(always)]
476+
fn from(s: Pointer<Tag>) -> Self {
477+
ScalarMaybeUndef::Scalar(s.into())
478+
}
479+
}
480+
474481
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
475482
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
476483
match self {

src/librustc_mir/interpret/cast.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
5555
).ok_or_else(|| err_inval!(TooGeneric))?;
5656

5757
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
58-
self.write_scalar(Scalar::Ptr(fn_ptr.into()), dest)?;
58+
self.write_scalar(fn_ptr, dest)?;
5959
}
6060
_ => bug!("reify fn pointer on {:?}", src.layout.ty),
6161
}
@@ -88,8 +88,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8888
ty::ClosureKind::FnOnce,
8989
);
9090
let fn_ptr = self.memory.create_fn_alloc(FnVal::Instance(instance));
91-
let val = Immediate::Scalar(Scalar::Ptr(fn_ptr.into()).into());
92-
self.write_immediate(val, dest)?;
91+
self.write_scalar(fn_ptr, dest)?;
9392
}
9493
_ => bug!("closure fn pointer on {:?}", src.layout.ty),
9594
}

src/librustc_mir/interpret/memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
350350
sptr
351351
} else {
352352
// A "real" access, we must get a pointer.
353-
Scalar::Ptr(self.force_ptr(sptr)?)
353+
Scalar::from(self.force_ptr(sptr)?)
354354
};
355355
Ok(match normalized.to_bits_or_ptr(self.pointer_size(), self) {
356356
Ok(bits) => {

src/librustc_mir/interpret/operand.rs

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

50+
impl<Tag> From<Pointer<Tag>> for Immediate<Tag> {
51+
#[inline(always)]
52+
fn from(val: Pointer<Tag>) -> Self {
53+
Immediate::Scalar(Scalar::from(val).into())
54+
}
55+
}
56+
5057
impl<'tcx, Tag> Immediate<Tag> {
5158
pub fn new_slice(
5259
val: Scalar<Tag>,
@@ -60,7 +67,7 @@ impl<'tcx, Tag> Immediate<Tag> {
6067
}
6168

6269
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
63-
Immediate::ScalarPair(val.into(), Scalar::Ptr(vtable).into())
70+
Immediate::ScalarPair(val.into(), vtable.into())
6471
}
6572

6673
#[inline]

src/librustc_mir/interpret/place.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,7 @@ where
686686
}
687687

688688
/// Write a scalar to a place
689+
#[inline(always)]
689690
pub fn write_scalar(
690691
&mut self,
691692
val: impl Into<ScalarMaybeUndef<M::PointerTag>>,

src/librustc_mir/interpret/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
6767
// allocation is correctly aligned as we created it above. Also we're only offsetting by
6868
// multiples of `ptr_align`, which means that it will stay aligned to `ptr_align`.
6969
let vtable_alloc = self.memory.get_raw_mut(vtable.alloc_id)?;
70-
vtable_alloc.write_ptr_sized(tcx, vtable, Scalar::Ptr(drop).into())?;
70+
vtable_alloc.write_ptr_sized(tcx, vtable, drop.into())?;
7171

7272
let size_ptr = vtable.offset(ptr_size, tcx)?;
7373
vtable_alloc.write_ptr_sized(tcx, size_ptr, Scalar::from_uint(size, ptr_size).into())?;
@@ -87,7 +87,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
8787
// We cannot use `vtable_allic` as we are creating fn ptrs in this loop.
8888
let method_ptr = vtable.offset(ptr_size * (3 + i as u64), tcx)?;
8989
self.memory.get_raw_mut(vtable.alloc_id)?
90-
.write_ptr_sized(tcx, method_ptr, Scalar::Ptr(fn_ptr).into())?;
90+
.write_ptr_sized(tcx, method_ptr, fn_ptr.into())?;
9191
}
9292
}
9393

0 commit comments

Comments
 (0)