Make untracked.cstore lockable so that resolution can still write to it when using TyCtxt

This commit is contained in:
Oli Scherer 2022-12-08 10:53:20 +00:00
parent e8e227aec8
commit ade3dceb38
9 changed files with 50 additions and 36 deletions

View file

@ -130,11 +130,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
def_key.disambiguated_data.data.get_opt_name().expect("module without name")
};
let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess);
let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess);
Some(self.new_module(
parent,
ModuleKind::Def(def_kind, def_id, name),
self.cstore().module_expansion_untracked(def_id, &self.tcx.sess),
self.cstore().get_span_untracked(def_id, &self.tcx.sess),
expn_id,
span,
// FIXME: Account for `#[no_implicit_prelude]` attributes.
parent.map_or(false, |module| module.no_implicit_prelude),
))
@ -179,7 +181,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
return macro_data.clone();
}
let (ext, macro_rules) = match self.cstore().load_macro_untracked(def_id, &self.tcx.sess) {
let load_macro_untracked = self.cstore().load_macro_untracked(def_id, &self.tcx.sess);
let (ext, macro_rules) = match load_macro_untracked {
LoadedMacro::MacroDef(item, edition) => (
Lrc::new(self.compile_macro(&item, edition).0),
matches!(item.kind, ItemKind::MacroDef(def) if def.macro_rules),
@ -204,9 +207,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'a>) {
for child in
Vec::from_iter(self.cstore().module_children_untracked(module.def_id(), self.tcx.sess))
{
let children =
Vec::from_iter(self.cstore().module_children_untracked(module.def_id(), self.tcx.sess));
for child in children {
let parent_scope = ParentScope::module(module, self);
BuildReducedGraphVisitor { r: self, parent_scope }
.build_reduced_graph_for_external_crate_res(child);
@ -1000,23 +1003,26 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
| Res::Err => bug!("unexpected resolution: {:?}", res),
}
// Record some extra data for better diagnostics.
let cstore = self.r.cstore();
match res {
Res::Def(DefKind::Struct, def_id) => {
let cstore = self.r.cstore();
if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) {
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
let field_visibilities =
cstore.struct_field_visibilities_untracked(def_id).collect();
drop(cstore);
self.r
.struct_constructors
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
} else {
drop(cstore);
}
self.insert_field_names_extern(def_id)
}
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
Res::Def(DefKind::AssocFn, def_id) => {
if cstore.fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {
if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {
self.r.has_self.insert(def_id);
}
}

View file

@ -27,7 +27,7 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::intern::Interned;
use rustc_data_structures::sync::{Lrc, RwLock};
use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard, RwLock};
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::{self, *};
@ -1132,7 +1132,7 @@ impl DefIdTree for ResolverTree<'_> {
let ResolverTree(Untracked { definitions, cstore, .. }) = self;
match id.as_local() {
Some(id) => definitions.read().def_key(id).parent,
None => cstore.as_any().downcast_ref::<CStore>().unwrap().def_key(id).parent,
None => cstore.read().as_any().downcast_ref::<CStore>().unwrap().def_key(id).parent,
}
.map(|index| DefId { index, ..id })
}
@ -1328,7 +1328,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
local_crate_name: crate_name,
used_extern_options: Default::default(),
untracked: Untracked {
cstore: Box::new(CStore::new(session)),
cstore: RwLock::new(Box::new(CStore::new(session))),
source_span,
definitions: RwLock::new(definitions),
},
@ -1487,14 +1487,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
&self.tcx.sess,
&*self.metadata_loader,
self.local_crate_name,
&mut *self.untracked.cstore.untracked_as_any().downcast_mut().unwrap(),
&mut *self.untracked.cstore.write().untracked_as_any().downcast_mut().unwrap(),
self.untracked.definitions.read(),
&mut self.used_extern_options,
))
}
fn cstore(&self) -> &CStore {
self.untracked.cstore.as_any().downcast_ref().unwrap()
fn cstore(&self) -> MappedReadGuard<'_, CStore> {
ReadGuard::map(self.untracked.cstore.read(), |r| r.as_any().downcast_ref().unwrap())
}
fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> {