Skip to content

Commit a49290d

Browse files
authored
Unrolled build for #154894
Rollup merge of #154894 - WaffleLapkin:mplace_ptr_conversions, r=RalfJung Slightly refactor mplace<->ptr conversions split off of #154327 r? RalfJung
2 parents bcded33 + 407259b commit a49290d

10 files changed

Lines changed: 32 additions & 22 deletions

File tree

compiler/rustc_const_eval/src/const_eval/machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> {
840840
{
841841
// That next check is expensive, that's why we have all the guards above.
842842
let is_immutable = ty.is_freeze(*ecx.tcx, ecx.typing_env());
843-
let place = ecx.ref_to_mplace(val)?;
843+
let place = ecx.imm_ptr_to_mplace(val)?;
844844
let new_place = if is_immutable {
845845
place.map_provenance(CtfeProvenance::as_immutable)
846846
} else {

compiler/rustc_const_eval/src/const_eval/type_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
261261
None => Cow::Owned(idx.to_string()), // For tuples
262262
};
263263
let name_place = self.allocate_str_dedup(&name)?;
264-
let ptr = self.mplace_to_ref(&name_place)?;
264+
let ptr = self.mplace_to_imm_ptr(&name_place, None)?;
265265
self.write_immediate(*ptr, &field_place)?
266266
}
267267
sym::ty => {
@@ -444,7 +444,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
444444
other_abi => {
445445
let (variant, variant_place) = self.downcast(&field_place, sym::Named)?;
446446
let str_place = self.allocate_str_dedup(other_abi.as_str())?;
447-
let str_ref = self.mplace_to_ref(&str_place)?;
447+
let str_ref = self.mplace_to_imm_ptr(&str_place, None)?;
448448
let payload = self.project_field(&variant_place, FieldIdx::ZERO)?;
449449
self.write_immediate(*str_ref, &payload)?;
450450
self.write_discriminant(variant, &field_place)?;

compiler/rustc_const_eval/src/const_eval/type_info/adt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
165165
match field_def.name {
166166
sym::name => {
167167
let name_place = self.allocate_str_dedup(variant_def.name.as_str())?;
168-
let ptr = self.mplace_to_ref(&name_place)?;
168+
let ptr = self.mplace_to_imm_ptr(&name_place, None)?;
169169
self.write_immediate(*ptr, &field_place)?
170170
}
171171
sym::fields => {

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
704704
// actually access memory to resolve this method.
705705
// Also see <https://github.com/rust-lang/miri/issues/2786>.
706706
let val = self.read_immediate(&receiver)?;
707-
break self.ref_to_mplace(&val)?;
707+
break self.imm_ptr_to_mplace(&val)?;
708708
}
709709
ty::Dynamic(..) => break receiver.assert_mem_place(), // no immediate unsized values
710710
_ => {
@@ -877,7 +877,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
877877
// then dispatches that to the normal call machinery. However, our call machinery currently
878878
// only supports calling `VtblEntry::Method`; it would choke on a `MetadataDropInPlace`. So
879879
// instead we do the virtual call stuff ourselves. It's easier here than in `eval_fn_call`
880-
// since we can just get a place of the underlying type and use `mplace_to_ref`.
880+
// since we can just get a place of the underlying type and use `mplace_to_imm_ptr`.
881881
let place = match place.layout.ty.kind() {
882882
ty::Dynamic(data, _) => {
883883
// Dropping a trait object. Need to find actual drop fn.
@@ -898,7 +898,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
898898
};
899899
let fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, ty::List::empty())?;
900900

901-
let arg = self.mplace_to_ref(&place)?;
901+
let arg = self.mplace_to_imm_ptr(&place, None)?;
902902
let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?);
903903

904904
self.init_fn_call(

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
281281
sym::align_of_val | sym::size_of_val => {
282282
// Avoid `deref_pointer` -- this is not a deref, the ptr does not have to be
283283
// dereferenceable!
284-
let place = self.ref_to_mplace(&self.read_immediate(&args[0])?)?;
284+
let place = self.imm_ptr_to_mplace(&self.read_immediate(&args[0])?)?;
285285
let (size, align) = self
286286
.size_and_align_of_val(&place)?
287287
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,36 +417,46 @@ where
417417
self.ptr_with_meta_to_mplace(ptr, MemPlaceMeta::None, layout, /*unaligned*/ true)
418418
}
419419

420-
/// Take a value, which represents a (thin or wide) reference, and make it a place.
421-
/// Alignment is just based on the type. This is the inverse of `mplace_to_ref()`.
420+
/// Take a value, which represents a (thin or wide) pointer, and make it a place.
421+
/// Alignment is just based on the type. This is the inverse of `mplace_to_imm_ptr()`.
422422
///
423423
/// Only call this if you are sure the place is "valid" (aligned and inbounds), or do not
424424
/// want to ever use the place for memory access!
425425
/// Generally prefer `deref_pointer`.
426-
pub fn ref_to_mplace(
426+
pub fn imm_ptr_to_mplace(
427427
&self,
428428
val: &ImmTy<'tcx, M::Provenance>,
429429
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
430430
let pointee_type =
431-
val.layout.ty.builtin_deref(true).expect("`ref_to_mplace` called on non-ptr type");
431+
val.layout.ty.builtin_deref(true).expect("`imm_ptr_to_mplace` called on non-ptr type");
432432
let layout = self.layout_of(pointee_type)?;
433433
let (ptr, meta) = val.to_scalar_and_meta();
434434

435-
// `ref_to_mplace` is called on raw pointers even if they don't actually get dereferenced;
435+
// `imm_ptr_to_mplace` is called on raw pointers even if they don't actually get dereferenced;
436436
// we hence can't call `size_and_align_of` since that asserts more validity than we want.
437437
let ptr = ptr.to_pointer(self)?;
438438
interp_ok(self.ptr_with_meta_to_mplace(ptr, meta, layout, /*unaligned*/ false))
439439
}
440440

441441
/// Turn a mplace into a (thin or wide) mutable raw pointer, pointing to the same space.
442+
///
442443
/// `align` information is lost!
443-
/// This is the inverse of `ref_to_mplace`.
444-
pub fn mplace_to_ref(
444+
/// This is the inverse of `imm_ptr_to_mplace`.
445+
///
446+
/// If `ptr_ty` is provided, the resulting pointer will be of that type. Otherwise, it defaults to `*mut _`.
447+
/// `ptr_ty` must be a type with builtin deref which derefs to the type of `mplace` (`mplace.layout.ty`).
448+
pub fn mplace_to_imm_ptr(
445449
&self,
446450
mplace: &MPlaceTy<'tcx, M::Provenance>,
451+
ptr_ty: Option<Ty<'tcx>>,
447452
) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> {
448453
let imm = mplace.mplace.to_ref(self);
449-
let layout = self.layout_of(Ty::new_mut_ptr(self.tcx.tcx, mplace.layout.ty))?;
454+
455+
let ptr_ty = ptr_ty
456+
.inspect(|t| assert_eq!(t.builtin_deref(true), Some(mplace.layout.ty)))
457+
.unwrap_or_else(|| Ty::new_mut_ptr(self.tcx.tcx, mplace.layout.ty));
458+
459+
let layout = self.layout_of(ptr_ty)?;
450460
interp_ok(ImmTy::from_immediate(imm, layout))
451461
}
452462

@@ -467,7 +477,7 @@ where
467477
let val = self.read_immediate(src)?;
468478
trace!("deref to {} on {:?}", val.layout.ty, *val);
469479

470-
let mplace = self.ref_to_mplace(&val)?;
480+
let mplace = self.imm_ptr_to_mplace(&val)?;
471481
interp_ok(mplace)
472482
}
473483

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
572572
self.add_data_range_place(val);
573573
}
574574
// Now turn it into a place.
575-
self.ecx.ref_to_mplace(&imm)
575+
self.ecx.imm_ptr_to_mplace(&imm)
576576
}
577577

578578
fn check_wide_ptr_meta(

src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ trait EvalContextPrivExt<'tcx, 'ecx>: crate::MiriInterpCxExt<'tcx> {
859859
info: RetagInfo, // diagnostics info about this retag
860860
) -> InterpResult<'tcx, ImmTy<'tcx>> {
861861
let this = self.eval_context_mut();
862-
let place = this.ref_to_mplace(val)?;
862+
let place = this.imm_ptr_to_mplace(val)?;
863863
let new_place = this.sb_retag_place(&place, new_perm, info)?;
864864
interp_ok(ImmTy::from_immediate(new_place.to_ref(this), val.layout))
865865
}

src/tools/miri/src/borrow_tracker/tree_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
415415
new_perm: NewPermission,
416416
) -> InterpResult<'tcx, ImmTy<'tcx>> {
417417
let this = self.eval_context_mut();
418-
let place = this.ref_to_mplace(val)?;
418+
let place = this.imm_ptr_to_mplace(val)?;
419419
let new_place = this.tb_retag_place(&place, new_perm)?;
420420
interp_ok(ImmTy::from_immediate(new_place.to_ref(this), val.layout))
421421
}

src/tools/miri/src/shims/panic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
2020
this.call_function(
2121
panic,
2222
ExternAbi::Rust,
23-
&[this.mplace_to_ref(&msg)?],
23+
&[this.mplace_to_imm_ptr(&msg, None)?],
2424
None,
2525
ReturnContinuation::Goto { ret: None, unwind },
2626
)
@@ -39,7 +39,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
3939
this.call_function(
4040
panic,
4141
ExternAbi::Rust,
42-
&[this.mplace_to_ref(&msg)?],
42+
&[this.mplace_to_imm_ptr(&msg, None)?],
4343
None,
4444
ReturnContinuation::Goto { ret: None, unwind: mir::UnwindAction::Unreachable },
4545
)

0 commit comments

Comments
 (0)