1
1
use super :: { AnonymousLifetimeMode , LoweringContext , ParamMode } ;
2
- use super :: { AstOwner , ImplTraitContext , ImplTraitPosition } ;
2
+ use super :: { AstOwner , ImplTraitContext , ImplTraitPosition , ResolverAstLowering } ;
3
3
use crate :: { Arena , FnDeclKind } ;
4
4
5
5
use rustc_ast:: ptr:: P ;
6
6
use rustc_ast:: visit:: AssocCtxt ;
7
7
use rustc_ast:: * ;
8
8
use rustc_data_structures:: fx:: FxHashSet ;
9
+ use rustc_data_structures:: sorted_map:: SortedMap ;
9
10
use rustc_errors:: struct_span_err;
10
11
use rustc_hir as hir;
11
12
use rustc_hir:: def:: { DefKind , Res } ;
12
13
use rustc_hir:: def_id:: { LocalDefId , CRATE_DEF_ID } ;
13
14
use rustc_index:: vec:: { Idx , IndexVec } ;
15
+ use rustc_session:: utils:: NtToTokenstream ;
16
+ use rustc_session:: Session ;
14
17
use rustc_span:: source_map:: { respan, DesugaringKind } ;
15
18
use rustc_span:: symbol:: { kw, sym, Ident } ;
16
19
use rustc_span:: Span ;
@@ -19,11 +22,14 @@ use smallvec::{smallvec, SmallVec};
19
22
use tracing:: debug;
20
23
21
24
use std:: iter;
22
- use std:: mem;
23
25
24
- pub ( super ) struct ItemLowerer < ' a , ' lowering , ' hir > {
25
- pub ( super ) lctx : & ' a mut LoweringContext < ' lowering , ' hir > ,
26
- pub ( super ) ast_index : & ' a IndexVec < LocalDefId , AstOwner < ' lowering > > ,
26
+ pub ( super ) struct ItemLowerer < ' a , ' hir > {
27
+ pub ( super ) sess : & ' a Session ,
28
+ pub ( super ) resolver : & ' a mut dyn ResolverAstLowering ,
29
+ pub ( super ) nt_to_tokenstream : NtToTokenstream ,
30
+ pub ( super ) arena : & ' hir Arena < ' hir > ,
31
+ pub ( super ) ast_index : & ' a IndexVec < LocalDefId , AstOwner < ' a > > ,
32
+ pub ( super ) owners : & ' a mut IndexVec < LocalDefId , hir:: MaybeOwner < & ' hir hir:: OwnerInfo < ' hir > > > ,
27
33
}
28
34
29
35
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -46,76 +52,50 @@ fn add_ty_alias_where_clause(
46
52
}
47
53
}
48
54
49
- impl < ' a , ' hir > ItemLowerer < ' _ , ' a , ' hir > {
50
- /// Clears (and restores) the `in_scope_lifetimes` field. Used when
51
- /// visiting nested items, which never inherit in-scope lifetimes
52
- /// from their surrounding environment.
53
- #[ tracing:: instrument( level = "debug" , skip( self , f) ) ]
54
- fn without_in_scope_lifetime_defs < T > ( & mut self , f : impl FnOnce ( & mut Self ) -> T ) -> T {
55
- let old_in_scope_lifetimes = mem:: take ( & mut self . lctx . in_scope_lifetimes ) ;
56
- debug ! ( ?old_in_scope_lifetimes) ;
57
-
58
- // this vector is only used when walking over impl headers,
59
- // input types, and the like, and should not be non-empty in
60
- // between items
61
- assert ! ( self . lctx. lifetimes_to_define. is_empty( ) ) ;
62
-
63
- let res = f ( self ) ;
64
-
65
- assert ! ( self . lctx. in_scope_lifetimes. is_empty( ) ) ;
66
- self . lctx . in_scope_lifetimes = old_in_scope_lifetimes;
67
-
68
- res
69
- }
70
-
71
- /// Evaluates `f` with the lifetimes in `params` in-scope.
72
- /// This is used to track which lifetimes have already been defined, and
73
- /// which are new in-band lifetimes that need to have a definition created
74
- /// for them.
75
- fn with_parent_item_lifetime_defs (
76
- & mut self ,
77
- parent_hir : & ' hir hir:: Item < ' hir > ,
78
- f : impl FnOnce ( & mut Self ) ,
79
- ) {
80
- let parent_generics = match parent_hir. kind {
81
- hir:: ItemKind :: Impl ( hir:: Impl { ref generics, .. } )
82
- | hir:: ItemKind :: Trait ( _, _, ref generics, ..) => generics. params ,
83
- _ => & [ ] ,
84
- } ;
85
- let lt_def_names = parent_generics
86
- . iter ( )
87
- . filter_map ( |param| match param. kind {
88
- hir:: GenericParamKind :: Lifetime { .. } => {
89
- Some ( param. name . normalize_to_macros_2_0 ( ) )
90
- }
91
- _ => None ,
92
- } )
93
- . collect ( ) ;
94
- let old_in_scope_lifetimes = mem:: replace ( & mut self . lctx . in_scope_lifetimes , lt_def_names) ;
95
-
96
- f ( self ) ;
97
-
98
- self . lctx . in_scope_lifetimes = old_in_scope_lifetimes;
99
- }
100
-
101
- fn with_trait_impl_ref (
102
- & mut self ,
103
- impl_ref : & Option < hir:: TraitRef < ' _ > > ,
104
- f : impl FnOnce ( & mut Self ) ,
105
- ) {
106
- let old = self . lctx . is_in_trait_impl ;
107
- self . lctx . is_in_trait_impl = impl_ref. is_some ( ) ;
108
- let ret = f ( self ) ;
109
- self . lctx . is_in_trait_impl = old;
110
- ret
55
+ impl < ' a , ' hir > ItemLowerer < ' a , ' hir > {
56
+ fn make_lctx ( & mut self ) -> LoweringContext < ' _ , ' hir > {
57
+ LoweringContext {
58
+ // Pseudo-globals.
59
+ sess : & self . sess ,
60
+ resolver : self . resolver ,
61
+ nt_to_tokenstream : self . nt_to_tokenstream ,
62
+ arena : self . arena ,
63
+ owners : self . owners ,
64
+
65
+ // HirId handling.
66
+ bodies : Vec :: new ( ) ,
67
+ attrs : SortedMap :: default ( ) ,
68
+ current_hir_id_owner : CRATE_DEF_ID ,
69
+ item_local_id_counter : hir:: ItemLocalId :: new ( 0 ) ,
70
+ node_id_to_local_id : Default :: default ( ) ,
71
+ local_id_to_def_id : SortedMap :: new ( ) ,
72
+ trait_map : Default :: default ( ) ,
73
+
74
+ // Lowering state.
75
+ catch_scope : None ,
76
+ loop_scope : None ,
77
+ is_in_loop_condition : false ,
78
+ is_in_trait_impl : false ,
79
+ is_in_dyn_type : false ,
80
+ anonymous_lifetime_mode : AnonymousLifetimeMode :: PassThrough ,
81
+ generator_kind : None ,
82
+ task_context : None ,
83
+ current_item : None ,
84
+ lifetimes_to_define : Vec :: new ( ) ,
85
+ is_collecting_anonymous_lifetimes : None ,
86
+ in_scope_lifetimes : Vec :: new ( ) ,
87
+ allow_try_trait : Some ( [ sym:: try_trait_v2] [ ..] . into ( ) ) ,
88
+ allow_gen_future : Some ( [ sym:: gen_future] [ ..] . into ( ) ) ,
89
+ allow_into_future : Some ( [ sym:: into_future] [ ..] . into ( ) ) ,
90
+ }
111
91
}
112
92
113
93
pub ( super ) fn lower_node (
114
94
& mut self ,
115
95
def_id : LocalDefId ,
116
96
) -> 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] {
97
+ self . owners . ensure_contains_elem ( def_id, || hir:: MaybeOwner :: Phantom ) ;
98
+ if let hir:: MaybeOwner :: Phantom = self . owners [ def_id] {
119
99
let node = self . ast_index [ def_id] ;
120
100
match node {
121
101
AstOwner :: NonOwner => { }
@@ -126,53 +106,72 @@ impl<'a, 'hir> ItemLowerer<'_, 'a, 'hir> {
126
106
}
127
107
}
128
108
129
- self . lctx . owners [ def_id]
109
+ self . owners [ def_id]
130
110
}
131
111
132
112
fn lower_crate ( & mut self , c : & ' a Crate ) {
133
- debug_assert_eq ! ( self . lctx . resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
113
+ debug_assert_eq ! ( self . resolver. local_def_id( CRATE_NODE_ID ) , CRATE_DEF_ID ) ;
134
114
135
- self . lctx . with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
115
+ let mut lctx = self . make_lctx ( ) ;
116
+ lctx. with_hir_id_owner ( CRATE_NODE_ID , |lctx| {
136
117
let module = lctx. lower_mod ( & c. items , c. spans . inner_span ) ;
137
118
lctx. lower_attrs ( hir:: CRATE_HIR_ID , & c. attrs ) ;
138
119
hir:: OwnerNode :: Crate ( lctx. arena . alloc ( module) )
139
- } ) ;
120
+ } )
140
121
}
141
122
142
123
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
- } ) ;
124
+ let mut lctx = self . make_lctx ( ) ;
125
+ lctx. with_hir_id_owner ( item. id , |lctx| hir:: OwnerNode :: Item ( lctx. lower_item ( item) ) )
146
126
}
147
127
148
128
fn lower_assoc_item ( & mut self , item : & ' a AssocItem , ctxt : AssocCtxt ) {
149
- let def_id = self . lctx . resolver . local_def_id ( item. id ) ;
150
-
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
- } ;
129
+ let def_id = self . resolver . local_def_id ( item. id ) ;
157
130
158
131
let parent_id = {
159
- let parent = self . lctx . resolver . definitions ( ) . def_key ( def_id) . parent ;
132
+ let parent = self . resolver . definitions ( ) . def_key ( def_id) . parent ;
160
133
let local_def_index = parent. unwrap ( ) ;
161
134
LocalDefId { local_def_index }
162
135
} ;
136
+
163
137
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 ) )
138
+ let mut lctx = self . make_lctx ( ) ;
139
+
140
+ // Evaluate with the lifetimes in `params` in-scope.
141
+ // This is used to track which lifetimes have already been defined,
142
+ // and which need to be replicated when lowering an async fn.
143
+ match parent_hir. kind {
144
+ hir:: ItemKind :: Impl ( hir:: Impl { ref of_trait, ref generics, .. } ) => {
145
+ lctx. is_in_trait_impl = of_trait. is_some ( ) ;
146
+ lctx. in_scope_lifetimes = generics
147
+ . params
148
+ . iter ( )
149
+ . filter ( |param| matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } ) )
150
+ . map ( |param| param. name )
151
+ . collect ( ) ;
152
+ }
153
+ hir:: ItemKind :: Trait ( _, _, ref generics, ..) => {
154
+ lctx. in_scope_lifetimes = generics
155
+ . params
156
+ . iter ( )
157
+ . filter ( |param| matches ! ( param. kind, hir:: GenericParamKind :: Lifetime { .. } ) )
158
+ . map ( |param| param. name )
159
+ . collect ( ) ;
167
160
}
168
- _ => do_lower ( this. lctx ) ,
169
- } ) ;
161
+ _ => { }
162
+ } ;
163
+
164
+ lctx. with_hir_id_owner ( item. id , |lctx| match ctxt {
165
+ AssocCtxt :: Trait => hir:: OwnerNode :: TraitItem ( lctx. lower_trait_item ( item) ) ,
166
+ AssocCtxt :: Impl => hir:: OwnerNode :: ImplItem ( lctx. lower_impl_item ( item) ) ,
167
+ } )
170
168
}
171
169
172
170
fn lower_foreign_item ( & mut self , item : & ' a ForeignItem ) {
173
- self . lctx . with_hir_id_owner ( item. id , |lctx| {
171
+ let mut lctx = self . make_lctx ( ) ;
172
+ lctx. with_hir_id_owner ( item. id , |lctx| {
174
173
hir:: OwnerNode :: ForeignItem ( lctx. lower_foreign_item ( item) )
175
- } ) ;
174
+ } )
176
175
}
177
176
}
178
177
0 commit comments