@@ -23,29 +23,11 @@ impl<'tcx> ConstReader<'tcx> {
2323 op : & OpTy < ' tcx > ,
2424 ) -> InterpResult < ' tcx , Const < ' tcx > > {
2525 let tcx = self . tcx ;
26- // Make sure we only read through if it's not dangling!
27- let pointee = ecx. deref_pointer ( op) . discard_err ( ) . and_then ( |place| {
28- let ( alloc_id, offset, _) = ecx. ptr_get_alloc_id ( place. ptr ( ) , 0 ) . discard_err ( ) ?;
29- Some ( ( place, alloc_id, offset) )
30- } ) ;
31- let Some ( ( place, alloc_id, offset) ) = pointee else {
32- // Invalid pointer; try reading it as a raw address
33- let int = ecx. read_scalar ( op) ?. to_scalar_int ( ) ?;
34- let kind = ConstKind :: PtrNoProvenance ( int. to_uint ( int. size ( ) ) ) ;
35- return interp_ok ( Const {
36- ty : op. layout . ty ,
37- kind,
38- } ) ;
26+ let ptr_ty = op. layout . ty ;
27+ let ( ty:: Ref ( .., mutability) | ty:: RawPtr ( _, mutability) ) = * ptr_ty. kind ( ) else {
28+ unreachable ! ( "not a pointer type: {ptr_ty:?}" )
3929 } ;
40- // A pointer into the allocation of a `TypeId` can only be expressed as bytes.
41- if let AllocTarget :: TypeId ( _) = self . alloc_target ( alloc_id) {
42- let kind = ConstKind :: Memory ( self . read_raw_bytes ( ecx, op) ?) ;
43- return interp_ok ( Const {
44- ty : op. layout . ty ,
45- kind,
46- } ) ;
47- }
48-
30+ let place = ecx. deref_pointer ( op) ?;
4931 let ty = place. layout . ty ;
5032 let unsize = if place. meta ( ) . has_meta ( ) {
5133 let tail = tcx. struct_tail_for_codegen ( ty, self . typing_env ) ;
@@ -60,6 +42,42 @@ impl<'tcx> ConstReader<'tcx> {
6042 Some ( unsize) => self . is_viewed_unsized_from ( ty) . then_some ( unsize. from ) ,
6143 } ;
6244
45+ // A pointer without provenance -- handle it separately to avoid ICEs.
46+ if let ( None , addr) = place. ptr ( ) . into_raw_parts ( ) {
47+ let addr = ConstKind :: PtrNoProvenance ( addr. bytes ( ) . into ( ) ) ;
48+ let kind = match ( unsize, view_ty) {
49+ ( None , _) => match ptr_ty. kind ( ) {
50+ ty:: TyKind :: RawPtr ( ..) => addr,
51+ ty:: TyKind :: Ref ( ..) => ConstKind :: PtrCast {
52+ ptr : Box :: new ( Const {
53+ ty : Ty :: new_ptr ( tcx, ty, mutability) ,
54+ kind : addr,
55+ } ) ,
56+ unsize : None ,
57+ } ,
58+ _ => unreachable ! ( ) ,
59+ } ,
60+ // Unsize the thin pointer with the metadata.
61+ ( Some ( unsize) , Some ( view_ty) ) => ConstKind :: PtrCast {
62+ ptr : Box :: new ( Const {
63+ ty : Ty :: new_ptr ( tcx, view_ty, mutability) ,
64+ kind : addr,
65+ } ) ,
66+ unsize : Some ( unsize) ,
67+ } ,
68+ // We don't have a sized type to unsize from, so we encode the pointer as bytes.
69+ ( Some ( _) , None ) => ConstKind :: Memory ( self . read_raw_bytes ( ecx, op) ?) ,
70+ } ;
71+ return interp_ok ( Const { ty : ptr_ty, kind } ) ;
72+ }
73+
74+ let ( alloc_id, offset, _) = ecx. ptr_get_alloc_id ( place. ptr ( ) , 0 ) ?;
75+ // A pointer into the allocation of a `TypeId` can only be expressed as bytes.
76+ if let AllocTarget :: TypeId ( _) = self . alloc_target ( alloc_id) {
77+ let kind = ConstKind :: Memory ( self . read_raw_bytes ( ecx, op) ?) ;
78+ return interp_ok ( Const { ty : ptr_ty, kind } ) ;
79+ }
80+
6381 let alloc_target = self . alloc_target ( alloc_id) ;
6482 // A pointer outside of its allocation (one-past-the-end is fine) needs a wrapping offset.
6583 let in_bounds = offset <= ecx. get_alloc_info ( alloc_id) . size ;
@@ -77,10 +95,6 @@ impl<'tcx> ConstReader<'tcx> {
7795 ConstKind :: Ptr { target, unsize }
7896 } else {
7997 // Point to the place we reached, then cast the pointer to the right type.
80- let ( ty:: Ref ( .., mutability) | ty:: RawPtr ( _, mutability) ) = * op. layout . ty . kind ( )
81- else {
82- unreachable ! ( )
83- } ;
8498 let ptr = |ty, kind| Box :: new ( Const { ty, kind } ) ;
8599 let inner = ptr (
86100 Ty :: new_ptr ( tcx, end_ty, mutability) ,
@@ -120,10 +134,7 @@ impl<'tcx> ConstReader<'tcx> {
120134 let target = PtrTarget :: Inline ( Box :: new ( self . read_op ( ecx, place. into ( ) ) ?) ) ;
121135 ConstKind :: Ptr { target, unsize }
122136 } ;
123- interp_ok ( Const {
124- ty : op. layout . ty ,
125- kind,
126- } )
137+ interp_ok ( Const { ty : ptr_ty, kind } )
127138 }
128139
129140 /// Whether we view a value of the unsized type `ty` at the sized type it was unsized from: a
0 commit comments