Skip to content

Commit 4e89a7c

Browse files
committed
now we can make scalar_to_ptr a method on Scalar
1 parent 665a7e8 commit 4e89a7c

File tree

10 files changed

+41
-45
lines changed

10 files changed

+41
-45
lines changed

Diff for: compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -173,18 +173,17 @@ pub(super) fn op_to_const<'tcx>(
173173
Immediate::ScalarPair(a, b) => {
174174
debug!("ScalarPair(a: {:?}, b: {:?})", a, b);
175175
// We know `offset` is relative to the allocation, so we can use `into_parts`.
176-
let (data, start) =
177-
match ecx.scalar_to_ptr(a.check_init().unwrap()).unwrap().into_parts() {
178-
(Some(alloc_id), offset) => {
179-
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
180-
}
181-
(None, _offset) => (
182-
ecx.tcx.intern_const_alloc(
183-
Allocation::from_bytes_byte_aligned_immutable(b"" as &[u8]),
184-
),
185-
0,
186-
),
187-
};
176+
let (data, start) = match a.to_pointer(ecx).unwrap().into_parts() {
177+
(Some(alloc_id), offset) => {
178+
(ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes())
179+
}
180+
(None, _offset) => (
181+
ecx.tcx.intern_const_alloc(Allocation::from_bytes_byte_aligned_immutable(
182+
b"" as &[u8],
183+
)),
184+
0,
185+
),
186+
};
188187
let len = b.to_machine_usize(ecx).unwrap();
189188
let start = start.try_into().unwrap();
190189
let len: usize = len.try_into().unwrap();

Diff for: compiler/rustc_const_eval/src/interpret/cast.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
180180
assert!(cast_ty.is_integral());
181181

182182
let scalar = src.to_scalar()?;
183-
let ptr = self.scalar_to_ptr(scalar)?;
183+
let ptr = scalar.to_pointer(self)?;
184184
match ptr.into_pointer_or_addr() {
185185
Ok(ptr) => M::expose_ptr(self, ptr)?,
186186
Err(_) => {} // Do nothing, exposing an invalid pointer (`None` provenance) is a NOP.
@@ -299,7 +299,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
299299
}
300300
(&ty::Dynamic(ref data_a, ..), &ty::Dynamic(ref data_b, ..)) => {
301301
let (old_data, old_vptr) = self.read_immediate(src)?.to_scalar_pair()?;
302-
let old_vptr = self.scalar_to_ptr(old_vptr)?;
302+
let old_vptr = old_vptr.to_pointer(self)?;
303303
let (ty, old_trait) = self.get_ptr_vtable(old_vptr)?;
304304
if old_trait != data_a.principal() {
305305
throw_ub_format!("upcast on a pointer whose vtable does not match its type");

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
629629
Ok(Some((size, align)))
630630
}
631631
ty::Dynamic(..) => {
632-
let vtable = self.scalar_to_ptr(metadata.unwrap_meta())?;
632+
let vtable = metadata.unwrap_meta().to_pointer(self)?;
633633
// Read size and align from vtable (already checks size).
634634
Ok(Some(self.get_vtable_size_and_align(vtable)?))
635635
}

Diff for: compiler/rustc_const_eval/src/interpret/intern.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx, const_eval::Memory
245245
if let ty::Dynamic(..) =
246246
tcx.struct_tail_erasing_lifetimes(referenced_ty, self.ecx.param_env).kind()
247247
{
248-
let ptr = self.ecx.scalar_to_ptr(mplace.meta.unwrap_meta())?;
248+
let ptr = mplace.meta.unwrap_meta().to_pointer(&tcx)?;
249249
if let Some(alloc_id) = ptr.provenance {
250250
// Explicitly choose const mode here, since vtables are immutable, even
251251
// if the reference of the fat pointer is mutable.

Diff for: compiler/rustc_const_eval/src/interpret/memory.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use std::assert_matches::assert_matches;
1010
use std::borrow::Cow;
1111
use std::collections::VecDeque;
12-
use std::convert::TryFrom;
1312
use std::fmt;
1413
use std::ptr;
1514

@@ -1172,34 +1171,14 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
11721171

11731172
/// Machine pointer introspection.
11741173
impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
1175-
pub fn scalar_to_ptr(
1176-
&self,
1177-
scalar: Scalar<M::Provenance>,
1178-
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
1179-
// We use `to_bits_or_ptr_internal` since we are just implementing the method people need to
1180-
// call to force getting out a pointer.
1181-
Ok(
1182-
match scalar
1183-
.to_bits_or_ptr_internal(self.pointer_size())
1184-
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
1185-
{
1186-
Err(ptr) => ptr.into(),
1187-
Ok(bits) => {
1188-
let addr = u64::try_from(bits).unwrap();
1189-
Pointer::from_addr(addr)
1190-
}
1191-
},
1192-
)
1193-
}
1194-
11951174
/// Test if this value might be null.
11961175
/// If the machine does not support ptr-to-int casts, this is conservative.
11971176
pub fn scalar_may_be_null(&self, scalar: Scalar<M::Provenance>) -> InterpResult<'tcx, bool> {
11981177
Ok(match scalar.try_to_int() {
11991178
Ok(int) => int.is_null(),
12001179
Err(_) => {
12011180
// Can only happen during CTFE.
1202-
let ptr = self.scalar_to_ptr(scalar)?;
1181+
let ptr = scalar.to_pointer(self)?;
12031182
match self.ptr_try_get_alloc_id(ptr) {
12041183
Ok((alloc_id, offset, _)) => {
12051184
let (size, _align, _kind) = self.get_alloc_info(alloc_id);

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_target::abi::{VariantIdx, Variants};
1414
use super::{
1515
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, GlobalId,
1616
InterpCx, InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, Place, PlaceTy, Pointer,
17-
PointerArithmetic, Provenance, Scalar, ScalarMaybeUninit,
17+
Provenance, Scalar, ScalarMaybeUninit,
1818
};
1919

2020
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -455,7 +455,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
455455
&self,
456456
op: &OpTy<'tcx, M::Provenance>,
457457
) -> InterpResult<'tcx, Pointer<Option<M::Provenance>>> {
458-
self.scalar_to_ptr(self.read_scalar(op)?.check_init()?)
458+
self.read_scalar(op)?.to_pointer(self)
459459
}
460460

461461
/// Turn the wide MPlace into a string (must already be dereferenced!)

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ where
331331
Immediate::Uninit => throw_ub!(InvalidUninitBytes(None)),
332332
};
333333

334-
let mplace = MemPlace { ptr: self.scalar_to_ptr(ptr.check_init()?)?, meta };
334+
let mplace = MemPlace { ptr: ptr.to_pointer(self)?, meta };
335335
// When deref'ing a pointer, the *static* alignment given by the type is what matters.
336336
let align = layout.align.abi;
337337
Ok(MPlaceTy { mplace, layout, align })
@@ -889,7 +889,7 @@ where
889889
&self,
890890
mplace: &MPlaceTy<'tcx, M::Provenance>,
891891
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
892-
let vtable = self.scalar_to_ptr(mplace.vtable())?; // also sanity checks the type
892+
let vtable = mplace.vtable().to_pointer(self)?; // also sanity checks the type
893893
let (ty, _) = self.get_ptr_vtable(vtable)?;
894894
let layout = self.layout_of(ty)?;
895895

Diff for: compiler/rustc_const_eval/src/interpret/terminator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
561561
};
562562

563563
// Get the required information from the vtable.
564-
let vptr = self.scalar_to_ptr(receiver_place.meta.unwrap_meta())?;
564+
let vptr = receiver_place.meta.unwrap_meta().to_pointer(self)?;
565565
let (dyn_ty, dyn_trait) = self.get_ptr_vtable(vptr)?;
566566
if dyn_trait != data.principal() {
567567
throw_ub_format!(

Diff for: compiler/rustc_const_eval/src/interpret/validity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
312312
let tail = self.ecx.tcx.struct_tail_erasing_lifetimes(pointee.ty, self.ecx.param_env);
313313
match tail.kind() {
314314
ty::Dynamic(..) => {
315-
let vtable = self.ecx.scalar_to_ptr(meta.unwrap_meta())?;
315+
let vtable = meta.unwrap_meta().to_pointer(self.ecx)?;
316316
// Make sure it is a genuine vtable pointer.
317317
let (_ty, _trait) = try_validation!(
318318
self.ecx.get_ptr_vtable(vtable),
@@ -566,7 +566,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
566566

567567
// If we check references recursively, also check that this points to a function.
568568
if let Some(_) = self.ref_tracking {
569-
let ptr = self.ecx.scalar_to_ptr(value)?;
569+
let ptr = value.to_pointer(self.ecx)?;
570570
let _fn = try_validation!(
571571
self.ecx.get_ptr_fn(ptr),
572572
self.path,

Diff for: compiler/rustc_middle/src/mir/interpret/value.rs

+18
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,19 @@ impl<Prov> Scalar<Prov> {
331331
}
332332

333333
impl<'tcx, Prov: Provenance> Scalar<Prov> {
334+
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
335+
match self
336+
.to_bits_or_ptr_internal(cx.pointer_size())
337+
.map_err(|s| err_ub!(ScalarSizeMismatch(s)))?
338+
{
339+
Err(ptr) => Ok(ptr.into()),
340+
Ok(bits) => {
341+
let addr = u64::try_from(bits).unwrap();
342+
Ok(Pointer::from_addr(addr))
343+
}
344+
}
345+
}
346+
334347
/// Fundamental scalar-to-int (cast) operation. Many convenience wrappers exist below, that you
335348
/// likely want to use instead.
336349
///
@@ -546,6 +559,11 @@ impl<Prov> ScalarMaybeUninit<Prov> {
546559
}
547560

548561
impl<'tcx, Prov: Provenance> ScalarMaybeUninit<Prov> {
562+
#[inline(always)]
563+
pub fn to_pointer(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, Pointer<Option<Prov>>> {
564+
self.check_init()?.to_pointer(cx)
565+
}
566+
549567
#[inline(always)]
550568
pub fn to_bool(self) -> InterpResult<'tcx, bool> {
551569
self.check_init()?.to_bool()

0 commit comments

Comments
 (0)