@@ -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
0 commit comments