11use rustc_abi:: FieldIdx ;
2+ use rustc_ast:: Mutability ;
23use rustc_hir:: LangItem ;
34use rustc_middle:: mir:: interpret:: CtfeProvenance ;
45use rustc_middle:: span_bug;
@@ -8,7 +9,7 @@ use rustc_span::{Symbol, sym};
89
910use crate :: const_eval:: CompileTimeMachine ;
1011use crate :: interpret:: {
11- Immediate , InterpCx , InterpResult , MPlaceTy , MemoryKind , Writeable , interp_ok,
12+ Immediate , InterpCx , InterpResult , MPlaceTy , MemoryKind , Scalar , Writeable , interp_ok,
1213} ;
1314
1415impl < ' tcx > InterpCx < ' tcx , CompileTimeMachine < ' tcx > > {
@@ -64,6 +65,15 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
6465
6566 variant
6667 }
68+ ty:: RawPtr ( ty, mutability) => {
69+ let ( variant, variant_place) = downcast ( sym:: Pointer ) ?;
70+ let pointer_place =
71+ self . project_field ( & variant_place, FieldIdx :: ZERO ) ?;
72+
73+ self . write_pointer_type_info ( pointer_place, * ty, * mutability) ?;
74+
75+ variant
76+ }
6777 // For now just merge all primitives into one `Leaf` variant with no data
6878 ty:: Uint ( _) | ty:: Int ( _) | ty:: Float ( _) | ty:: Char | ty:: Bool => {
6979 downcast ( sym:: Leaf ) ?. 0
@@ -73,7 +83,6 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
7383 | ty:: Str
7484 | ty:: Pat ( _, _)
7585 | ty:: Slice ( _)
76- | ty:: RawPtr ( ..)
7786 | ty:: Ref ( ..)
7887 | ty:: FnDef ( ..)
7988 | ty:: FnPtr ( ..)
@@ -203,4 +212,30 @@ impl<'tcx> InterpCx<'tcx, CompileTimeMachine<'tcx>> {
203212
204213 interp_ok ( ( ) )
205214 }
215+
216+ pub ( crate ) fn write_pointer_type_info (
217+ & mut self ,
218+ place : impl Writeable < ' tcx , CtfeProvenance > ,
219+ ty : Ty < ' tcx > ,
220+ mutability : Mutability ,
221+ ) -> InterpResult < ' tcx > {
222+ // Iterate over all fields of `type_info::Pointer`.
223+ for ( field_idx, field) in
224+ place. layout ( ) . ty . ty_adt_def ( ) . unwrap ( ) . non_enum_variant ( ) . fields . iter_enumerated ( )
225+ {
226+ let field_place = self . project_field ( & place, field_idx) ?;
227+
228+ match field. name {
229+ // Write the `TypeId` of the pointer's inner type to the `ty` field.
230+ sym:: pointee => self . write_type_id ( ty, & field_place) ?,
231+ // Write the boolean representing the pointer's mutability to the `mutable` field.
232+ sym:: mutable => {
233+ self . write_scalar ( Scalar :: from_bool ( mutability. is_mut ( ) ) , & field_place) ?
234+ }
235+ other => span_bug ! ( self . tcx. def_span( field. did) , "unimplemented field {other}" ) ,
236+ }
237+ }
238+
239+ interp_ok ( ( ) )
240+ }
206241}
0 commit comments