@@ -49,12 +49,14 @@ use rustc_data_structures::sync::Lrc;
4949use rustc_errors:: struct_span_err;
5050use rustc_hir as hir;
5151use 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 } ;
5353use rustc_hir:: definitions:: { DefKey , DefPathData , Definitions } ;
5454use rustc_hir:: intravisit;
5555use rustc_hir:: { ConstArg , GenericArg , ParamName } ;
5656use rustc_index:: vec:: { Idx , IndexVec } ;
57+ use rustc_middle:: ty:: ResolverOutputs ;
5758use rustc_query_system:: ich:: StableHashingContext ;
59+ use rustc_session:: cstore:: CrateStoreDyn ;
5860use rustc_session:: utils:: { FlattenNonterminals , NtToTokenstream } ;
5961use rustc_session:: Session ;
6062use rustc_span:: hygiene:: ExpnId ;
@@ -85,13 +87,17 @@ struct LoweringContext<'a, 'hir: 'a> {
8587 /// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
8688 sess : & ' a Session ,
8789
88- resolver : & ' a mut dyn ResolverAstLowering ,
90+ definitions : & ' a mut Definitions ,
91+ cstore : & ' a CrateStoreDyn ,
92+ resolver : & ' a mut ResolverOutputs ,
8993
9094 /// HACK(Centril): there is a cyclic dependency between the parser and lowering
9195 /// if we don't have this function pointer. To avoid that dependency so that
9296 /// `rustc_middle` is independent of the parser, we use dynamic dispatch here.
9397 nt_to_tokenstream : NtToTokenstream ,
9498
99+ item_generics_num_lifetimes : fn ( & Session , & CrateStoreDyn , DefId ) -> usize ,
100+
95101 /// Used to allocate HIR nodes.
96102 arena : & ' hir Arena < ' hir > ,
97103
@@ -160,48 +166,79 @@ struct LoweringContext<'a, 'hir: 'a> {
160166 allow_into_future : Option < Lrc < [ Symbol ] > > ,
161167}
162168
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 > > ;
173171 fn get_partial_res ( & self , id : NodeId ) -> Option < PartialRes > ;
174-
175- /// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
176172 fn get_import_res ( & self , id : NodeId ) -> PerNS < Option < Res < NodeId > > > ;
177-
178- /// Obtains resolution for a label with the given `NodeId`.
179173 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+ }
180178
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+ }
182187
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+ }
184192
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+ }
186200
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+ }
188206
189- fn take_trait_map ( & mut self , node : NodeId ) -> Option < Vec < hir:: TraitCandidate > > ;
207+ None
208+ }
190209
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+ }
192214
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+ }
194219
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+ }
196224
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+ }
205242}
206243
207244/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
@@ -281,17 +318,23 @@ impl<'a> ImplTraitContext<'_, 'a> {
281318pub fn lower_crate < ' a , ' hir > (
282319 sess : & ' a Session ,
283320 krate : & ' a Crate ,
284- resolver : & ' a mut dyn ResolverAstLowering ,
321+ definitions : & ' a mut Definitions ,
322+ cstore : & ' a CrateStoreDyn ,
323+ resolver : & ' a mut ResolverOutputs ,
285324 nt_to_tokenstream : NtToTokenstream ,
325+ item_generics_num_lifetimes : fn ( & Session , & CrateStoreDyn , DefId ) -> usize ,
286326 arena : & ' hir Arena < ' hir > ,
287327) -> & ' hir hir:: Crate < ' hir > {
288328 let _prof_timer = sess. prof . verbose_generic_activity ( "hir_lowering" ) ;
289329
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 ( ) ) ;
291331 LoweringContext {
292332 sess,
333+ definitions,
334+ cstore,
293335 resolver,
294336 nt_to_tokenstream,
337+ item_generics_num_lifetimes,
295338 arena,
296339 owners,
297340 bodies : Vec :: new ( ) ,
@@ -409,7 +452,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
409452 }
410453 }
411454
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) ;
413456
414457 let krate = hir:: Crate { owners : self . owners , hir_hash } ;
415458 self . arena . alloc ( krate)
@@ -418,7 +461,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
418461 /// Compute the hash for the HIR of the full crate.
419462 /// This hash will then be part of the crate_hash which is stored in the metadata.
420463 fn compute_hir_hash ( & mut self ) -> Fingerprint {
421- let definitions = self . resolver . definitions ( ) ;
464+ let definitions = & * self . definitions ;
422465 let mut hir_body_nodes: Vec < _ > = self
423466 . owners
424467 . iter_enumerated ( )
@@ -431,11 +474,61 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
431474 hir_body_nodes. sort_unstable_by_key ( |bn| bn. 0 ) ;
432475
433476 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 ( ) ;
435478 hir_body_nodes. hash_stable ( & mut hcx, & mut stable_hasher) ;
436479 stable_hasher. finish ( )
437480 }
438481
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+
439532 fn with_hir_id_owner (
440533 & mut self ,
441534 owner : NodeId ,
@@ -479,7 +572,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
479572 . into_iter ( )
480573 . filter_map ( |node_id| {
481574 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) ?;
483576 Some ( ( hir_id. local_id , traits. into_boxed_slice ( ) ) )
484577 } )
485578 . collect ( ) ;
@@ -495,11 +588,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
495588 bodies. sort_by_key ( |( k, _) | * k) ;
496589 let bodies = SortedMap :: from_presorted_elements ( bodies) ;
497590 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) ;
500592 let nodes = hir:: OwnerNodes { hash_including_bodies, hash_without_bodies, nodes, bodies } ;
501593 let attrs = {
502- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
594+ let mut hcx = self . create_stable_hashing_context ( ) ;
503595 let mut stable_hasher = StableHasher :: new ( ) ;
504596 attrs. hash_stable ( & mut hcx, & mut stable_hasher) ;
505597 let hash = stable_hasher. finish ( ) ;
@@ -516,7 +608,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
516608 node : hir:: OwnerNode < ' hir > ,
517609 bodies : & SortedMap < hir:: ItemLocalId , & ' hir hir:: Body < ' hir > > ,
518610 ) -> ( Fingerprint , Fingerprint ) {
519- let mut hcx = self . resolver . create_stable_hashing_context ( ) ;
611+ let mut hcx = self . create_stable_hashing_context ( ) ;
520612 let mut stable_hasher = StableHasher :: new ( ) ;
521613 hcx. with_hir_bodies ( true , node. def_id ( ) , bodies, |hcx| {
522614 node. hash_stable ( hcx, & mut stable_hasher)
@@ -591,7 +683,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
591683 allow_internal_unstable,
592684 reason,
593685 self . sess . edition ( ) ,
594- self . resolver . create_stable_hashing_context ( ) ,
686+ self . create_stable_hashing_context ( ) ,
595687 )
596688 }
597689
@@ -671,7 +763,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
671763 } ;
672764
673765 // Add a definition for the in-band lifetime def.
674- self . resolver . create_def (
766+ self . create_def (
675767 parent_def_id,
676768 node_id,
677769 DefPathData :: LifetimeNs ( str_name) ,
@@ -1054,7 +1146,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10541146 // constructing the HIR for `impl bounds...` and then lowering that.
10551147
10561148 let impl_trait_node_id = self . resolver . next_node_id ( ) ;
1057- self . resolver . create_def (
1149+ self . create_def (
10581150 parent_def_id,
10591151 impl_trait_node_id,
10601152 DefPathData :: ImplTrait ,
@@ -1129,7 +1221,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11291221 let node_id = self . resolver . next_node_id ( ) ;
11301222
11311223 // Add a definition for the in-band const def.
1132- self . resolver . create_def (
1224+ self . create_def (
11331225 parent_def_id,
11341226 node_id,
11351227 DefPathData :: AnonConst ,
@@ -1397,7 +1489,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
13971489 lctx. arena . alloc_from_iter ( collected_lifetimes. iter ( ) . map ( |& ( name, span) | {
13981490 let def_node_id = lctx. resolver . next_node_id ( ) ;
13991491 let hir_id = lctx. lower_node_id ( def_node_id) ;
1400- lctx. resolver . create_def (
1492+ lctx. create_def (
14011493 opaque_ty_def_id,
14021494 def_node_id,
14031495 DefPathData :: LifetimeNs ( name. ident ( ) . name ) ,
0 commit comments