@@ -148,8 +148,8 @@ struct LoweringContext<'a, 'hir: 'a> {
148
148
/// vector.
149
149
in_scope_lifetimes : Vec < ParamName > ,
150
150
151
- current_hir_id_owner : ( LocalDefId , u32 ) ,
152
- item_local_id_counters : IndexVec < LocalDefId , u32 > ,
151
+ current_hir_id_owner : LocalDefId ,
152
+ item_local_id_counter : hir :: ItemLocalId ,
153
153
node_id_to_hir_id : IndexVec < NodeId , Option < hir:: HirId > > ,
154
154
155
155
allow_try_trait : Option < Lrc < [ Symbol ] > > ,
@@ -328,8 +328,8 @@ pub fn lower_crate<'a, 'hir>(
328
328
is_in_trait_impl : false ,
329
329
is_in_dyn_type : false ,
330
330
anonymous_lifetime_mode : AnonymousLifetimeMode :: PassThrough ,
331
- current_hir_id_owner : ( CRATE_DEF_ID , 0 ) ,
332
- item_local_id_counters : Default :: default ( ) ,
331
+ current_hir_id_owner : CRATE_DEF_ID ,
332
+ item_local_id_counter : hir :: ItemLocalId :: new ( 0 ) ,
333
333
node_id_to_hir_id : IndexVec :: new ( ) ,
334
334
generator_kind : None ,
335
335
task_context : None ,
@@ -410,15 +410,15 @@ enum AnonymousLifetimeMode {
410
410
411
411
impl < ' a , ' hir > LoweringContext < ' a , ' hir > {
412
412
fn lower_crate ( mut self , c : & Crate ) -> & ' hir hir:: Crate < ' hir > {
413
- self . lower_node_id ( CRATE_NODE_ID ) ;
414
- debug_assert ! ( self . node_id_to_hir_id[ CRATE_NODE_ID ] == Some ( hir:: CRATE_HIR_ID ) ) ;
413
+ debug_assert_eq ! ( self . resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
415
414
416
415
visit:: walk_crate ( & mut item:: ItemLowerer { lctx : & mut self } , c) ;
417
416
418
- let module = self . arena . alloc ( self . lower_mod ( & c. items , c. span ) ) ;
419
- self . lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
420
- self . owners . ensure_contains_elem ( CRATE_DEF_ID , || None ) ;
421
- self . owners [ CRATE_DEF_ID ] = Some ( hir:: OwnerNode :: Crate ( module) ) ;
417
+ self . with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
418
+ let module = lctx. lower_mod ( & c. items , c. span ) ;
419
+ lctx. lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
420
+ hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
421
+ } ) ;
422
422
423
423
let mut trait_map: FxHashMap < _ , FxHashMap < _ , _ > > = FxHashMap :: default ( ) ;
424
424
for ( k, v) in self . resolver . take_trait_map ( ) . into_iter ( ) {
@@ -454,83 +454,40 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
454
454
self . arena . alloc ( krate)
455
455
}
456
456
457
- fn insert_item ( & mut self , item : hir:: Item < ' hir > ) -> hir:: ItemId {
458
- let id = item. item_id ( ) ;
459
- let item = self . arena . alloc ( item) ;
460
- self . owners . ensure_contains_elem ( id. def_id , || None ) ;
461
- self . owners [ id. def_id ] = Some ( hir:: OwnerNode :: Item ( item) ) ;
462
- id
463
- }
464
-
465
- fn insert_foreign_item ( & mut self , item : hir:: ForeignItem < ' hir > ) -> hir:: ForeignItemId {
466
- let id = item. foreign_item_id ( ) ;
467
- let item = self . arena . alloc ( item) ;
468
- self . owners . ensure_contains_elem ( id. def_id , || None ) ;
469
- self . owners [ id. def_id ] = Some ( hir:: OwnerNode :: ForeignItem ( item) ) ;
470
- id
471
- }
472
-
473
- fn insert_impl_item ( & mut self , item : hir:: ImplItem < ' hir > ) -> hir:: ImplItemId {
474
- let id = item. impl_item_id ( ) ;
475
- let item = self . arena . alloc ( item) ;
476
- self . owners . ensure_contains_elem ( id. def_id , || None ) ;
477
- self . owners [ id. def_id ] = Some ( hir:: OwnerNode :: ImplItem ( item) ) ;
478
- id
479
- }
480
-
481
- fn insert_trait_item ( & mut self , item : hir:: TraitItem < ' hir > ) -> hir:: TraitItemId {
482
- let id = item. trait_item_id ( ) ;
483
- let item = self . arena . alloc ( item) ;
484
- self . owners . ensure_contains_elem ( id. def_id , || None ) ;
485
- self . owners [ id. def_id ] = Some ( hir:: OwnerNode :: TraitItem ( item) ) ;
486
- id
487
- }
488
-
489
457
fn create_stable_hashing_context ( & self ) -> LoweringHasher < ' _ > {
490
458
LoweringHasher {
491
459
source_map : CachingSourceMapView :: new ( self . sess . source_map ( ) ) ,
492
460
resolver : self . resolver ,
493
461
}
494
462
}
495
463
496
- fn allocate_hir_id_counter ( & mut self , owner : NodeId ) -> LocalDefId {
497
- // Set up the counter if needed.
464
+ fn with_hir_id_owner (
465
+ & mut self ,
466
+ owner : NodeId ,
467
+ f : impl FnOnce ( & mut Self ) -> hir:: OwnerNode < ' hir > ,
468
+ ) -> LocalDefId {
498
469
let def_id = self . resolver . local_def_id ( owner) ;
499
470
500
471
// Always allocate the first `HirId` for the owner itself.
501
472
self . node_id_to_hir_id . ensure_contains_elem ( owner, || None ) ;
502
473
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 ) ;
505
- } else {
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;
474
+ panic ! ( "with_hir_id_owner must not be called multiple times on owner {:?}" , def_id) ;
475
+ }
476
+ self . node_id_to_hir_id [ owner] = Some ( hir:: HirId :: make_owner ( def_id) ) ;
509
477
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 ) ;
478
+ let current_owner = std:: mem:: replace ( & mut self . current_hir_id_owner , def_id) ;
479
+ let current_local_counter =
480
+ std:: mem:: replace ( & mut self . item_local_id_counter , hir:: ItemLocalId :: new ( 1 ) ) ;
514
481
515
- * local_id_counter += 1 ;
516
- self . node_id_to_hir_id [ owner] = Some ( hir:: HirId :: make_owner ( def_id) ) ;
517
- }
518
- def_id
519
- }
482
+ let item = f ( self ) ;
520
483
521
- fn with_hir_id_owner < T > ( & mut self , owner : NodeId , f : impl FnOnce ( & mut Self ) -> T ) -> T {
522
- let def_id = self . resolver . local_def_id ( owner) ;
523
- let counter = self . item_local_id_counters [ def_id] ;
524
- let old_owner = std:: mem:: replace ( & mut self . current_hir_id_owner , ( def_id, counter) ) ;
525
- let ret = f ( self ) ;
526
- let ( new_def_id, new_counter) =
527
- std:: mem:: replace ( & mut self . current_hir_id_owner , old_owner) ;
484
+ self . current_hir_id_owner = current_owner;
485
+ self . item_local_id_counter = current_local_counter;
528
486
529
- debug_assert ! ( def_id == new_def_id ) ;
530
- debug_assert ! ( new_counter >= counter ) ;
487
+ self . owners . ensure_contains_elem ( def_id, || None ) ;
488
+ self . owners [ def_id ] = Some ( item ) ;
531
489
532
- self . item_local_id_counters [ def_id] = new_counter;
533
- ret
490
+ def_id
534
491
}
535
492
536
493
/// This method allocates a new `HirId` for the given `NodeId` and stores it in
@@ -547,10 +504,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
547
504
existing_hir_id
548
505
} else {
549
506
// Generate a new `HirId`.
550
- let & mut ( owner, ref mut local_id_counter ) = & mut self . current_hir_id_owner ;
551
- let local_id = * local_id_counter ;
552
- * local_id_counter += 1 ;
553
- let hir_id = hir:: HirId { owner, local_id : hir :: ItemLocalId :: from_u32 ( local_id ) } ;
507
+ let owner = self . current_hir_id_owner ;
508
+ let local_id = self . item_local_id_counter ;
509
+ self . item_local_id_counter . increment_by ( 1 ) ;
510
+ let hir_id = hir:: HirId { owner, local_id } ;
554
511
self . node_id_to_hir_id [ ast_node_id] = Some ( hir_id) ;
555
512
hir_id
556
513
}
@@ -626,7 +583,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
626
583
/// Mark a span as relative to the current owning item.
627
584
fn lower_span ( & self , span : Span ) -> Span {
628
585
if self . sess . opts . debugging_opts . incremental_relative_spans {
629
- span. with_parent ( Some ( self . current_hir_id_owner . 0 ) )
586
+ span. with_parent ( Some ( self . current_hir_id_owner ) )
630
587
} else {
631
588
// Do not make spans relative when not using incremental compilation.
632
589
span
@@ -799,7 +756,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
799
756
// wouldn't have been added yet.
800
757
let generics = this. lower_generics_mut (
801
758
generics,
802
- ImplTraitContext :: Universal ( & mut params, this. current_hir_id_owner . 0 ) ,
759
+ ImplTraitContext :: Universal ( & mut params, this. current_hir_id_owner ) ,
803
760
) ;
804
761
let res = f ( this, & mut params) ;
805
762
( params, ( generics, res) )
@@ -1005,7 +962,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1005
962
}
1006
963
AssocTyConstraintKind :: Bound { ref bounds } => {
1007
964
let mut capturable_lifetimes;
1008
- let mut parent_def_id = self . current_hir_id_owner . 0 ;
965
+ let mut parent_def_id = self . current_hir_id_owner ;
1009
966
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
1010
967
let ( desugar_to_impl_trait, itctx) = match itctx {
1011
968
// We are in the return position:
@@ -1133,7 +1090,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1133
1090
1134
1091
// Construct an AnonConst where the expr is the "ty"'s path.
1135
1092
1136
- let parent_def_id = self . current_hir_id_owner . 0 ;
1093
+ let parent_def_id = self . current_hir_id_owner ;
1137
1094
let node_id = self . resolver . next_node_id ( ) ;
1138
1095
1139
1096
// Add a definition for the in-band const def.
@@ -1399,12 +1356,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1399
1356
// frequently opened issues show.
1400
1357
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: OpaqueTy , span, None ) ;
1401
1358
1402
- let opaque_ty_def_id = self . allocate_hir_id_counter ( opaque_ty_node_id) ;
1359
+ let opaque_ty_def_id = self . resolver . local_def_id ( opaque_ty_node_id) ;
1403
1360
1404
- let collected_lifetimes = self . with_hir_id_owner ( opaque_ty_node_id, move |lctx| {
1361
+ let mut collected_lifetimes = Vec :: new ( ) ;
1362
+ self . with_hir_id_owner ( opaque_ty_node_id, |lctx| {
1405
1363
let hir_bounds = lower_bounds ( lctx) ;
1406
1364
1407
- let collected_lifetimes = lifetimes_from_impl_trait_bounds (
1365
+ collected_lifetimes = lifetimes_from_impl_trait_bounds (
1408
1366
opaque_ty_node_id,
1409
1367
& hir_bounds,
1410
1368
capturable_lifetimes,
@@ -1457,9 +1415,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1457
1415
} ;
1458
1416
1459
1417
trace ! ( "lower_opaque_impl_trait: {:#?}" , opaque_ty_def_id) ;
1460
- lctx. generate_opaque_type ( opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span) ;
1461
-
1462
- collected_lifetimes
1418
+ lctx. generate_opaque_type ( opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span)
1463
1419
} ) ;
1464
1420
1465
1421
let lifetimes =
@@ -1481,7 +1437,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1481
1437
opaque_ty_item : hir:: OpaqueTy < ' hir > ,
1482
1438
span : Span ,
1483
1439
opaque_ty_span : Span ,
1484
- ) {
1440
+ ) -> hir :: OwnerNode < ' hir > {
1485
1441
let opaque_ty_item_kind = hir:: ItemKind :: OpaqueTy ( opaque_ty_item) ;
1486
1442
// Generate an `type Foo = impl Trait;` declaration.
1487
1443
trace ! ( "registering opaque type with id {:#?}" , opaque_ty_id) ;
@@ -1492,11 +1448,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1492
1448
vis : respan ( self . lower_span ( span. shrink_to_lo ( ) ) , hir:: VisibilityKind :: Inherited ) ,
1493
1449
span : self . lower_span ( opaque_ty_span) ,
1494
1450
} ;
1495
-
1496
- // Insert the item into the global item list. This usually happens
1497
- // automatically for all AST items. But this opaque type item
1498
- // does not actually exist in the AST.
1499
- self . insert_item ( opaque_ty_item) ;
1451
+ hir:: OwnerNode :: Item ( self . arena . alloc ( opaque_ty_item) )
1500
1452
}
1501
1453
1502
1454
fn lower_fn_params_to_names ( & mut self , decl : & FnDecl ) -> & ' hir [ Ident ] {
@@ -1565,7 +1517,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1565
1517
if let Some ( ( _, ibty) ) = & mut in_band_ty_params {
1566
1518
this. lower_ty_direct (
1567
1519
& param. ty ,
1568
- ImplTraitContext :: Universal ( ibty, this. current_hir_id_owner . 0 ) ,
1520
+ ImplTraitContext :: Universal ( ibty, this. current_hir_id_owner ) ,
1569
1521
)
1570
1522
} else {
1571
1523
this. lower_ty_direct ( & param. ty , ImplTraitContext :: disallowed ( ) )
@@ -1656,7 +1608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1656
1608
1657
1609
let opaque_ty_span = self . mark_span_with_reason ( DesugaringKind :: Async , span, None ) ;
1658
1610
1659
- let opaque_ty_def_id = self . allocate_hir_id_counter ( opaque_ty_node_id) ;
1611
+ let opaque_ty_def_id = self . resolver . local_def_id ( opaque_ty_node_id) ;
1660
1612
1661
1613
// When we create the opaque type for this async fn, it is going to have
1662
1614
// to capture all the lifetimes involved in the signature (including in the
@@ -1706,7 +1658,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1706
1658
// grow.
1707
1659
let input_lifetimes_count = self . in_scope_lifetimes . len ( ) + self . lifetimes_to_define . len ( ) ;
1708
1660
1709
- let lifetime_params = self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1661
+ let mut lifetime_params = Vec :: new ( ) ;
1662
+ self . with_hir_id_owner ( opaque_ty_node_id, |this| {
1710
1663
// We have to be careful to get elision right here. The
1711
1664
// idea is that we create a lifetime parameter for each
1712
1665
// lifetime in the return type. So, given a return type
@@ -1728,7 +1681,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1728
1681
//
1729
1682
// Note: this must be done after lowering the output type,
1730
1683
// as the output type may introduce new in-band lifetimes.
1731
- let lifetime_params: Vec < ( Span , ParamName ) > = this
1684
+ lifetime_params = this
1732
1685
. in_scope_lifetimes
1733
1686
. iter ( )
1734
1687
. cloned ( )
@@ -1757,9 +1710,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1757
1710
} ;
1758
1711
1759
1712
trace ! ( "exist ty from async fn def id: {:#?}" , opaque_ty_def_id) ;
1760
- this. generate_opaque_type ( opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span) ;
1761
-
1762
- lifetime_params
1713
+ this. generate_opaque_type ( opaque_ty_def_id, opaque_ty_item, span, opaque_ty_span)
1763
1714
} ) ;
1764
1715
1765
1716
// As documented above on the variable
0 commit comments