2424use std:: cell:: Ref ;
2525use std:: collections:: BTreeSet ;
2626use std:: ops:: ControlFlow ;
27- use std:: sync:: { Arc , Once } ;
27+ use std:: sync:: { Arc , OnceLock } ;
2828use std:: { fmt, mem} ;
2929
3030use diagnostics:: { ParamKindInEnumDiscriminant , ParamKindInNonTrivialAnonConst } ;
@@ -633,7 +633,25 @@ impl BindingKey {
633633 }
634634}
635635
636- type Resolutions < ' ra > = CmRefCell < FxIndexMap < BindingKey , NameResolutionRef < ' ra > > > ;
636+ type ResolutionTable < ' ra > = CmRefCell < FxIndexMap < BindingKey , NameResolutionRef < ' ra > > > ;
637+
638+ type ExternResolutions < ' ra > = OnceLock < ResolutionTable < ' ra > > ;
639+ type LocalResolutions < ' ra > = ResolutionTable < ' ra > ;
640+
641+ enum Resolutions < ' ra > {
642+ Local ( LocalResolutions < ' ra > ) ,
643+ Extern ( ExternResolutions < ' ra > ) ,
644+ }
645+
646+ impl < ' ra > Resolutions < ' ra > {
647+ fn new ( local : bool ) -> Self {
648+ if local {
649+ Resolutions :: Local ( Default :: default ( ) )
650+ } else {
651+ Resolutions :: Extern ( Default :: default ( ) )
652+ }
653+ }
654+ }
637655
638656/// One node in the tree of modules.
639657///
@@ -655,8 +673,6 @@ struct ModuleData<'ra> {
655673 /// Mapping between names and their (possibly in-progress) resolutions in this module.
656674 /// Resolutions in modules from other crates are not populated until accessed.
657675 lazy_resolutions : Resolutions < ' ra > ,
658- /// True if this is a module from other crate that needs to be populated on access.
659- populate_on_access : Once ,
660676 /// Used to disambiguate underscore items (`const _: T = ...`) in the module.
661677 underscore_disambiguator : CmCell < u32 > ,
662678
@@ -710,6 +726,7 @@ impl<'ra> ModuleData<'ra> {
710726 vis : Visibility < ModId > ,
711727 arenas : & ' ra ResolverArenas < ' ra > ,
712728 ) -> Self {
729+ let lazy_resolutions = Resolutions :: new ( kind. is_local ( ) ) ;
713730 let self_decl = match kind {
714731 ModuleKind :: Def ( def_kind, def_id, ..) => {
715732 let expn_id = expansion. as_local ( ) . unwrap_or ( LocalExpnId :: ROOT ) ;
@@ -720,8 +737,7 @@ impl<'ra> ModuleData<'ra> {
720737 ModuleData {
721738 parent,
722739 kind,
723- lazy_resolutions : Default :: default ( ) ,
724- populate_on_access : Once :: new ( ) ,
740+ lazy_resolutions,
725741 underscore_disambiguator : CmCell :: new ( 0 ) ,
726742 unexpanded_invocations : Default :: default ( ) ,
727743 no_implicit_prelude,
@@ -2159,15 +2175,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
21592175 self . tcx . hir_arena . alloc_slice ( & import_ids)
21602176 }
21612177
2162- fn resolutions ( & self , module : Module < ' ra > ) -> & ' ra Resolutions < ' ra > {
2163- if !module. is_local ( ) {
2164- // as long as 1 thread is building this external table, all other threads will wait
2165- module. populate_on_access . call_once ( || {
2166- * module. lazy_resolutions . borrow_mut_unchecked ( ) =
2167- self . build_reduced_graph_external ( module. expect_extern ( ) ) ;
2168- } ) ;
2178+ fn resolutions ( & self , module : Module < ' ra > ) -> & ' ra ResolutionTable < ' ra > {
2179+ match & module. 0 . 0 . lazy_resolutions {
2180+ Resolutions :: Local ( local_res) => local_res,
2181+ Resolutions :: Extern ( extern_res) => {
2182+ // as long as 1 thread is building this external table, all other threads will wait
2183+ extern_res. get_or_init ( || {
2184+ CmRefCell :: new ( self . build_reduced_graph_external ( module. expect_extern ( ) ) )
2185+ } )
2186+ }
21692187 }
2170- & module. 0 . 0 . lazy_resolutions
21712188 }
21722189
21732190 fn resolution (
@@ -2920,13 +2937,6 @@ mod ref_mut {
29202937 CmRefCell ( RefCell :: new ( value) )
29212938 }
29222939
2923- #[ track_caller]
2924- // FIXME: this should be eliminated in the process of migration
2925- // to parallel name resolution.
2926- pub ( crate ) fn borrow_mut_unchecked ( & self ) -> RefMut < ' _ , T > {
2927- self . 0 . borrow_mut ( )
2928- }
2929-
29302940 #[ track_caller]
29312941 pub ( crate ) fn borrow_mut < ' ra , ' tcx > ( & self , r : & Resolver < ' ra , ' tcx > ) -> RefMut < ' _ , T > {
29322942 if r. assert_speculative {
0 commit comments