11use super :: { AnonymousLifetimeMode , LoweringContext , ParamMode } ;
2- use super :: { ImplTraitContext , ImplTraitPosition } ;
2+ use super :: { AstOwner , ImplTraitContext , ImplTraitPosition } ;
33use crate :: { Arena , FnDeclKind } ;
44
55use rustc_ast:: ptr:: P ;
6- use rustc_ast:: visit:: { self , AssocCtxt , FnCtxt , FnKind , Visitor } ;
6+ use rustc_ast:: visit:: AssocCtxt ;
77use rustc_ast:: * ;
88use rustc_data_structures:: fx:: FxHashSet ;
99use rustc_errors:: struct_span_err;
1010use rustc_hir as hir;
1111use rustc_hir:: def:: { DefKind , Res } ;
12- use rustc_hir:: def_id:: LocalDefId ;
13- use rustc_index:: vec:: Idx ;
12+ use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
13+ use rustc_index:: vec:: { Idx , IndexVec } ;
1414use rustc_span:: source_map:: { respan, DesugaringKind } ;
1515use rustc_span:: symbol:: { kw, sym, Ident } ;
1616use rustc_span:: Span ;
@@ -23,6 +23,7 @@ use std::mem;
2323
2424pub ( super ) struct ItemLowerer < ' a , ' lowering , ' hir > {
2525 pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
26+ pub ( super ) ast_index : & ' a IndexVec < LocalDefId , AstOwner < ' lowering > > ,
2627}
2728
2829/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -45,7 +46,7 @@ fn add_ty_alias_where_clause(
4546 }
4647}
4748
48- impl ItemLowerer < ' _ , ' _ , ' _ > {
49+ impl < ' a , ' hir > ItemLowerer < ' _ , ' a , ' hir > {
4950 /// Clears (and restores) the `in_scope_lifetimes` field. Used when
5051 /// visiting nested items, which never inherit in-scope lifetimes
5152 /// from their surrounding environment.
@@ -73,10 +74,9 @@ impl ItemLowerer<'_, '_, '_> {
7374 /// for them.
7475 fn with_parent_item_lifetime_defs (
7576 & mut self ,
76- parent_item : LocalDefId ,
77+ parent_hir : & ' hir hir :: Item < ' hir > ,
7778 f : impl FnOnce ( & mut Self ) ,
7879 ) {
79- let parent_hir = self . lctx . owners [ parent_item] . unwrap ( ) . node ( ) . expect_item ( ) ;
8080 let parent_generics = match parent_hir. kind {
8181 hir:: ItemKind :: Impl ( hir:: Impl { ref generics, .. } )
8282 | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => generics. params ,
@@ -98,65 +98,81 @@ impl ItemLowerer<'_, '_, '_> {
9898 self . lctx . in_scope_lifetimes = old_in_scope_lifetimes;
9999 }
100100
101- fn with_trait_impl_ref ( & mut self , impl_ref : & Option < TraitRef > , f : impl FnOnce ( & mut Self ) ) {
101+ fn with_trait_impl_ref (
102+ & mut self ,
103+ impl_ref : & Option < hir:: TraitRef < ' _ > > ,
104+ f : impl FnOnce ( & mut Self ) ,
105+ ) {
102106 let old = self . lctx . is_in_trait_impl ;
103107 self . lctx . is_in_trait_impl = impl_ref. is_some ( ) ;
104108 let ret = f ( self ) ;
105109 self . lctx . is_in_trait_impl = old;
106110 ret
107111 }
108- }
109112
110- impl < ' a > Visitor < ' a > for ItemLowerer < ' a , ' _ , ' _ > {
111- fn visit_attribute ( & mut self , _: & ' a Attribute ) {
112- // We do not want to lower expressions that appear in attributes,
113- // as they are not accessible to the rest of the HIR.
113+ pub ( super ) fn lower_node (
114+ & mut self ,
115+ def_id : LocalDefId ,
116+ ) -> hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > {
117+ self . lctx . owners . ensure_contains_elem ( def_id, || hir:: MaybeOwner :: Phantom ) ;
118+ if let hir:: MaybeOwner :: Phantom = self . lctx . owners [ def_id] {
119+ let node = self . ast_index [ def_id] ;
120+ match node {
121+ AstOwner :: NonOwner => { }
122+ AstOwner :: Crate ( c) => self . lower_crate ( c) ,
123+ AstOwner :: Item ( item) => self . lower_item ( item) ,
124+ AstOwner :: AssocItem ( item, ctxt) => self . lower_assoc_item ( item, ctxt) ,
125+ AstOwner :: ForeignItem ( item) => self . lower_foreign_item ( item) ,
126+ }
127+ }
128+
129+ self . lctx . owners [ def_id]
114130 }
115131
116- fn visit_item ( & mut self , item : & ' a Item ) {
117- let hir_id = self . without_in_scope_lifetime_defs ( |this| {
118- this. lctx . with_hir_id_owner ( item. id , |lctx| {
119- let node = lctx. lower_item ( item) ;
120- hir:: OwnerNode :: Item ( node)
121- } )
122- } ) ;
132+ fn lower_crate ( & mut self , c : & ' a Crate ) {
133+ debug_assert_eq ! ( self . lctx. resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
123134
124- self . with_parent_item_lifetime_defs ( hir_id, |this| match item. kind {
125- ItemKind :: Impl ( box Impl { ref of_trait, .. } ) => {
126- this. with_trait_impl_ref ( of_trait, |this| visit:: walk_item ( this, item) ) ;
127- }
128- _ => visit:: walk_item ( this, item) ,
129- } )
135+ self . lctx . with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
136+ let module = lctx. lower_mod ( & c. items , c. spans . inner_span ) ;
137+ lctx. lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
138+ hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
139+ } ) ;
130140 }
131141
132- fn visit_fn ( & mut self , fk : FnKind < ' a > , sp : Span , _: NodeId ) {
133- match fk {
134- FnKind :: Fn ( FnCtxt :: Foreign , _, sig, _, _) => {
135- self . visit_fn_header ( & sig. header ) ;
136- visit:: walk_fn_decl ( self , & sig. decl ) ;
137- // Don't visit the foreign function body even if it has one, since lowering the
138- // body would have no meaning and will have already been caught as a parse error.
139- }
140- _ => visit:: walk_fn ( self , fk, sp) ,
141- }
142+ fn lower_item ( & mut self , item : & ' a Item ) {
143+ self . without_in_scope_lifetime_defs ( |this| {
144+ this. lctx . with_hir_id_owner ( item. id , |lctx| hir:: OwnerNode :: Item ( lctx. lower_item ( item) ) )
145+ } ) ;
142146 }
143147
144- fn visit_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
145- debug ! ( in_scope_lifetimes = ?self . lctx. in_scope_lifetimes) ;
146- self . lctx . with_hir_id_owner ( item. id , |lctx| match ctxt {
147- AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
148- AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
149- } ) ;
148+ fn lower_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
149+ let def_id = self . lctx . resolver . local_def_id ( item. id ) ;
150150
151- visit:: walk_assoc_item ( self , item, ctxt) ;
151+ let do_lower = |lctx : & mut LoweringContext < ' _ , ' _ > | {
152+ lctx. with_hir_id_owner ( item. id , |lctx| match ctxt {
153+ AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
154+ AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
155+ } ) ;
156+ } ;
157+
158+ let parent_id = {
159+ let parent = self . lctx . resolver . definitions ( ) . def_key ( def_id) . parent ;
160+ let local_def_index = parent. unwrap ( ) ;
161+ LocalDefId { local_def_index }
162+ } ;
163+ let parent_hir = self . lower_node ( parent_id) . unwrap ( ) . node ( ) . expect_item ( ) ;
164+ self . with_parent_item_lifetime_defs ( parent_hir, |this| match parent_hir. kind {
165+ hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, .. } ) => {
166+ this. with_trait_impl_ref ( of_trait, |this| do_lower ( this. lctx ) )
167+ }
168+ _ => do_lower ( this. lctx ) ,
169+ } ) ;
152170 }
153171
154- fn visit_foreign_item ( & mut self , item : & ' a ForeignItem ) {
172+ fn lower_foreign_item ( & mut self , item : & ' a ForeignItem ) {
155173 self . lctx . with_hir_id_owner ( item. id , |lctx| {
156174 hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) )
157175 } ) ;
158-
159- visit:: walk_foreign_item ( self , item) ;
160176 }
161177}
162178
0 commit comments