1
1
use super :: { AnonymousLifetimeMode , LoweringContext , ParamMode } ;
2
- use super :: { ImplTraitContext , ImplTraitPosition } ;
2
+ use super :: { AstOwner , ImplTraitContext , ImplTraitPosition } ;
3
3
use crate :: { Arena , FnDeclKind } ;
4
4
5
5
use rustc_ast:: ptr:: P ;
6
- use rustc_ast:: visit:: { self , AssocCtxt , FnCtxt , FnKind , Visitor } ;
6
+ use rustc_ast:: visit:: AssocCtxt ;
7
7
use rustc_ast:: * ;
8
8
use rustc_data_structures:: fx:: FxHashSet ;
9
9
use rustc_errors:: struct_span_err;
10
10
use rustc_hir as hir;
11
11
use 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 } ;
14
14
use rustc_span:: source_map:: { respan, DesugaringKind } ;
15
15
use rustc_span:: symbol:: { kw, sym, Ident } ;
16
16
use rustc_span:: Span ;
@@ -23,6 +23,7 @@ use std::mem;
23
23
24
24
pub ( super ) struct ItemLowerer < ' a , ' lowering , ' hir > {
25
25
pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
26
+ pub ( super ) ast_index : & ' a IndexVec < LocalDefId , AstOwner < ' lowering > > ,
26
27
}
27
28
28
29
/// 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(
45
46
}
46
47
}
47
48
48
- impl ItemLowerer < ' _ , ' _ , ' _ > {
49
+ impl < ' a , ' hir > ItemLowerer < ' _ , ' a , ' hir > {
49
50
/// Clears (and restores) the `in_scope_lifetimes` field. Used when
50
51
/// visiting nested items, which never inherit in-scope lifetimes
51
52
/// from their surrounding environment.
@@ -73,10 +74,9 @@ impl ItemLowerer<'_, '_, '_> {
73
74
/// for them.
74
75
fn with_parent_item_lifetime_defs (
75
76
& mut self ,
76
- parent_item : LocalDefId ,
77
+ parent_hir : & ' hir hir :: Item < ' hir > ,
77
78
f : impl FnOnce ( & mut Self ) ,
78
79
) {
79
- let parent_hir = self . lctx . owners [ parent_item] . unwrap ( ) . node ( ) . expect_item ( ) ;
80
80
let parent_generics = match parent_hir. kind {
81
81
hir:: ItemKind :: Impl ( hir:: Impl { ref generics, .. } )
82
82
| hir:: ItemKind :: Trait ( _, _, ref generics, ..) => generics. params ,
@@ -98,65 +98,81 @@ impl ItemLowerer<'_, '_, '_> {
98
98
self . lctx . in_scope_lifetimes = old_in_scope_lifetimes;
99
99
}
100
100
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
+ ) {
102
106
let old = self . lctx . is_in_trait_impl ;
103
107
self . lctx . is_in_trait_impl = impl_ref. is_some ( ) ;
104
108
let ret = f ( self ) ;
105
109
self . lctx . is_in_trait_impl = old;
106
110
ret
107
111
}
108
- }
109
112
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]
114
130
}
115
131
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 ) ;
123
134
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
+ } ) ;
130
140
}
131
141
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
+ } ) ;
142
146
}
143
147
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 ) ;
150
150
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
+ } ) ;
152
170
}
153
171
154
- fn visit_foreign_item ( & mut self , item : & ' a ForeignItem ) {
172
+ fn lower_foreign_item ( & mut self , item : & ' a ForeignItem ) {
155
173
self . lctx . with_hir_id_owner ( item. id , |lctx| {
156
174
hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) )
157
175
} ) ;
158
-
159
- visit:: walk_foreign_item ( self , item) ;
160
176
}
161
177
}
162
178
0 commit comments