diff --git a/compiler/rustc_const_eval/src/const_eval/type_info.rs b/compiler/rustc_const_eval/src/const_eval/type_info.rs index 427100e78aae..dffc66f731af 100644 --- a/compiler/rustc_const_eval/src/const_eval/type_info.rs +++ b/compiler/rustc_const_eval/src/const_eval/type_info.rs @@ -261,7 +261,7 @@ fn write_field( None => Cow::Owned(idx.to_string()), // For tuples }; let name_place = self.allocate_str_dedup(&name)?; - let ptr = self.mplace_to_imm_ptr(&name_place)?; + let ptr = self.mplace_to_imm_ptr(&name_place, None)?; self.write_immediate(*ptr, &field_place)? } sym::ty => { @@ -444,7 +444,7 @@ pub(crate) fn write_fn_ptr_type_info( other_abi => { let (variant, variant_place) = self.downcast(&field_place, sym::Named)?; let str_place = self.allocate_str_dedup(other_abi.as_str())?; - let str_ref = self.mplace_to_imm_ptr(&str_place)?; + let str_ref = self.mplace_to_imm_ptr(&str_place, None)?; let payload = self.project_field(&variant_place, FieldIdx::ZERO)?; self.write_immediate(*str_ref, &payload)?; self.write_discriminant(variant, &field_place)?; diff --git a/compiler/rustc_const_eval/src/const_eval/type_info/adt.rs b/compiler/rustc_const_eval/src/const_eval/type_info/adt.rs index ac730faf9d43..2143313bbbad 100644 --- a/compiler/rustc_const_eval/src/const_eval/type_info/adt.rs +++ b/compiler/rustc_const_eval/src/const_eval/type_info/adt.rs @@ -165,7 +165,7 @@ fn write_enum_variant( match field_def.name { sym::name => { let name_place = self.allocate_str_dedup(variant_def.name.as_str())?; - let ptr = self.mplace_to_imm_ptr(&name_place)?; + let ptr = self.mplace_to_imm_ptr(&name_place, None)?; self.write_immediate(*ptr, &field_place)? } sym::fields => { diff --git a/compiler/rustc_const_eval/src/interpret/call.rs b/compiler/rustc_const_eval/src/interpret/call.rs index ef99a64cb147..d948b78a0bcf 100644 --- a/compiler/rustc_const_eval/src/interpret/call.rs +++ b/compiler/rustc_const_eval/src/interpret/call.rs @@ -898,7 +898,7 @@ pub(super) fn init_drop_in_place_call( }; let fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, ty::List::empty())?; - let arg = self.mplace_to_imm_ptr(&place)?; + let arg = self.mplace_to_imm_ptr(&place, None)?; let ret = MPlaceTy::fake_alloc_zst(self.layout_of(self.tcx.types.unit)?); self.init_fn_call( diff --git a/compiler/rustc_const_eval/src/interpret/place.rs b/compiler/rustc_const_eval/src/interpret/place.rs index 0e0ee1ccd8f3..0118fd71a597 100644 --- a/compiler/rustc_const_eval/src/interpret/place.rs +++ b/compiler/rustc_const_eval/src/interpret/place.rs @@ -439,14 +439,24 @@ pub fn imm_ptr_to_mplace( } /// Turn a mplace into a (thin or wide) mutable raw pointer, pointing to the same space. + /// /// `align` information is lost! /// This is the inverse of `imm_ptr_to_mplace`. + /// + /// If `ptr_ty` is provided, the resulting pointer will be of that type. Otherwise, it defaults to `*mut _`. + /// `ptr_ty` must be a type with builtin deref which derefs to the type of `mplace` (`mplace.layout.ty`). pub fn mplace_to_imm_ptr( &self, mplace: &MPlaceTy<'tcx, M::Provenance>, + ptr_ty: Option>, ) -> InterpResult<'tcx, ImmTy<'tcx, M::Provenance>> { let imm = mplace.mplace.to_ref(self); - let layout = self.layout_of(Ty::new_mut_ptr(self.tcx.tcx, mplace.layout.ty))?; + + let ptr_ty = ptr_ty + .inspect(|t| assert_eq!(t.builtin_deref(true), Some(mplace.layout.ty))) + .unwrap_or_else(|| Ty::new_mut_ptr(self.tcx.tcx, mplace.layout.ty)); + + let layout = self.layout_of(ptr_ty)?; interp_ok(ImmTy::from_immediate(imm, layout)) } diff --git a/src/tools/miri/src/shims/panic.rs b/src/tools/miri/src/shims/panic.rs index 34589ecf7af6..50e32cffaee3 100644 --- a/src/tools/miri/src/shims/panic.rs +++ b/src/tools/miri/src/shims/panic.rs @@ -20,7 +20,7 @@ fn start_panic(&mut self, msg: &str, unwind: mir::UnwindAction) -> InterpResult< this.call_function( panic, ExternAbi::Rust, - &[this.mplace_to_imm_ptr(&msg)?], + &[this.mplace_to_imm_ptr(&msg, None)?], None, ReturnContinuation::Goto { ret: None, unwind }, ) @@ -39,7 +39,7 @@ fn start_panic_nounwind(&mut self, msg: &str) -> InterpResult<'tcx> { this.call_function( panic, ExternAbi::Rust, - &[this.mplace_to_imm_ptr(&msg)?], + &[this.mplace_to_imm_ptr(&msg, None)?], None, ReturnContinuation::Goto { ret: None, unwind: mir::UnwindAction::Unreachable }, )