Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ use crate::ref_mut::CmCell;
use crate::{
BindingKey, Decl, DeclData, DeclKind, DelayedVisResolutionError, ExternModule,
ExternPreludeEntry, Finalize, IdentKey, LocalModule, Module, ModuleKind, ModuleOrUniformRoot,
ParentScope, PathResult, Res, Resolver, Segment, Used, VisResolutionError, diagnostics,
ParentScope, PathResult, Res, ResolutionTable, Resolver, Segment, Used, VisResolutionError,
diagnostics,
};

impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Expand Down Expand Up @@ -336,7 +337,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn build_reduced_graph_external(
&self,
module: ExternModule<'ra>,
) -> FxIndexMap<BindingKey, NameResolutionRef<'ra>> {
) -> ResolutionTable<'ra> {
let mut resolutions = FxIndexMap::default();
let def_id = module.def_id();
let children = self.tcx.module_children(def_id);
Expand Down
49 changes: 28 additions & 21 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use std::cell::Ref;
use std::collections::BTreeSet;
use std::ops::ControlFlow;
use std::sync::{Arc, Once};
use std::sync::{Arc, OnceLock};
use std::{fmt, mem};

use diagnostics::{ParamKindInEnumDiscriminant, ParamKindInNonTrivialAnonConst};
Expand Down Expand Up @@ -633,7 +633,22 @@ impl BindingKey {
}
}

type Resolutions<'ra> = CmRefCell<FxIndexMap<BindingKey, NameResolutionRef<'ra>>>;
type ResolutionTable<'ra> = FxIndexMap<BindingKey, NameResolutionRef<'ra>>;

enum Resolutions<'ra> {
Local(CmRefCell<ResolutionTable<'ra>>),
Extern(OnceLock<CmRefCell<ResolutionTable<'ra>>>),
}

impl<'ra> Resolutions<'ra> {
fn new(local: bool) -> Self {
if local {
Resolutions::Local(Default::default())
} else {
Resolutions::Extern(Default::default())
}
}
}

/// One node in the tree of modules.
///
Expand All @@ -655,8 +670,6 @@ struct ModuleData<'ra> {
/// Mapping between names and their (possibly in-progress) resolutions in this module.
/// Resolutions in modules from other crates are not populated until accessed.
lazy_resolutions: Resolutions<'ra>,
/// True if this is a module from other crate that needs to be populated on access.
populate_on_access: Once,
/// Used to disambiguate underscore items (`const _: T = ...`) in the module.
underscore_disambiguator: CmCell<u32>,

Expand Down Expand Up @@ -710,6 +723,7 @@ impl<'ra> ModuleData<'ra> {
vis: Visibility<ModId>,
arenas: &'ra ResolverArenas<'ra>,
) -> Self {
let lazy_resolutions = Resolutions::new(kind.is_local());
let self_decl = match kind {
ModuleKind::Def(def_kind, def_id, ..) => {
let expn_id = expansion.as_local().unwrap_or(LocalExpnId::ROOT);
Expand All @@ -720,8 +734,7 @@ impl<'ra> ModuleData<'ra> {
ModuleData {
parent,
kind,
lazy_resolutions: Default::default(),
populate_on_access: Once::new(),
lazy_resolutions,
underscore_disambiguator: CmCell::new(0),
unexpanded_invocations: Default::default(),
no_implicit_prelude,
Expand Down Expand Up @@ -2159,15 +2172,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.tcx.hir_arena.alloc_slice(&import_ids)
}

fn resolutions(&self, module: Module<'ra>) -> &'ra Resolutions<'ra> {
if !module.is_local() {
// as long as 1 thread is building this external table, all other threads will wait
module.populate_on_access.call_once(|| {
*module.lazy_resolutions.borrow_mut_unchecked() =
self.build_reduced_graph_external(module.expect_extern());
});
fn resolutions(&self, module: Module<'ra>) -> &'ra CmRefCell<ResolutionTable<'ra>> {
match &module.0.0.lazy_resolutions {
Resolutions::Local(local_res) => local_res,
Resolutions::Extern(extern_res) => {
// as long as 1 thread is building this external table, all other threads will wait
extern_res.get_or_init(|| {
CmRefCell::new(self.build_reduced_graph_external(module.expect_extern()))
})
}
}
&module.0.0.lazy_resolutions
}

fn resolution(
Expand Down Expand Up @@ -2920,13 +2934,6 @@ mod ref_mut {
CmRefCell(RefCell::new(value))
}

#[track_caller]
// FIXME: this should be eliminated in the process of migration
// to parallel name resolution.
pub(crate) fn borrow_mut_unchecked(&self) -> RefMut<'_, T> {
self.0.borrow_mut()
}

#[track_caller]
pub(crate) fn borrow_mut<'ra, 'tcx>(&self, r: &Resolver<'ra, 'tcx>) -> RefMut<'_, T> {
if r.assert_speculative {
Expand Down
Loading