@@ -80,8 +80,6 @@ mod item;
80
80
mod pat;
81
81
mod path;
82
82
83
- const HIR_ID_COUNTER_LOCKED : u32 = 0xFFFFFFFF ;
84
-
85
83
rustc_hir:: arena_types!( rustc_arena:: declare_arena, ' tcx) ;
86
84
87
85
struct LoweringContext < ' a , ' hir : ' a > {
@@ -151,7 +149,7 @@ struct LoweringContext<'a, 'hir: 'a> {
151
149
in_scope_lifetimes : Vec < ParamName > ,
152
150
153
151
current_hir_id_owner : ( LocalDefId , u32 ) ,
154
- item_local_id_counters : NodeMap < u32 > ,
152
+ item_local_id_counters : IndexVec < LocalDefId , u32 > ,
155
153
node_id_to_hir_id : IndexVec < NodeId , Option < hir:: HirId > > ,
156
154
157
155
allow_try_trait : Option < Lrc < [ Symbol ] > > ,
@@ -488,52 +486,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
488
486
id
489
487
}
490
488
491
- fn allocate_hir_id_counter ( & mut self , owner : NodeId ) -> LocalDefId {
492
- // Set up the counter if needed.
493
- self . item_local_id_counters . entry ( owner) . or_insert ( 0 ) ;
494
- // Always allocate the first `HirId` for the owner itself.
495
- let lowered = self . lower_node_id_with_owner ( owner, owner) ;
496
- debug_assert_eq ! ( lowered. local_id. as_u32( ) , 0 ) ;
497
- lowered. owner
498
- }
499
-
500
489
fn create_stable_hashing_context ( & self ) -> LoweringHasher < ' _ > {
501
490
LoweringHasher {
502
491
source_map : CachingSourceMapView :: new ( self . sess . source_map ( ) ) ,
503
492
resolver : self . resolver ,
504
493
}
505
494
}
506
495
507
- fn lower_node_id_generic (
508
- & mut self ,
509
- ast_node_id : NodeId ,
510
- alloc_hir_id : impl FnOnce ( & mut Self ) -> hir:: HirId ,
511
- ) -> hir:: HirId {
512
- assert_ne ! ( ast_node_id, DUMMY_NODE_ID ) ;
513
-
514
- let min_size = ast_node_id. as_usize ( ) + 1 ;
515
-
516
- if min_size > self . node_id_to_hir_id . len ( ) {
517
- self . node_id_to_hir_id . resize ( min_size, None ) ;
518
- }
496
+ fn allocate_hir_id_counter ( & mut self , owner : NodeId ) -> LocalDefId {
497
+ // Set up the counter if needed.
498
+ let def_id = self . resolver . local_def_id ( owner) ;
519
499
520
- if let Some ( existing_hir_id) = self . node_id_to_hir_id [ ast_node_id] {
521
- existing_hir_id
500
+ // Always allocate the first `HirId` for the owner itself.
501
+ self . node_id_to_hir_id . ensure_contains_elem ( owner, || None ) ;
502
+ if let Some ( _lowered) = self . node_id_to_hir_id [ owner] {
503
+ debug_assert_eq ! ( _lowered. owner, def_id) ;
504
+ debug_assert_eq ! ( _lowered. local_id. as_u32( ) , 0 ) ;
522
505
} else {
523
- // Generate a new `HirId`.
524
- let hir_id = alloc_hir_id ( self ) ;
525
- self . node_id_to_hir_id [ ast_node_id ] = Some ( hir_id ) ;
506
+ self . item_local_id_counters . ensure_contains_elem ( def_id , || 0 ) ;
507
+ let local_id_counter = & mut self . item_local_id_counters [ def_id ] ;
508
+ let local_id = * local_id_counter ;
526
509
527
- hir_id
510
+ // We want to be sure not to modify the counter in the map while it
511
+ // is also on the stack. Otherwise we'll get lost updates when writing
512
+ // back from the stack to the map.
513
+ debug_assert_eq ! ( local_id, 0 ) ;
514
+
515
+ * local_id_counter += 1 ;
516
+ self . node_id_to_hir_id [ owner] = Some ( hir:: HirId :: make_owner ( def_id) ) ;
528
517
}
518
+ def_id
529
519
}
530
520
531
521
fn with_hir_id_owner < T > ( & mut self , owner : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
532
- let counter = self
533
- . item_local_id_counters
534
- . insert ( owner, HIR_ID_COUNTER_LOCKED )
535
- . unwrap_or_else ( || panic ! ( "no `item_local_id_counters` entry for {:?}" , owner) ) ;
536
522
let def_id = self . resolver . local_def_id ( owner) ;
523
+ let counter = self . item_local_id_counters [ def_id] ;
537
524
let old_owner = std:: mem:: replace ( & mut self . current_hir_id_owner , ( def_id, counter) ) ;
538
525
let ret = f ( self ) ;
539
526
let ( new_def_id, new_counter) =
@@ -542,8 +529,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
542
529
debug_assert ! ( def_id == new_def_id) ;
543
530
debug_assert ! ( new_counter >= counter) ;
544
531
545
- let prev = self . item_local_id_counters . insert ( owner, new_counter) . unwrap ( ) ;
546
- debug_assert ! ( prev == HIR_ID_COUNTER_LOCKED ) ;
532
+ self . item_local_id_counters [ def_id] = new_counter;
547
533
ret
548
534
}
549
535
@@ -554,35 +540,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
554
540
/// `HirIdValidator` later on, which makes sure that all `NodeId`s got mapped
555
541
/// properly. Calling the method twice with the same `NodeId` is fine though.
556
542
fn lower_node_id ( & mut self , ast_node_id : NodeId ) -> hir:: HirId {
557
- self . lower_node_id_generic ( ast_node_id, |this| {
558
- let & mut ( owner, ref mut local_id_counter) = & mut this. current_hir_id_owner ;
559
- let local_id = * local_id_counter;
560
- * local_id_counter += 1 ;
561
- hir:: HirId { owner, local_id : hir:: ItemLocalId :: from_u32 ( local_id) }
562
- } )
563
- }
543
+ assert_ne ! ( ast_node_id, DUMMY_NODE_ID ) ;
564
544
565
- fn lower_node_id_with_owner ( & mut self , ast_node_id : NodeId , owner : NodeId ) -> hir :: HirId {
566
- self . lower_node_id_generic ( ast_node_id, |this| {
567
- let local_id_counter = this
568
- . item_local_id_counters
569
- . get_mut ( & owner )
570
- . expect ( "called `lower_node_id_with_owner` before `allocate_hir_id_counter`" ) ;
545
+ self . node_id_to_hir_id . ensure_contains_elem ( ast_node_id , || None ) ;
546
+ if let Some ( existing_hir_id ) = self . node_id_to_hir_id [ ast_node_id] {
547
+ existing_hir_id
548
+ } else {
549
+ // Generate a new `HirId`.
550
+ let & mut ( owner , ref mut local_id_counter ) = & mut self . current_hir_id_owner ;
571
551
let local_id = * local_id_counter;
572
-
573
- // We want to be sure not to modify the counter in the map while it
574
- // is also on the stack. Otherwise we'll get lost updates when writing
575
- // back from the stack to the map.
576
- debug_assert ! ( local_id != HIR_ID_COUNTER_LOCKED ) ;
577
-
578
552
* local_id_counter += 1 ;
579
- let owner = this. resolver . opt_local_def_id ( owner) . expect (
580
- "you forgot to call `create_def` or are lowering node-IDs \
581
- that do not belong to the current owner",
582
- ) ;
583
-
584
- hir:: HirId { owner, local_id : hir:: ItemLocalId :: from_u32 ( local_id) }
585
- } )
553
+ let hir_id = hir:: HirId { owner, local_id : hir:: ItemLocalId :: from_u32 ( local_id) } ;
554
+ self . node_id_to_hir_id [ ast_node_id] = Some ( hir_id) ;
555
+ hir_id
556
+ }
586
557
}
587
558
588
559
fn next_id ( & mut self ) -> hir:: HirId {
@@ -592,7 +563,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
592
563
593
564
fn lower_res ( & mut self , res : Res < NodeId > ) -> Res {
594
565
res. map_id ( |id| {
595
- self . lower_node_id_generic ( id, |_ | {
566
+ self . node_id_to_hir_id . get ( id) . copied ( ) . flatten ( ) . unwrap_or_else ( | | {
596
567
panic ! ( "expected `NodeId` to be lowered already for res {:#?}" , res) ;
597
568
} )
598
569
} )
0 commit comments