@@ -7,13 +7,11 @@ use rustc_ast::visit::AssocCtxt;
77use rustc_ast:: * ;
88use rustc_data_structures:: fx:: FxHashMap ;
99use rustc_data_structures:: sorted_map:: SortedMap ;
10- use rustc_data_structures:: steal:: Steal ;
1110use rustc_errors:: struct_span_err;
1211use rustc_hir as hir;
1312use rustc_hir:: def:: { DefKind , Res } ;
14- use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
15- use rustc_index:: vec:: { Idx , IndexVec } ;
16- use rustc_middle:: ty:: { AstOwner , ResolverOutputs , TyCtxt } ;
13+ use rustc_hir:: def_id:: CRATE_DEF_ID ;
14+ use rustc_middle:: ty:: { DefIdTree , ResolverOutputs , TyCtxt } ;
1715use rustc_session:: utils:: NtToTokenstream ;
1816use rustc_span:: source_map:: DesugaringKind ;
1917use rustc_span:: symbol:: { kw, sym, Ident } ;
@@ -24,12 +22,10 @@ use tracing::debug;
2422
2523use std:: iter;
2624
27- pub ( super ) struct ItemLowerer < ' a , ' hir > {
25+ pub ( super ) struct ItemLowerer < ' hir > {
2826 pub ( super ) tcx : TyCtxt < ' hir > ,
2927 pub ( super ) resolver : & ' hir ResolverOutputs ,
3028 pub ( super ) nt_to_tokenstream : NtToTokenstream ,
31- pub ( super ) ast_index : & ' a IndexVec < LocalDefId , Steal < AstOwner > > ,
32- pub ( super ) owners : & ' a mut IndexVec < LocalDefId , hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > > ,
3329}
3430
3531/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -52,13 +48,13 @@ fn add_ty_alias_where_clause(
5248 }
5349}
5450
55- impl < ' a , ' hir > ItemLowerer < ' a , ' hir > {
51+ impl < ' hir > ItemLowerer < ' hir > {
5652 fn with_lctx (
5753 & mut self ,
5854 owner : NodeId ,
5955 f : impl FnOnce ( & mut LoweringContext < ' hir > ) -> hir:: OwnerNode < ' hir > ,
60- ) {
61- let next_node_id = self . resolver . next_node_id ;
56+ ) -> hir :: MaybeOwner < & ' hir hir :: OwnerInfo < ' hir > > {
57+ let current_hir_id_owner = self . resolver . node_id_to_def_id [ & owner ] ;
6258 let mut lctx = LoweringContext {
6359 // Pseudo-globals.
6460 tcx : self . tcx ,
@@ -71,13 +67,15 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7167 bodies : Vec :: new ( ) ,
7268 attrs : SortedMap :: default ( ) ,
7369 children : FxHashMap :: default ( ) ,
74- current_hir_id_owner : CRATE_DEF_ID ,
75- item_local_id_counter : hir:: ItemLocalId :: new ( 0 ) ,
76- node_id_to_local_id : Default :: default ( ) ,
7770 local_id_to_def_id : SortedMap :: new ( ) ,
7871 trait_map : Default :: default ( ) ,
7972 local_node_id_to_def_id : FxHashMap :: default ( ) ,
80- next_node_id,
73+ next_node_id : self . resolver . next_node_id ,
74+
75+ // HirId management.
76+ current_hir_id_owner,
77+ node_id_to_local_id : [ ( owner, hir:: ItemLocalId :: from_u32 ( 0 ) ) ] . into_iter ( ) . collect ( ) ,
78+ item_local_id_counter : hir:: ItemLocalId :: from_u32 ( 1 ) , // 0 is `current_hir_id_owner`.
8179
8280 // Lowering state.
8381 catch_scope : None ,
@@ -93,35 +91,15 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
9391 allow_gen_future : Some ( [ sym:: gen_future] [ ..] . into ( ) ) ,
9492 allow_into_future : Some ( [ sym:: into_future] [ ..] . into ( ) ) ,
9593 } ;
96- lctx. with_hir_id_owner ( owner, |lctx| f ( lctx) ) ;
9794
98- for ( def_id, info) in lctx. children {
99- self . owners . ensure_contains_elem ( def_id, || hir:: MaybeOwner :: Phantom ) ;
100- debug_assert ! ( matches!( self . owners[ def_id] , hir:: MaybeOwner :: Phantom ) ) ;
101- self . owners [ def_id] = info;
102- }
103- }
95+ let item = f ( & mut lctx) ;
96+ debug_assert_eq ! ( current_hir_id_owner, item. def_id( ) ) ;
10497
105- pub ( super ) fn lower_node (
106- & mut self ,
107- def_id : LocalDefId ,
108- ) -> hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > {
109- self . owners . ensure_contains_elem ( def_id, || hir:: MaybeOwner :: Phantom ) ;
110- if let hir:: MaybeOwner :: Phantom = self . owners [ def_id] {
111- let node = self . ast_index [ def_id] . steal ( ) ;
112- match node {
113- AstOwner :: NonOwner | AstOwner :: Synthetic ( ..) => { }
114- AstOwner :: Crate ( c) => self . lower_crate ( & c) ,
115- AstOwner :: Item ( item) => self . lower_item ( & item) ,
116- AstOwner :: AssocItem ( item, ctxt) => self . lower_assoc_item ( & item, ctxt) ,
117- AstOwner :: ForeignItem ( item) => self . lower_foreign_item ( & item) ,
118- }
119- }
120-
121- self . owners [ def_id]
98+ let info = lctx. make_owner_info ( item) ;
99+ hir:: MaybeOwner :: Owner ( self . tcx . hir_arena . alloc ( info) )
122100 }
123101
124- fn lower_crate ( & mut self , c : & Crate ) {
102+ pub ( super ) fn lower_crate ( & mut self , c : & Crate ) -> hir :: MaybeOwner < & ' hir hir :: OwnerInfo < ' hir > > {
125103 debug_assert_eq ! ( self . resolver. node_id_to_def_id[ & CRATE_NODE_ID ] , CRATE_DEF_ID ) ;
126104 self . with_lctx ( CRATE_NODE_ID , |lctx| {
127105 let module = lctx. lower_mod ( & c. items , & c. spans ) ;
@@ -130,25 +108,28 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
130108 } )
131109 }
132110
133- fn lower_item ( & mut self , item : & Item ) {
111+ pub ( super ) fn lower_item (
112+ & mut self ,
113+ item : & Item ,
114+ ) -> hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > {
134115 self . with_lctx ( item. id , |lctx| hir:: OwnerNode :: Item ( lctx. lower_item ( item) ) )
135116 }
136117
137- fn lower_assoc_item ( & mut self , item : & AssocItem , ctxt : AssocCtxt ) {
118+ pub ( super ) fn lower_assoc_item (
119+ & mut self ,
120+ item : & AssocItem ,
121+ ctxt : AssocCtxt ,
122+ ) -> hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > {
138123 let def_id = self . resolver . node_id_to_def_id [ & item. id ] ;
139124
140- let parent_id = {
141- let parent = self . tcx . hir ( ) . def_key ( def_id) . parent ;
142- let local_def_index = parent. unwrap ( ) ;
143- LocalDefId { local_def_index }
144- } ;
125+ let parent_id = self . tcx . local_parent ( def_id) . unwrap ( ) ;
145126
146- let parent_hir = self . lower_node ( parent_id ) . unwrap ( ) ;
127+ let parent_hir = self . tcx . hir ( ) . expect_item ( parent_id ) ;
147128 self . with_lctx ( item. id , |lctx| {
148129 // Evaluate with the lifetimes in `params` in-scope.
149130 // This is used to track which lifetimes have already been defined,
150131 // and which need to be replicated when lowering an async fn.
151- match parent_hir. node ( ) . expect_item ( ) . kind {
132+ match parent_hir. kind {
152133 hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, .. } ) => {
153134 lctx. is_in_trait_impl = of_trait. is_some ( ) ;
154135 }
@@ -162,7 +143,10 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
162143 } )
163144 }
164145
165- fn lower_foreign_item ( & mut self , item : & ForeignItem ) {
146+ pub ( super ) fn lower_foreign_item (
147+ & mut self ,
148+ item : & ForeignItem ,
149+ ) -> hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > {
166150 self . with_lctx ( item. id , |lctx| hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) ) )
167151 }
168152}
@@ -545,7 +529,7 @@ impl<'hir> LoweringContext<'hir> {
545529 let path = this. lower_path_extra ( res, & path, ParamMode :: Explicit ) ;
546530 let kind = hir:: ItemKind :: Use ( path, hir:: UseKind :: Single ) ;
547531 if let Some ( attrs) = attrs {
548- this. attrs . insert ( hir:: ItemLocalId :: new ( 0 ) , attrs) ;
532+ this. attrs . insert ( hir:: ItemLocalId :: from_u32 ( 0 ) , attrs) ;
549533 }
550534
551535 let item = hir:: Item {
@@ -619,7 +603,7 @@ impl<'hir> LoweringContext<'hir> {
619603 let kind =
620604 this. lower_use_tree ( use_tree, & prefix, id, vis_span, & mut ident, attrs) ;
621605 if let Some ( attrs) = attrs {
622- this. attrs . insert ( hir:: ItemLocalId :: new ( 0 ) , attrs) ;
606+ this. attrs . insert ( hir:: ItemLocalId :: from_u32 ( 0 ) , attrs) ;
623607 }
624608
625609 let item = hir:: Item {
0 commit comments