@@ -133,6 +133,10 @@ struct LoweringContext<'a, 'hir: 'a> {
133
133
/// NodeIds that are lowered inside the current HIR owner.
134
134
node_id_to_local_id : FxHashMap < NodeId , hir:: ItemLocalId > ,
135
135
136
+ // The next_node_id is reset for each item.
137
+ next_node_id : ast:: NodeId ,
138
+ local_node_id_to_def_id : FxHashMap < ast:: NodeId , LocalDefId > ,
139
+
136
140
allow_try_trait : Option < Lrc < [ Symbol ] > > ,
137
141
allow_gen_future : Option < Lrc < [ Symbol ] > > ,
138
142
allow_into_future : Option < Lrc < [ Symbol ] > > ,
@@ -167,9 +171,6 @@ trait ResolverAstLoweringExt {
167
171
fn get_label_res ( & self , id : NodeId ) -> Option < NodeId > ;
168
172
fn get_lifetime_res ( & self , id : NodeId ) -> Option < LifetimeRes > ;
169
173
fn take_extra_lifetime_params ( & mut self , id : NodeId ) -> Vec < ( Ident , NodeId , LifetimeRes ) > ;
170
- fn next_node_id ( & mut self ) -> NodeId ;
171
- fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > ;
172
- fn local_def_id ( & self , node : NodeId ) -> LocalDefId ;
173
174
fn decl_macro_kind ( & self , def_id : LocalDefId ) -> MacroKind ;
174
175
}
175
176
@@ -229,24 +230,6 @@ impl ResolverAstLoweringExt for ResolverOutputs {
229
230
self . extra_lifetime_params_map . remove ( & id) . unwrap_or_default ( )
230
231
}
231
232
232
- fn next_node_id ( & mut self ) -> NodeId {
233
- let next = self
234
- . next_node_id
235
- . as_usize ( )
236
- . checked_add ( 1 )
237
- . expect ( "input too large; ran out of NodeIds" ) ;
238
- self . next_node_id = NodeId :: from_usize ( next) ;
239
- self . next_node_id
240
- }
241
-
242
- fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
243
- self . node_id_to_def_id . get ( & node) . copied ( )
244
- }
245
-
246
- fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
247
- self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
248
- }
249
-
250
233
fn decl_macro_kind ( & self , def_id : LocalDefId ) -> MacroKind {
251
234
self . builtin_macro_kinds . get ( & def_id) . copied ( ) . unwrap_or ( MacroKind :: Bang )
252
235
}
@@ -359,17 +342,17 @@ enum AstOwner<'a> {
359
342
}
360
343
361
344
fn index_crate < ' a > (
362
- resolver : & ResolverOutputs ,
345
+ node_id_to_def_id : & FxHashMap < NodeId , LocalDefId > ,
363
346
krate : & ' a Crate ,
364
347
) -> IndexVec < LocalDefId , AstOwner < ' a > > {
365
- let mut indexer = Indexer { resolver , index : IndexVec :: new ( ) } ;
348
+ let mut indexer = Indexer { node_id_to_def_id , index : IndexVec :: new ( ) } ;
366
349
indexer. index . ensure_contains_elem ( CRATE_DEF_ID , || AstOwner :: NonOwner ) ;
367
350
indexer. index [ CRATE_DEF_ID ] = AstOwner :: Crate ( krate) ;
368
351
visit:: walk_crate ( & mut indexer, krate) ;
369
352
return indexer. index ;
370
353
371
354
struct Indexer < ' s , ' a > {
372
- resolver : & ' s ResolverOutputs ,
355
+ node_id_to_def_id : & ' s FxHashMap < NodeId , LocalDefId > ,
373
356
index : IndexVec < LocalDefId , AstOwner < ' a > > ,
374
357
}
375
358
@@ -380,21 +363,21 @@ fn index_crate<'a>(
380
363
}
381
364
382
365
fn visit_item ( & mut self , item : & ' a ast:: Item ) {
383
- let def_id = self . resolver . local_def_id ( item. id ) ;
366
+ let def_id = self . node_id_to_def_id [ & item. id ] ;
384
367
self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
385
368
self . index [ def_id] = AstOwner :: Item ( item) ;
386
369
visit:: walk_item ( self , item)
387
370
}
388
371
389
372
fn visit_assoc_item ( & mut self , item : & ' a ast:: AssocItem , ctxt : visit:: AssocCtxt ) {
390
- let def_id = self . resolver . local_def_id ( item. id ) ;
373
+ let def_id = self . node_id_to_def_id [ & item. id ] ;
391
374
self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
392
375
self . index [ def_id] = AstOwner :: AssocItem ( item, ctxt) ;
393
376
visit:: walk_assoc_item ( self , item, ctxt) ;
394
377
}
395
378
396
379
fn visit_foreign_item ( & mut self , item : & ' a ast:: ForeignItem ) {
397
- let def_id = self . resolver . local_def_id ( item. id ) ;
380
+ let def_id = self . node_id_to_def_id [ & item. id ] ;
398
381
self . index . ensure_contains_elem ( def_id, || AstOwner :: NonOwner ) ;
399
382
self . index [ def_id] = AstOwner :: ForeignItem ( item) ;
400
383
visit:: walk_foreign_item ( self , item) ;
@@ -436,7 +419,7 @@ pub fn lower_crate<'hir>(
436
419
) -> & ' hir hir:: Crate < ' hir > {
437
420
let _prof_timer = sess. prof . verbose_generic_activity ( "hir_lowering" ) ;
438
421
439
- let ast_index = index_crate ( resolver, krate) ;
422
+ let ast_index = index_crate ( & resolver. node_id_to_def_id , krate) ;
440
423
441
424
let mut owners =
442
425
IndexVec :: from_fn_n ( |_| hir:: MaybeOwner :: Phantom , definitions. def_index_count ( ) ) ;
@@ -488,11 +471,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
488
471
span : Span ,
489
472
) -> LocalDefId {
490
473
assert ! (
491
- ! self . resolver . node_id_to_def_id . contains_key ( & node_id) ,
474
+ self . opt_local_def_id ( node_id) . is_none ( ) ,
492
475
"adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
493
476
node_id,
494
477
data,
495
- self . definitions. def_key( self . resolver . node_id_to_def_id [ & node_id] ) ,
478
+ self . definitions. def_key( self . local_def_id ( node_id) ) ,
496
479
) ;
497
480
498
481
let def_id = self . definitions . create_def ( parent, data, expn_id, span) ;
@@ -502,20 +485,41 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
502
485
// we don't need a mapping from `NodeId` to `LocalDefId`.
503
486
if node_id != ast:: DUMMY_NODE_ID {
504
487
debug ! ( "create_def: def_id_to_node_id[{:?}] <-> {:?}" , def_id, node_id) ;
505
- self . resolver . node_id_to_def_id . insert ( node_id, def_id) ;
488
+ self . local_node_id_to_def_id . insert ( node_id, def_id) ;
506
489
}
507
- assert_eq ! ( self . resolver. def_id_to_node_id. push( node_id) , def_id) ;
508
490
509
491
def_id
510
492
}
511
493
494
+ fn next_node_id ( & mut self ) -> NodeId {
495
+ let next = self
496
+ . next_node_id
497
+ . as_usize ( )
498
+ . checked_add ( 1 )
499
+ . expect ( "input too large; ran out of NodeIds" ) ;
500
+ self . next_node_id = NodeId :: from_usize ( next) ;
501
+ self . next_node_id
502
+ }
503
+
504
+ fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
505
+ if node <= self . resolver . next_node_id {
506
+ self . resolver . node_id_to_def_id . get ( & node) . copied ( )
507
+ } else {
508
+ self . local_node_id_to_def_id . get ( & node) . copied ( )
509
+ }
510
+ }
511
+
512
+ fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
513
+ self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
514
+ }
515
+
512
516
#[ instrument( level = "debug" , skip( self , f) ) ]
513
517
fn with_hir_id_owner (
514
518
& mut self ,
515
519
owner : NodeId ,
516
520
f : impl FnOnce ( & mut Self ) -> hir:: OwnerNode < ' hir > ,
517
521
) {
518
- let def_id = self . resolver . local_def_id ( owner) ;
522
+ let def_id = self . local_def_id ( owner) ;
519
523
520
524
let current_attrs = std:: mem:: take ( & mut self . attrs ) ;
521
525
let current_bodies = std:: mem:: take ( & mut self . bodies ) ;
@@ -527,6 +531,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
527
531
std:: mem:: replace ( & mut self . item_local_id_counter , hir:: ItemLocalId :: new ( 1 ) ) ;
528
532
let current_impl_trait_defs = std:: mem:: take ( & mut self . impl_trait_defs ) ;
529
533
let current_impl_trait_bounds = std:: mem:: take ( & mut self . impl_trait_bounds ) ;
534
+ // Do not reset `next_node_id` and `node_id_to_def_id` as we want to refer to the
535
+ // subdefinitions' nodes.
530
536
531
537
// Always allocate the first `HirId` for the owner itself.
532
538
let _old = self . node_id_to_local_id . insert ( owner, hir:: ItemLocalId :: new ( 0 ) ) ;
@@ -633,7 +639,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
633
639
self . item_local_id_counter . increment_by ( 1 ) ;
634
640
635
641
assert_ne ! ( local_id, hir:: ItemLocalId :: new( 0 ) ) ;
636
- if let Some ( def_id) = self . resolver . opt_local_def_id ( ast_node_id) {
642
+ if let Some ( def_id) = self . opt_local_def_id ( ast_node_id) {
637
643
// Do not override a `MaybeOwner::Owner` that may already here.
638
644
self . children . entry ( def_id) . or_insert ( hir:: MaybeOwner :: NonOwner ( hir_id) ) ;
639
645
self . local_id_to_def_id . insert ( local_id, def_id) ;
@@ -649,7 +655,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
649
655
}
650
656
651
657
fn next_id ( & mut self ) -> hir:: HirId {
652
- let node_id = self . resolver . next_node_id ( ) ;
658
+ let node_id = self . next_node_id ( ) ;
653
659
self . lower_node_id ( node_id)
654
660
}
655
661
@@ -991,7 +997,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
991
997
// constructing the HIR for `impl bounds...` and then lowering that.
992
998
993
999
let parent_def_id = self . current_hir_id_owner ;
994
- let impl_trait_node_id = self . resolver . next_node_id ( ) ;
1000
+ let impl_trait_node_id = self . next_node_id ( ) ;
995
1001
self . create_def (
996
1002
parent_def_id,
997
1003
impl_trait_node_id,
@@ -1001,7 +1007,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1001
1007
) ;
1002
1008
1003
1009
self . with_dyn_type_scope ( false , |this| {
1004
- let node_id = this. resolver . next_node_id ( ) ;
1010
+ let node_id = this. next_node_id ( ) ;
1005
1011
let ty = this. lower_ty (
1006
1012
& Ty {
1007
1013
id : node_id,
@@ -1101,7 +1107,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1101
1107
// Construct an AnonConst where the expr is the "ty"'s path.
1102
1108
1103
1109
let parent_def_id = self . current_hir_id_owner ;
1104
- let node_id = self . resolver . next_node_id ( ) ;
1110
+ let node_id = self . next_node_id ( ) ;
1105
1111
1106
1112
// Add a definition for the in-band const def.
1107
1113
self . create_def (
@@ -1180,7 +1186,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1180
1186
debug_assert_eq ! ( start. plus( 1 ) , end) ;
1181
1187
start
1182
1188
} else {
1183
- self . resolver . next_node_id ( )
1189
+ self . next_node_id ( )
1184
1190
} ;
1185
1191
let span = self . sess . source_map ( ) . next_point ( t. span . shrink_to_lo ( ) ) ;
1186
1192
Lifetime { ident : Ident :: new ( kw:: UnderscoreLifetime , span) , id }
@@ -1323,7 +1329,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1323
1329
// frequently opened issues show.
1324
1330
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: OpaqueTy , span, None ) ;
1325
1331
1326
- let opaque_ty_def_id = self . resolver . local_def_id ( opaque_ty_node_id) ;
1332
+ let opaque_ty_def_id = self . local_def_id ( opaque_ty_node_id) ;
1327
1333
1328
1334
let mut collected_lifetimes = FxHashMap :: default ( ) ;
1329
1335
self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
@@ -1341,7 +1347,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1341
1347
let lifetime_defs = lctx. arena . alloc_from_iter ( collected_lifetimes. iter ( ) . map (
1342
1348
|( _, & ( span, p_id, p_name, _) ) | {
1343
1349
let hir_id = lctx. lower_node_id ( p_id) ;
1344
- debug_assert_ne ! ( lctx. resolver . opt_local_def_id( p_id) , None ) ;
1350
+ debug_assert_ne ! ( lctx. opt_local_def_id( p_id) , None ) ;
1345
1351
1346
1352
let kind = if p_name. ident ( ) . name == kw:: UnderscoreLifetime {
1347
1353
hir:: LifetimeParamKind :: Elided
@@ -1380,7 +1386,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1380
1386
1381
1387
let lifetimes = self . arena . alloc_from_iter ( collected_lifetimes. into_iter ( ) . map (
1382
1388
|( _, ( span, _, p_name, res) ) | {
1383
- let id = self . resolver . next_node_id ( ) ;
1389
+ let id = self . next_node_id ( ) ;
1384
1390
let ident = Ident :: new ( p_name. ident ( ) . name , span) ;
1385
1391
let l = self . new_named_lifetime_with_res ( id, span, ident, res) ;
1386
1392
hir:: GenericArg :: Lifetime ( l)
@@ -1489,7 +1495,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1489
1495
FnRetTy :: Ty ( ref ty) => {
1490
1496
let context = match fn_node_id {
1491
1497
Some ( fn_node_id) if kind. impl_trait_return_allowed ( ) => {
1492
- let fn_def_id = self . resolver . local_def_id ( fn_node_id) ;
1498
+ let fn_def_id = self . local_def_id ( fn_node_id) ;
1493
1499
ImplTraitContext :: ReturnPositionOpaqueTy {
1494
1500
origin : hir:: OpaqueTyOrigin :: FnReturn ( fn_def_id) ,
1495
1501
}
@@ -1563,8 +1569,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1563
1569
1564
1570
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: Async , span, None ) ;
1565
1571
1566
- let opaque_ty_def_id = self . resolver . local_def_id ( opaque_ty_node_id) ;
1567
- let fn_def_id = self . resolver . local_def_id ( fn_node_id) ;
1572
+ let opaque_ty_def_id = self . local_def_id ( opaque_ty_node_id) ;
1573
+ let fn_def_id = self . local_def_id ( fn_node_id) ;
1568
1574
1569
1575
// When we create the opaque type for this async fn, it is going to have
1570
1576
// to capture all the lifetimes involved in the signature (including in the
@@ -1614,8 +1620,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1614
1620
debug ! ( ?extra_lifetime_params) ;
1615
1621
for ( ident, outer_node_id, outer_res) in extra_lifetime_params {
1616
1622
let Ident { name, span } = ident;
1617
- let outer_def_id = self . resolver . local_def_id ( outer_node_id) ;
1618
- let inner_node_id = self . resolver . next_node_id ( ) ;
1623
+ let outer_def_id = self . local_def_id ( outer_node_id) ;
1624
+ let inner_node_id = self . next_node_id ( ) ;
1619
1625
1620
1626
// Add a definition for the in scope lifetime def.
1621
1627
self . create_def (
@@ -1665,7 +1671,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1665
1671
let generic_params =
1666
1672
this. arena . alloc_from_iter ( captures. iter ( ) . map ( |( _, & ( span, p_id, p_name, _) ) | {
1667
1673
let hir_id = this. lower_node_id ( p_id) ;
1668
- debug_assert_ne ! ( this. resolver . opt_local_def_id( p_id) , None ) ;
1674
+ debug_assert_ne ! ( this. opt_local_def_id( p_id) , None ) ;
1669
1675
1670
1676
let kind = if p_name. ident ( ) . name == kw:: UnderscoreLifetime {
1671
1677
hir:: LifetimeParamKind :: Elided
@@ -1717,7 +1723,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1717
1723
// generate `'_`.
1718
1724
let generic_args =
1719
1725
self . arena . alloc_from_iter ( captures. into_iter ( ) . map ( |( _, ( span, _, p_name, res) ) | {
1720
- let id = self . resolver . next_node_id ( ) ;
1726
+ let id = self . next_node_id ( ) ;
1721
1727
let ident = Ident :: new ( p_name. ident ( ) . name , span) ;
1722
1728
let l = self . new_named_lifetime_with_res ( id, span, ident, res) ;
1723
1729
hir:: GenericArg :: Lifetime ( l)
@@ -1811,9 +1817,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1811
1817
if let Some ( mut captured_lifetimes) = self . captured_lifetimes . take ( ) {
1812
1818
if !captured_lifetimes. binders_to_ignore . contains ( & binder) {
1813
1819
match captured_lifetimes. captures . entry ( param) {
1814
- Entry :: Occupied ( o) => param = self . resolver . local_def_id ( o. get ( ) . 1 ) ,
1820
+ Entry :: Occupied ( o) => param = self . local_def_id ( o. get ( ) . 1 ) ,
1815
1821
Entry :: Vacant ( v) => {
1816
- let p_id = self . resolver . next_node_id ( ) ;
1822
+ let p_id = self . next_node_id ( ) ;
1817
1823
let p_def_id = self . create_def (
1818
1824
captured_lifetimes. parent_def_id ,
1819
1825
p_id,
@@ -1837,9 +1843,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1837
1843
if let Some ( mut captured_lifetimes) = self . captured_lifetimes . take ( ) {
1838
1844
if !captured_lifetimes. binders_to_ignore . contains ( & binder) {
1839
1845
match captured_lifetimes. captures . entry ( param) {
1840
- Entry :: Occupied ( o) => param = self . resolver . local_def_id ( o. get ( ) . 1 ) ,
1846
+ Entry :: Occupied ( o) => param = self . local_def_id ( o. get ( ) . 1 ) ,
1841
1847
Entry :: Vacant ( v) => {
1842
- let p_id = self . resolver . next_node_id ( ) ;
1848
+ let p_id = self . next_node_id ( ) ;
1843
1849
let p_def_id = self . create_def (
1844
1850
captured_lifetimes. parent_def_id ,
1845
1851
p_id,
@@ -1862,7 +1868,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1862
1868
let mut l_name = None ;
1863
1869
if let Some ( mut captured_lifetimes) = self . captured_lifetimes . take ( ) {
1864
1870
if !captured_lifetimes. binders_to_ignore . contains ( & binder) {
1865
- let p_id = self . resolver . next_node_id ( ) ;
1871
+ let p_id = self . next_node_id ( ) ;
1866
1872
let p_def_id = self . create_def (
1867
1873
captured_lifetimes. parent_def_id ,
1868
1874
p_id,
@@ -2011,7 +2017,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2011
2017
bounds : & [ GenericBound ] ,
2012
2018
) -> ( hir:: GenericParam < ' hir > , Option < hir:: WherePredicate < ' hir > > , hir:: TyKind < ' hir > ) {
2013
2019
// Add a definition for the in-band `Param`.
2014
- let def_id = self . resolver . local_def_id ( node_id) ;
2020
+ let def_id = self . local_def_id ( node_id) ;
2015
2021
2016
2022
let hir_bounds = self . lower_param_bounds ( bounds, ImplTraitContext :: Universal ) ;
2017
2023
// Set the name to `impl Bound1 + Bound2`.
0 commit comments