@@ -46,13 +46,12 @@ pub trait CompileTimeMachine<'tcx, T> = Machine<
4646pub trait HasStaticRootDefId {
4747 /// Returns the `DefId` of the static item that is currently being evaluated.
4848 /// Used for interning to be able to handle nested allocations.
49- fn static_parent_and_disambiguator ( & mut self ) -> Option < ( LocalDefId , & mut DisambiguatorState ) > ;
49+ fn static_def_id ( & self ) -> Option < LocalDefId > ;
5050}
5151
5252impl HasStaticRootDefId for const_eval:: CompileTimeMachine < ' _ > {
53- fn static_parent_and_disambiguator ( & mut self ) -> Option < ( LocalDefId , & mut DisambiguatorState ) > {
54- let ( _, static_id, d) = self . static_root_ids . as_mut ( ) ?;
55- Some ( ( * static_id, d) )
53+ fn static_def_id ( & self ) -> Option < LocalDefId > {
54+ Some ( self . static_root_ids ?. 1 )
5655 }
5756}
5857
@@ -67,6 +66,7 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
6766 ecx : & mut InterpCx < ' tcx , M > ,
6867 alloc_id : AllocId ,
6968 mutability : Mutability ,
69+ disambiguator : Option < & mut DisambiguatorState > ,
7070) -> Result < impl Iterator < Item = CtfeProvenance > + ' tcx , ( ) > {
7171 trace ! ( "intern_shallow {:?}" , alloc_id) ;
7272 // remove allocation
@@ -88,8 +88,14 @@ fn intern_shallow<'tcx, T, M: CompileTimeMachine<'tcx, T>>(
8888 }
8989 // link the alloc id to the actual allocation
9090 let alloc = ecx. tcx . mk_const_alloc ( alloc) ;
91- if let Some ( ( static_id, disambiguator) ) = ecx. machine . static_parent_and_disambiguator ( ) {
92- intern_as_new_static ( ecx. tcx , static_id, alloc_id, alloc, disambiguator) ;
91+ if let Some ( static_id) = ecx. machine . static_def_id ( ) {
92+ intern_as_new_static (
93+ ecx. tcx ,
94+ static_id,
95+ alloc_id,
96+ alloc,
97+ disambiguator. expect ( "disambiguator needed" ) ,
98+ ) ;
9399 } else {
94100 ecx. tcx . set_alloc_id_memory ( alloc_id, alloc) ;
95101 }
@@ -105,6 +111,10 @@ fn intern_as_new_static<'tcx>(
105111 alloc : ConstAllocation < ' tcx > ,
106112 disambiguator : & mut DisambiguatorState ,
107113) {
114+ // `intern_const_alloc_recursive` is called once per static and it contains the `DisambiguatorState`.
115+ // The `<static_id>::{{nested}}` path is thus unique to `intern_const_alloc_recursive` and the
116+ // `DisambiguatorState` ensures the generated path is unique for this call as we generate
117+ // `<static_id>::{{nested#n}}` where `n` is the `n`th `intern_as_new_static` call.
108118 let feed = tcx. create_def (
109119 static_id,
110120 None ,
@@ -158,6 +168,8 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
158168 intern_kind : InternKind ,
159169 ret : & MPlaceTy < ' tcx > ,
160170) -> Result < ( ) , InternResult > {
171+ let mut disambiguator = DisambiguatorState :: new ( ) ;
172+
161173 // We are interning recursively, and for mutability we are distinguishing the "root" allocation
162174 // that we are starting in, and all other allocations that we are encountering recursively.
163175 let ( base_mutability, inner_mutability, is_static) = match intern_kind {
@@ -201,7 +213,9 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
201213 alloc. 1 . mutability = base_mutability;
202214 alloc. 1 . provenance ( ) . ptrs ( ) . iter ( ) . map ( |& ( _, prov) | prov) . collect ( )
203215 } else {
204- intern_shallow ( ecx, base_alloc_id, base_mutability) . unwrap ( ) . collect ( )
216+ intern_shallow ( ecx, base_alloc_id, base_mutability, Some ( & mut disambiguator) )
217+ . unwrap ( )
218+ . collect ( )
205219 } ;
206220 // We need to distinguish "has just been interned" from "was already in `tcx`",
207221 // so we track this in a separate set.
@@ -295,7 +309,7 @@ pub fn intern_const_alloc_recursive<'tcx, M: CompileTimeMachine<'tcx, const_eval
295309 // okay with losing some potential for immutability here. This can anyway only affect
296310 // `static mut`.
297311 just_interned. insert ( alloc_id) ;
298- match intern_shallow ( ecx, alloc_id, inner_mutability) {
312+ match intern_shallow ( ecx, alloc_id, inner_mutability, Some ( & mut disambiguator ) ) {
299313 Ok ( nested) => todo. extend ( nested) ,
300314 Err ( ( ) ) => {
301315 ecx. tcx . dcx ( ) . delayed_bug ( "found dangling pointer during const interning" ) ;
@@ -317,8 +331,9 @@ pub fn intern_const_alloc_for_constprop<'tcx, T, M: CompileTimeMachine<'tcx, T>>
317331 return interp_ok ( ( ) ) ;
318332 }
319333 // Move allocation to `tcx`.
320- if let Some ( _) =
321- ( intern_shallow ( ecx, alloc_id, Mutability :: Not ) . map_err ( |( ) | err_ub ! ( DeadLocal ) ) ?) . next ( )
334+ if let Some ( _) = intern_shallow ( ecx, alloc_id, Mutability :: Not , None )
335+ . map_err ( |( ) | err_ub ! ( DeadLocal ) ) ?
336+ . next ( )
322337 {
323338 // We are not doing recursive interning, so we don't currently support provenance.
324339 // (If this assertion ever triggers, we should just implement a
@@ -344,7 +359,7 @@ impl<'tcx> InterpCx<'tcx, DummyMachine> {
344359 let dest = self . allocate ( layout, MemoryKind :: Stack ) ?;
345360 f ( self , & dest. clone ( ) . into ( ) ) ?;
346361 let alloc_id = dest. ptr ( ) . provenance . unwrap ( ) . alloc_id ( ) ; // this was just allocated, it must have provenance
347- for prov in intern_shallow ( self , alloc_id, Mutability :: Not ) . unwrap ( ) {
362+ for prov in intern_shallow ( self , alloc_id, Mutability :: Not , None ) . unwrap ( ) {
348363 // We are not doing recursive interning, so we don't currently support provenance.
349364 // (If this assertion ever triggers, we should just implement a
350365 // proper recursive interning loop -- or just call `intern_const_alloc_recursive`.
0 commit comments