Skip to content
Open
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
52 changes: 31 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,25 @@ impl BindingKey {
}
}

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

type ExternResolutions<'ra> = OnceLock<ResolutionTable<'ra>>;
type LocalResolutions<'ra> = ResolutionTable<'ra>;

enum Resolutions<'ra> {
Local(LocalResolutions<'ra>),
Extern(ExternResolutions<'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 +673,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 +726,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 +737,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 +2175,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 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 +2937,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