@@ -431,6 +431,8 @@ impl ModuleKind {
431431 }
432432}
433433
434+ type Resolutions < ' a > = RefCell < FxHashMap < ( Ident , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ;
435+
434436/// One node in the tree of modules.
435437pub struct ModuleData < ' a > {
436438 parent : Option < Module < ' a > > ,
@@ -439,7 +441,11 @@ pub struct ModuleData<'a> {
439441 // The def id of the closest normal module (`mod`) ancestor (including this module).
440442 normal_ancestor_id : DefId ,
441443
442- resolutions : RefCell < FxHashMap < ( Ident , Namespace ) , & ' a RefCell < NameResolution < ' a > > > > ,
444+ // Mapping between names and their (possibly in-progress) resolutions in this module.
445+ // Resolutions in modules from other crates are not populated until accessed.
446+ lazy_resolutions : Resolutions < ' a > ,
447+ // True if this is a module from other crate that needs to be populated on access.
448+ populate_on_access : Cell < bool > ,
443449
444450 // Macro invocations that can expand into items in this module.
445451 unresolved_invocations : RefCell < FxHashSet < ExpnId > > ,
@@ -452,11 +458,6 @@ pub struct ModuleData<'a> {
452458 // Used to memoize the traits in this module for faster searches through all traits in scope.
453459 traits : RefCell < Option < Box < [ ( Ident , & ' a NameBinding < ' a > ) ] > > > ,
454460
455- // Whether this module is populated. If not populated, any attempt to
456- // access the children must be preceded with a
457- // `populate_module_if_necessary` call.
458- populated : Cell < bool > ,
459-
460461 /// Span of the module itself. Used for error reporting.
461462 span : Span ,
462463
@@ -475,30 +476,34 @@ impl<'a> ModuleData<'a> {
475476 parent,
476477 kind,
477478 normal_ancestor_id,
478- resolutions : Default :: default ( ) ,
479+ lazy_resolutions : Default :: default ( ) ,
480+ populate_on_access : Cell :: new ( !normal_ancestor_id. is_local ( ) ) ,
479481 unresolved_invocations : Default :: default ( ) ,
480482 no_implicit_prelude : false ,
481483 glob_importers : RefCell :: new ( Vec :: new ( ) ) ,
482484 globs : RefCell :: new ( Vec :: new ( ) ) ,
483485 traits : RefCell :: new ( None ) ,
484- populated : Cell :: new ( normal_ancestor_id. is_local ( ) ) ,
485486 span,
486487 expansion,
487488 }
488489 }
489490
490- fn for_each_child < F : FnMut ( Ident , Namespace , & ' a NameBinding < ' a > ) > ( & self , mut f : F ) {
491- for ( & ( ident, ns) , name_resolution) in self . resolutions . borrow ( ) . iter ( ) {
492- name_resolution. borrow ( ) . binding . map ( |binding| f ( ident, ns, binding) ) ;
491+ fn for_each_child < R , F > ( & ' a self , resolver : & mut R , mut f : F )
492+ where R : AsMut < Resolver < ' a > > , F : FnMut ( & mut R , Ident , Namespace , & ' a NameBinding < ' a > )
493+ {
494+ for ( & ( ident, ns) , name_resolution) in resolver. as_mut ( ) . resolutions ( self ) . borrow ( ) . iter ( ) {
495+ name_resolution. borrow ( ) . binding . map ( |binding| f ( resolver, ident, ns, binding) ) ;
493496 }
494497 }
495498
496- fn for_each_child_stable < F : FnMut ( Ident , Namespace , & ' a NameBinding < ' a > ) > ( & self , mut f : F ) {
497- let resolutions = self . resolutions . borrow ( ) ;
499+ fn for_each_child_stable < R , F > ( & ' a self , resolver : & mut R , mut f : F )
500+ where R : AsMut < Resolver < ' a > > , F : FnMut ( & mut R , Ident , Namespace , & ' a NameBinding < ' a > )
501+ {
502+ let resolutions = resolver. as_mut ( ) . resolutions ( self ) . borrow ( ) ;
498503 let mut resolutions = resolutions. iter ( ) . collect :: < Vec < _ > > ( ) ;
499504 resolutions. sort_by_cached_key ( |& ( & ( ident, ns) , _) | ( ident. as_str ( ) , ns) ) ;
500505 for & ( & ( ident, ns) , & resolution) in resolutions. iter ( ) {
501- resolution. borrow ( ) . binding . map ( |binding| f ( ident, ns, binding) ) ;
506+ resolution. borrow ( ) . binding . map ( |binding| f ( resolver , ident, ns, binding) ) ;
502507 }
503508 }
504509
@@ -983,6 +988,10 @@ impl<'a> ResolverArenas<'a> {
983988 }
984989}
985990
991+ impl < ' a > AsMut < Resolver < ' a > > for Resolver < ' a > {
992+ fn as_mut ( & mut self ) -> & mut Resolver < ' a > { self }
993+ }
994+
986995impl < ' a , ' b > ty:: DefIdTree for & ' a Resolver < ' b > {
987996 fn parent ( self , id : DefId ) -> Option < DefId > {
988997 match id. krate {
@@ -2634,7 +2643,6 @@ impl<'a> Resolver<'a> {
26342643 return None ;
26352644 } ;
26362645 let crate_root = self . get_module ( DefId { krate : crate_id, index : CRATE_DEF_INDEX } ) ;
2637- self . populate_module_if_necessary ( & crate_root) ;
26382646 Some ( ( crate_root, ty:: Visibility :: Public , DUMMY_SP , ExpnId :: root ( ) )
26392647 . to_name_binding ( self . arenas ) )
26402648 }
0 commit comments