@@ -49,12 +49,14 @@ use rustc_data_structures::sync::Lrc;
49
49
use rustc_errors:: struct_span_err;
50
50
use rustc_hir as hir;
51
51
use rustc_hir:: def:: { DefKind , Namespace , PartialRes , PerNS , Res } ;
52
- use rustc_hir:: def_id:: { DefId , DefPathHash , LocalDefId , CRATE_DEF_ID } ;
52
+ use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID } ;
53
53
use rustc_hir:: definitions:: { DefKey , DefPathData , Definitions } ;
54
54
use rustc_hir:: intravisit;
55
55
use rustc_hir:: { ConstArg , GenericArg , ParamName } ;
56
56
use rustc_index:: vec:: { Idx , IndexVec } ;
57
+ use rustc_middle:: ty:: ResolverOutputs ;
57
58
use rustc_query_system:: ich:: StableHashingContext ;
59
+ use rustc_session:: cstore:: CrateStoreDyn ;
58
60
use rustc_session:: utils:: { FlattenNonterminals , NtToTokenstream } ;
59
61
use rustc_session:: Session ;
60
62
use rustc_span:: hygiene:: ExpnId ;
@@ -85,13 +87,17 @@ struct LoweringContext<'a, 'hir: 'a> {
85
87
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
86
88
sess : & ' a Session ,
87
89
88
- resolver : & ' a mut dyn ResolverAstLowering ,
90
+ definitions : & ' a mut Definitions ,
91
+ cstore : & ' a CrateStoreDyn ,
92
+ resolver : & ' a mut ResolverOutputs ,
89
93
90
94
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
91
95
/// if we don't have this function pointer. To avoid that dependency so that
92
96
/// `rustc_middle` is independent of the parser, we use dynamic dispatch here.
93
97
nt_to_tokenstream : NtToTokenstream ,
94
98
99
+ item_generics_num_lifetimes : fn ( & Session , & CrateStoreDyn , DefId ) -> usize ,
100
+
95
101
/// Used to allocate HIR nodes.
96
102
arena : & ' hir Arena < ' hir > ,
97
103
@@ -160,48 +166,79 @@ struct LoweringContext<'a, 'hir: 'a> {
160
166
allow_into_future : Option < Lrc < [ Symbol ] > > ,
161
167
}
162
168
163
- pub trait ResolverAstLowering {
164
- fn def_key ( & self , id : DefId ) -> DefKey ;
165
-
166
- fn def_span ( & self , id : LocalDefId ) -> Span ;
167
-
168
- fn item_generics_num_lifetimes ( & self , def : DefId ) -> usize ;
169
-
170
- fn legacy_const_generic_args ( & mut self , expr : & Expr ) -> Option < Vec < usize > > ;
171
-
172
- /// Obtains resolution for a `NodeId` with a single resolution.
169
+ trait ResolverAstLoweringExt {
170
+ fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > ;
173
171
fn get_partial_res ( & self , id : NodeId ) -> Option < PartialRes > ;
174
-
175
- /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
176
172
fn get_import_res ( & self , id : NodeId ) -> PerNS < Option < Res < NodeId > > > ;
177
-
178
- /// Obtains resolution for a label with the given `NodeId`.
179
173
fn get_label_res ( & self , id : NodeId ) -> Option < NodeId > ;
174
+ fn next_node_id ( & mut self ) -> NodeId ;
175
+ fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > ;
176
+ fn local_def_id ( & self , node : NodeId ) -> LocalDefId ;
177
+ }
180
178
181
- fn create_stable_hashing_context ( & self ) -> StableHashingContext < ' _ > ;
179
+ impl ResolverAstLoweringExt for ResolverOutputs {
180
+ fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > {
181
+ if let ExprKind :: Path ( None , path) = & expr. kind {
182
+ // Don't perform legacy const generics rewriting if the path already
183
+ // has generic arguments.
184
+ if path. segments . last ( ) . unwrap ( ) . args . is_some ( ) {
185
+ return None ;
186
+ }
182
187
183
- fn definitions ( & self ) -> & Definitions ;
188
+ let partial_res = self . partial_res_map . get ( & expr. id ) ?;
189
+ if partial_res. unresolved_segments ( ) != 0 {
190
+ return None ;
191
+ }
184
192
185
- fn init_def_id_to_hir_id_mapping ( & mut self , mapping : IndexVec < LocalDefId , Option < hir:: HirId > > ) ;
193
+ if let Res :: Def ( DefKind :: Fn , def_id) = partial_res. base_res ( ) {
194
+ // We only support cross-crate argument rewriting. Uses
195
+ // within the same crate should be updated to use the new
196
+ // const generics style.
197
+ if def_id. is_local ( ) {
198
+ return None ;
199
+ }
186
200
187
- fn next_node_id ( & mut self ) -> NodeId ;
201
+ if let Some ( v) = self . legacy_const_generic_args . get ( & def_id) {
202
+ return v. clone ( ) ;
203
+ }
204
+ }
205
+ }
188
206
189
- fn take_trait_map ( & mut self , node : NodeId ) -> Option < Vec < hir:: TraitCandidate > > ;
207
+ None
208
+ }
190
209
191
- fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > ;
210
+ /// Obtains resolution for a `NodeId` with a single resolution.
211
+ fn get_partial_res ( & self , id : NodeId ) -> Option < PartialRes > {
212
+ self . partial_res_map . get ( & id) . cloned ( )
213
+ }
192
214
193
- fn local_def_id ( & self , node : NodeId ) -> LocalDefId ;
215
+ /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
216
+ fn get_import_res ( & self , id : NodeId ) -> PerNS < Option < Res < NodeId > > > {
217
+ self . import_res_map . get ( & id) . cloned ( ) . unwrap_or_default ( )
218
+ }
194
219
195
- fn def_path_hash ( & self , def_id : DefId ) -> DefPathHash ;
220
+ /// Obtains resolution for a label with the given `NodeId`.
221
+ fn get_label_res ( & self , id : NodeId ) -> Option < NodeId > {
222
+ self . label_res_map . get ( & id) . cloned ( )
223
+ }
196
224
197
- fn create_def (
198
- & mut self ,
199
- parent : LocalDefId ,
200
- node_id : ast:: NodeId ,
201
- data : DefPathData ,
202
- expn_id : ExpnId ,
203
- span : Span ,
204
- ) -> LocalDefId ;
225
+ fn next_node_id ( & mut self ) -> NodeId {
226
+ let next = self
227
+ . next_node_id
228
+ . as_usize ( )
229
+ . checked_add ( 1 )
230
+ . expect ( "input too large; ran out of NodeIds" ) ;
231
+ self . next_node_id = NodeId :: from_usize ( next) ;
232
+ self . next_node_id
233
+ }
234
+
235
+ fn opt_local_def_id ( & self , node : NodeId ) -> Option < LocalDefId > {
236
+ self . node_id_to_def_id . get ( & node) . copied ( )
237
+ }
238
+
239
+ fn local_def_id ( & self , node : NodeId ) -> LocalDefId {
240
+ self . opt_local_def_id ( node) . unwrap_or_else ( || panic ! ( "no entry for node id: `{:?}`" , node) )
241
+ }
205
242
}
206
243
207
244
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -281,17 +318,23 @@ impl<'a> ImplTraitContext<'_, 'a> {
281
318
pub fn lower_crate < ' a , ' hir > (
282
319
sess : & ' a Session ,
283
320
krate : & ' a Crate ,
284
- resolver : & ' a mut dyn ResolverAstLowering ,
321
+ definitions : & ' a mut Definitions ,
322
+ cstore : & ' a CrateStoreDyn ,
323
+ resolver : & ' a mut ResolverOutputs ,
285
324
nt_to_tokenstream : NtToTokenstream ,
325
+ item_generics_num_lifetimes : fn ( & Session , & CrateStoreDyn , DefId ) -> usize ,
286
326
arena : & ' hir Arena < ' hir > ,
287
327
) -> & ' hir hir:: Crate < ' hir > {
288
328
let _prof_timer = sess. prof . verbose_generic_activity ( "hir_lowering" ) ;
289
329
290
- let owners = IndexVec :: from_fn_n ( |_| None , resolver . definitions ( ) . def_index_count ( ) ) ;
330
+ let owners = IndexVec :: from_fn_n ( |_| None , definitions. def_index_count ( ) ) ;
291
331
LoweringContext {
292
332
sess,
333
+ definitions,
334
+ cstore,
293
335
resolver,
294
336
nt_to_tokenstream,
337
+ item_generics_num_lifetimes,
295
338
arena,
296
339
owners,
297
340
bodies : Vec :: new ( ) ,
@@ -409,7 +452,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
409
452
}
410
453
}
411
454
412
- self . resolver . init_def_id_to_hir_id_mapping ( def_id_to_hir_id) ;
455
+ self . definitions . init_def_id_to_hir_id_mapping ( def_id_to_hir_id) ;
413
456
414
457
let krate = hir:: Crate { owners : self . owners , hir_hash } ;
415
458
self . arena . alloc ( krate)
@@ -418,7 +461,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
418
461
/// Compute the hash for the HIR of the full crate.
419
462
/// This hash will then be part of the crate_hash which is stored in the metadata.
420
463
fn compute_hir_hash ( & mut self ) -> Fingerprint {
421
- let definitions = self . resolver . definitions ( ) ;
464
+ let definitions = & * self . definitions ;
422
465
let mut hir_body_nodes: Vec < _ > = self
423
466
. owners
424
467
. iter_enumerated ( )
@@ -431,11 +474,61 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
431
474
hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
432
475
433
476
let mut stable_hasher = StableHasher :: new ( ) ;
434
- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
477
+ let mut hcx = self . create_stable_hashing_context ( ) ;
435
478
hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
436
479
stable_hasher. finish ( )
437
480
}
438
481
482
+ fn create_stable_hashing_context ( & self ) -> StableHashingContext < ' _ > {
483
+ StableHashingContext :: new ( self . sess , self . definitions , self . cstore )
484
+ }
485
+
486
+ fn def_key ( & self , id : DefId ) -> DefKey {
487
+ if let Some ( id) = id. as_local ( ) {
488
+ self . definitions . def_key ( id)
489
+ } else {
490
+ self . cstore . def_key ( id)
491
+ }
492
+ }
493
+
494
+ fn item_generics_num_lifetimes ( & self , def_id : DefId ) -> usize {
495
+ if let Some ( def_id) = def_id. as_local ( ) {
496
+ self . resolver . item_generics_num_lifetimes [ & def_id]
497
+ } else {
498
+ ( self . item_generics_num_lifetimes ) ( self . sess , self . cstore , def_id)
499
+ }
500
+ }
501
+
502
+ fn create_def (
503
+ & mut self ,
504
+ parent : LocalDefId ,
505
+ node_id : ast:: NodeId ,
506
+ data : DefPathData ,
507
+ expn_id : ExpnId ,
508
+ span : Span ,
509
+ ) -> LocalDefId {
510
+ assert ! (
511
+ !self . resolver. node_id_to_def_id. contains_key( & node_id) ,
512
+ "adding a def'n for node-id {:?} and data {:?} but a previous def'n exists: {:?}" ,
513
+ node_id,
514
+ data,
515
+ self . definitions. def_key( self . resolver. node_id_to_def_id[ & node_id] ) ,
516
+ ) ;
517
+
518
+ let def_id = self . definitions . create_def ( parent, data, expn_id, span) ;
519
+
520
+ // Some things for which we allocate `LocalDefId`s don't correspond to
521
+ // anything in the AST, so they don't have a `NodeId`. For these cases
522
+ // we don't need a mapping from `NodeId` to `LocalDefId`.
523
+ if node_id != ast:: DUMMY_NODE_ID {
524
+ debug ! ( "create_def: def_id_to_node_id[{:?}] <-> {:?}" , def_id, node_id) ;
525
+ self . resolver . node_id_to_def_id . insert ( node_id, def_id) ;
526
+ }
527
+ assert_eq ! ( self . resolver. def_id_to_node_id. push( node_id) , def_id) ;
528
+
529
+ def_id
530
+ }
531
+
439
532
fn with_hir_id_owner (
440
533
& mut self ,
441
534
owner : NodeId ,
@@ -479,7 +572,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
479
572
. into_iter ( )
480
573
. filter_map ( |node_id| {
481
574
let hir_id = self . node_id_to_hir_id [ node_id] ?;
482
- let traits = self . resolver . take_trait_map ( node_id) ?;
575
+ let traits = self . resolver . trait_map . remove ( & node_id) ?;
483
576
Some ( ( hir_id. local_id , traits. into_boxed_slice ( ) ) )
484
577
} )
485
578
. collect ( ) ;
@@ -495,11 +588,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
495
588
bodies. sort_by_key ( |( k, _) | * k) ;
496
589
let bodies = SortedMap :: from_presorted_elements ( bodies) ;
497
590
let ( hash_including_bodies, hash_without_bodies) = self . hash_owner ( node, & bodies) ;
498
- let ( nodes, parenting) =
499
- index:: index_hir ( self . sess , self . resolver . definitions ( ) , node, & bodies) ;
591
+ let ( nodes, parenting) = index:: index_hir ( self . sess , self . definitions , node, & bodies) ;
500
592
let nodes = hir:: OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies } ;
501
593
let attrs = {
502
- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
594
+ let mut hcx = self . create_stable_hashing_context ( ) ;
503
595
let mut stable_hasher = StableHasher :: new ( ) ;
504
596
attrs. hash_stable ( & mut hcx, & mut stable_hasher) ;
505
597
let hash = stable_hasher. finish ( ) ;
@@ -516,7 +608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
516
608
node : hir:: OwnerNode < ' hir > ,
517
609
bodies : & SortedMap < hir:: ItemLocalId , & ' hir hir:: Body < ' hir > > ,
518
610
) -> ( Fingerprint , Fingerprint ) {
519
- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
611
+ let mut hcx = self . create_stable_hashing_context ( ) ;
520
612
let mut stable_hasher = StableHasher :: new ( ) ;
521
613
hcx. with_hir_bodies ( true , node. def_id ( ) , bodies, |hcx| {
522
614
node. hash_stable ( hcx, & mut stable_hasher)
@@ -591,7 +683,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
591
683
allow_internal_unstable,
592
684
reason,
593
685
self . sess . edition ( ) ,
594
- self . resolver . create_stable_hashing_context ( ) ,
686
+ self . create_stable_hashing_context ( ) ,
595
687
)
596
688
}
597
689
@@ -671,7 +763,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
671
763
} ;
672
764
673
765
// Add a definition for the in-band lifetime def.
674
- self . resolver . create_def (
766
+ self . create_def (
675
767
parent_def_id,
676
768
node_id,
677
769
DefPathData :: LifetimeNs ( str_name) ,
@@ -1054,7 +1146,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1054
1146
// constructing the HIR for `impl bounds...` and then lowering that.
1055
1147
1056
1148
let impl_trait_node_id = self . resolver . next_node_id ( ) ;
1057
- self . resolver . create_def (
1149
+ self . create_def (
1058
1150
parent_def_id,
1059
1151
impl_trait_node_id,
1060
1152
DefPathData :: ImplTrait ,
@@ -1129,7 +1221,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1129
1221
let node_id = self . resolver . next_node_id ( ) ;
1130
1222
1131
1223
// Add a definition for the in-band const def.
1132
- self . resolver . create_def (
1224
+ self . create_def (
1133
1225
parent_def_id,
1134
1226
node_id,
1135
1227
DefPathData :: AnonConst ,
@@ -1397,7 +1489,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1397
1489
lctx. arena . alloc_from_iter ( collected_lifetimes. iter ( ) . map ( |& ( name, span) | {
1398
1490
let def_node_id = lctx. resolver . next_node_id ( ) ;
1399
1491
let hir_id = lctx. lower_node_id ( def_node_id) ;
1400
- lctx. resolver . create_def (
1492
+ lctx. create_def (
1401
1493
opaque_ty_def_id,
1402
1494
def_node_id,
1403
1495
DefPathData :: LifetimeNs ( name. ident ( ) . name ) ,
0 commit comments