Skip to content

Commit acc1dc2

Browse files
authored
Rollup merge of #70508 - RalfJung:scalar-from, r=eddyb
Miri: use more specialized Scalar::from_ constructors where appropriate
2 parents 96e2934 + bd9e046 commit acc1dc2

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

src/librustc_mir/interpret/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
208208

209209
Char => {
210210
// `u8` to `char` cast
211-
Ok(Scalar::from_uint(u8::try_from(v).unwrap(), Size::from_bytes(4)))
211+
Ok(Scalar::from_u32(u8::try_from(v).unwrap().into()))
212212
}
213213

214214
// Casts to bool are not permitted by rustc, no need to handle them here.

src/librustc_mir/interpret/intrinsics.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//! looking at their MIR. Intrinsics/functions supported here are shared by CTFE
33
//! and miri.
44
5+
use std::convert::TryFrom;
6+
57
use rustc::mir::{
68
self,
79
interpret::{ConstValue, GlobalId, InterpResult, Scalar},
@@ -220,7 +222,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
220222
sym::discriminant_value => {
221223
let place = self.deref_operand(args[0])?;
222224
let discr_val = self.read_discriminant(place.into())?.0;
223-
self.write_scalar(Scalar::from_uint(discr_val, dest.layout.size), dest)?;
225+
self.write_scalar(Scalar::from_u64(u64::try_from(discr_val).unwrap()), dest)?;
224226
}
225227
sym::unchecked_shl
226228
| sym::unchecked_shr
@@ -275,7 +277,6 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
275277
}
276278

277279
sym::ptr_offset_from => {
278-
let isize_layout = self.layout_of(self.tcx.types.isize)?;
279280
let a = self.read_immediate(args[0])?.to_scalar()?;
280281
let b = self.read_immediate(args[1])?.to_scalar()?;
281282

@@ -292,7 +293,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
292293
let a = a.to_machine_usize(self)?;
293294
let b = b.to_machine_usize(self)?;
294295
if a == b && a != 0 {
295-
self.write_scalar(Scalar::from_int(0, isize_layout.size), dest)?;
296+
self.write_scalar(Scalar::from_machine_isize(0, self), dest)?;
296297
true
297298
} else {
298299
false
@@ -312,6 +313,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
312313
);
313314
}
314315
let usize_layout = self.layout_of(self.tcx.types.usize)?;
316+
let isize_layout = self.layout_of(self.tcx.types.isize)?;
315317
let a_offset = ImmTy::from_uint(a.offset.bytes(), usize_layout);
316318
let b_offset = ImmTy::from_uint(b.offset.bytes(), usize_layout);
317319
let (val, _overflowed, _ty) =

src/librustc_mir/interpret/operand.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ impl<Tag> From<Pointer<Tag>> for Immediate<Tag> {
5454

5555
impl<'tcx, Tag> Immediate<Tag> {
5656
pub fn new_slice(val: Scalar<Tag>, len: u64, cx: &impl HasDataLayout) -> Self {
57-
Immediate::ScalarPair(
58-
val.into(),
59-
Scalar::from_uint(len, cx.data_layout().pointer_size).into(),
60-
)
57+
Immediate::ScalarPair(val.into(), Scalar::from_machine_usize(len, cx).into())
6158
}
6259

6360
pub fn new_dyn_trait(val: Scalar<Tag>, vtable: Pointer<Tag>) -> Self {
@@ -621,7 +618,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
621618
let real_discr = if discr_val.layout.abi.is_signed() {
622619
// going from layout tag type to typeck discriminant type
623620
// requires first sign extending with the discriminant layout
624-
let sexted = sign_extend(bits_discr, discr_val.layout.size) as i128;
621+
let sexted = sign_extend(bits_discr, discr_val.layout.size);
625622
// and then zeroing with the typeck discriminant type
626623
let discr_ty = rval
627624
.layout
@@ -631,8 +628,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
631628
.repr
632629
.discr_type();
633630
let size = layout::Integer::from_attr(self, discr_ty).size();
634-
let truncatee = sexted as u128;
635-
truncate(truncatee, size)
631+
truncate(sexted, size)
636632
} else {
637633
bits_discr
638634
};

src/librustc_mir/interpret/place.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'tcx, Tag> MPlaceTy<'tcx, Tag> {
180180
#[inline]
181181
pub fn dangling(layout: TyLayout<'tcx>, cx: &impl HasDataLayout) -> Self {
182182
let align = layout.align.abi;
183-
let ptr = Scalar::from_uint(align.bytes(), cx.pointer_size());
183+
let ptr = Scalar::from_machine_usize(align.bytes(), cx);
184184
// `Poison` this to make sure that the pointer value `ptr` is never observable by the program.
185185
MPlaceTy { mplace: MemPlace { ptr, align, meta: MemPlaceMeta::Poison }, layout }
186186
}
@@ -504,7 +504,7 @@ where
504504
// implement this.
505505
ty::Array(inner, _) => (MemPlaceMeta::None, self.tcx.mk_array(inner, inner_len)),
506506
ty::Slice(..) => {
507-
let len = Scalar::from_uint(inner_len, self.pointer_size());
507+
let len = Scalar::from_machine_usize(inner_len, self);
508508
(MemPlaceMeta::Meta(len), base.layout.ty)
509509
}
510510
_ => bug!("cannot subslice non-array type: `{:?}`", base.layout.ty),
@@ -1044,7 +1044,7 @@ where
10441044
kind: MemoryKind<M::MemoryKind>,
10451045
) -> MPlaceTy<'tcx, M::PointerTag> {
10461046
let ptr = self.memory.allocate_bytes(str.as_bytes(), kind);
1047-
let meta = Scalar::from_uint(u128::try_from(str.len()).unwrap(), self.pointer_size());
1047+
let meta = Scalar::from_machine_usize(u64::try_from(str.len()).unwrap(), self);
10481048
let mplace = MemPlace {
10491049
ptr: ptr.into(),
10501050
align: Align::from_bytes(1).unwrap(),

src/librustc_mir/interpret/step.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! The main entry point is the `step` method.
44
55
use rustc::mir;
6-
use rustc::mir::interpret::{InterpResult, PointerArithmetic, Scalar};
6+
use rustc::mir::interpret::{InterpResult, Scalar};
77
use rustc::ty::layout::LayoutOf;
88

99
use super::{InterpCx, Machine};
@@ -229,8 +229,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
229229
let src = self.eval_place(place)?;
230230
let mplace = self.force_allocation(src)?;
231231
let len = mplace.len(self)?;
232-
let size = self.pointer_size();
233-
self.write_scalar(Scalar::from_uint(len, size), dest)?;
232+
self.write_scalar(Scalar::from_machine_usize(len, self), dest)?;
234233
}
235234

236235
AddressOf(_, ref place) | Ref(_, _, ref place) => {
@@ -254,8 +253,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
254253
!layout.is_unsized(),
255254
"SizeOf nullary MIR operator called for unsized type"
256255
);
257-
let size = self.pointer_size();
258-
self.write_scalar(Scalar::from_uint(layout.size.bytes(), size), dest)?;
256+
self.write_scalar(Scalar::from_machine_usize(layout.size.bytes(), self), dest)?;
259257
}
260258

261259
Cast(kind, ref operand, _) => {

0 commit comments

Comments
 (0)