Skip to content

Commit 407259b

Browse files
committed
allow mplace_to_imm_ptr to return different types of pointers
1 parent 56603e4 commit 407259b

5 files changed

Lines changed: 17 additions & 7 deletions

File tree

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_imm_ptr(&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_imm_ptr(&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_imm_ptr(&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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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_imm_ptr(&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/place.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,24 @@ where
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!
443444
/// 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`).
444448
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

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_imm_ptr(&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_imm_ptr(&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)