Skip to content

Commit ade3dce

Browse files
committed
Make untracked.cstore lockable so that resolution can still write to it when using TyCtxt
1 parent e8e227a commit ade3dce

File tree

9 files changed

+50
-36
lines changed

9 files changed

+50
-36
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
597597
}
598598
}
599599

600-
write_out_deps(sess, tcx.cstore_untracked(), &outputs, &output_paths);
600+
write_out_deps(sess, &*tcx.cstore_untracked(), &outputs, &output_paths);
601601

602602
let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo)
603603
&& sess.opts.output_types.len() == 1;

compiler/rustc_interface/src/queries.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ impl<'tcx> Queries<'tcx> {
215215
ast_lowering: untracked_resolver_for_lowering,
216216
} = resolver_outputs;
217217

218+
// Make sure we don't mutate the cstore from here on.
219+
std::mem::forget(untracked.cstore.read());
220+
218221
let gcx = passes::create_global_ctxt(
219222
self.compiler,
220223
lint_store,

compiler/rustc_metadata/src/creader.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind;
88
use rustc_ast::{self as ast, *};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::svh::Svh;
11-
use rustc_data_structures::sync::ReadGuard;
11+
use rustc_data_structures::sync::{MappedReadGuard, ReadGuard};
1212
use rustc_expand::base::SyntaxExtension;
1313
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1414
use rustc_hir::definitions::Definitions;
@@ -127,11 +127,10 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
127127
}
128128

129129
impl CStore {
130-
pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
131-
tcx.cstore_untracked()
132-
.as_any()
133-
.downcast_ref::<CStore>()
134-
.expect("`tcx.cstore` is not a `CStore`")
130+
pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> {
131+
MappedReadGuard::map(tcx.cstore_untracked(), |c| {
132+
c.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
133+
})
135134
}
136135

137136
fn alloc_new_crate_num(&mut self) -> CrateNum {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ macro_rules! provide_one {
130130
$tcx.ensure().crate_hash($def_id.krate);
131131
}
132132

133-
let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate);
133+
let cdata = rustc_data_structures::sync::MappedReadGuard::map(CStore::from_tcx($tcx), |c| {
134+
c.get_crate_data($def_id.krate).cdata
135+
});
136+
let $cdata = crate::creader::CrateMetadataRef {
137+
cdata: &cdata,
138+
cstore: &CStore::from_tcx($tcx),
139+
};
134140

135141
$compute
136142
}

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use rustc_data_structures::profiling::SelfProfilerRef;
3636
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
3737
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3838
use rustc_data_structures::steal::Steal;
39-
use rustc_data_structures::sync::{self, Lock, Lrc, ReadGuard, WorkerLocal};
39+
use rustc_data_structures::sync::{self, Lock, Lrc, MappedReadGuard, ReadGuard, WorkerLocal};
4040
use rustc_errors::{
4141
DecorateLint, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, MultiSpan,
4242
};
@@ -836,7 +836,7 @@ impl<'tcx> TyCtxt<'tcx> {
836836
if let Some(id) = id.as_local() {
837837
self.definitions_untracked().def_key(id)
838838
} else {
839-
self.untracked.cstore.def_key(id)
839+
self.cstore_untracked().def_key(id)
840840
}
841841
}
842842

@@ -850,7 +850,7 @@ impl<'tcx> TyCtxt<'tcx> {
850850
if let Some(id) = id.as_local() {
851851
self.definitions_untracked().def_path(id)
852852
} else {
853-
self.untracked.cstore.def_path(id)
853+
self.cstore_untracked().def_path(id)
854854
}
855855
}
856856

@@ -860,7 +860,7 @@ impl<'tcx> TyCtxt<'tcx> {
860860
if let Some(def_id) = def_id.as_local() {
861861
self.definitions_untracked().def_path_hash(def_id)
862862
} else {
863-
self.untracked.cstore.def_path_hash(def_id)
863+
self.cstore_untracked().def_path_hash(def_id)
864864
}
865865
}
866866

@@ -869,7 +869,7 @@ impl<'tcx> TyCtxt<'tcx> {
869869
if crate_num == LOCAL_CRATE {
870870
self.sess.local_stable_crate_id()
871871
} else {
872-
self.untracked.cstore.stable_crate_id(crate_num)
872+
self.cstore_untracked().stable_crate_id(crate_num)
873873
}
874874
}
875875

@@ -880,7 +880,7 @@ impl<'tcx> TyCtxt<'tcx> {
880880
if stable_crate_id == self.sess.local_stable_crate_id() {
881881
LOCAL_CRATE
882882
} else {
883-
self.untracked.cstore.stable_crate_id_to_crate_num(stable_crate_id)
883+
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
884884
}
885885
}
886886

@@ -899,7 +899,7 @@ impl<'tcx> TyCtxt<'tcx> {
899899
} else {
900900
// If this is a DefPathHash from an upstream crate, let the CrateStore map
901901
// it to a DefId.
902-
let cstore = &*self.untracked.cstore;
902+
let cstore = &*self.cstore_untracked();
903903
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
904904
cstore.def_path_hash_to_def_id(cnum, hash)
905905
}
@@ -913,7 +913,7 @@ impl<'tcx> TyCtxt<'tcx> {
913913
let (crate_name, stable_crate_id) = if def_id.is_local() {
914914
(self.crate_name(LOCAL_CRATE), self.sess.local_stable_crate_id())
915915
} else {
916-
let cstore = &*self.untracked.cstore;
916+
let cstore = &*self.cstore_untracked();
917917
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
918918
};
919919

@@ -1011,8 +1011,8 @@ impl<'tcx> TyCtxt<'tcx> {
10111011

10121012
/// Note that this is *untracked* and should only be used within the query
10131013
/// system if the result is otherwise tracked through queries
1014-
pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
1015-
&*self.untracked.cstore
1014+
pub fn cstore_untracked(self) -> MappedReadGuard<'tcx, CrateStoreDyn> {
1015+
ReadGuard::map(self.untracked.cstore.read(), |c| &**c)
10161016
}
10171017

10181018
/// Note that this is *untracked* and should only be used within the query

compiler/rustc_query_system/src/ich/hcx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> StableHashingContext<'a> {
9090
if let Some(def_id) = def_id.as_local() {
9191
self.local_def_path_hash(def_id)
9292
} else {
93-
self.untracked.cstore.def_path_hash(def_id)
93+
self.untracked.cstore.read().def_path_hash(def_id)
9494
}
9595
}
9696

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
130130
def_key.disambiguated_data.data.get_opt_name().expect("module without name")
131131
};
132132

133+
let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess);
134+
let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess);
133135
Some(self.new_module(
134136
parent,
135137
ModuleKind::Def(def_kind, def_id, name),
136-
self.cstore().module_expansion_untracked(def_id, &self.tcx.sess),
137-
self.cstore().get_span_untracked(def_id, &self.tcx.sess),
138+
expn_id,
139+
span,
138140
// FIXME: Account for `#[no_implicit_prelude]` attributes.
139141
parent.map_or(false, |module| module.no_implicit_prelude),
140142
))
@@ -179,7 +181,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
179181
return macro_data.clone();
180182
}
181183

182-
let (ext, macro_rules) = match self.cstore().load_macro_untracked(def_id, &self.tcx.sess) {
184+
let load_macro_untracked = self.cstore().load_macro_untracked(def_id, &self.tcx.sess);
185+
let (ext, macro_rules) = match load_macro_untracked {
183186
LoadedMacro::MacroDef(item, edition) => (
184187
Lrc::new(self.compile_macro(&item, edition).0),
185188
matches!(item.kind, ItemKind::MacroDef(def) if def.macro_rules),
@@ -204,9 +207,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
204207
}
205208

206209
pub(crate) fn build_reduced_graph_external(&mut self, module: Module<'a>) {
207-
for child in
208-
Vec::from_iter(self.cstore().module_children_untracked(module.def_id(), self.tcx.sess))
209-
{
210+
let children =
211+
Vec::from_iter(self.cstore().module_children_untracked(module.def_id(), self.tcx.sess));
212+
for child in children {
210213
let parent_scope = ParentScope::module(module, self);
211214
BuildReducedGraphVisitor { r: self, parent_scope }
212215
.build_reduced_graph_for_external_crate_res(child);
@@ -1000,23 +1003,26 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
10001003
| Res::Err => bug!("unexpected resolution: {:?}", res),
10011004
}
10021005
// Record some extra data for better diagnostics.
1003-
let cstore = self.r.cstore();
10041006
match res {
10051007
Res::Def(DefKind::Struct, def_id) => {
1008+
let cstore = self.r.cstore();
10061009
if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) {
10071010
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
10081011
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
10091012
let field_visibilities =
10101013
cstore.struct_field_visibilities_untracked(def_id).collect();
1014+
drop(cstore);
10111015
self.r
10121016
.struct_constructors
10131017
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
1018+
} else {
1019+
drop(cstore);
10141020
}
10151021
self.insert_field_names_extern(def_id)
10161022
}
10171023
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
10181024
Res::Def(DefKind::AssocFn, def_id) => {
1019-
if cstore.fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {
1025+
if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {
10201026
self.r.has_self.insert(def_id);
10211027
}
10221028
}

compiler/rustc_resolve/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
2727
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
2929
use rustc_data_structures::intern::Interned;
30-
use rustc_data_structures::sync::{Lrc, RwLock};
30+
use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard, RwLock};
3131
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
3232
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
3333
use rustc_hir::def::Namespace::{self, *};
@@ -1132,7 +1132,7 @@ impl DefIdTree for ResolverTree<'_> {
11321132
let ResolverTree(Untracked { definitions, cstore, .. }) = self;
11331133
match id.as_local() {
11341134
Some(id) => definitions.read().def_key(id).parent,
1135-
None => cstore.as_any().downcast_ref::<CStore>().unwrap().def_key(id).parent,
1135+
None => cstore.read().as_any().downcast_ref::<CStore>().unwrap().def_key(id).parent,
11361136
}
11371137
.map(|index| DefId { index, ..id })
11381138
}
@@ -1328,7 +1328,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13281328
local_crate_name: crate_name,
13291329
used_extern_options: Default::default(),
13301330
untracked: Untracked {
1331-
cstore: Box::new(CStore::new(session)),
1331+
cstore: RwLock::new(Box::new(CStore::new(session))),
13321332
source_span,
13331333
definitions: RwLock::new(definitions),
13341334
},
@@ -1487,14 +1487,14 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14871487
&self.tcx.sess,
14881488
&*self.metadata_loader,
14891489
self.local_crate_name,
1490-
&mut *self.untracked.cstore.untracked_as_any().downcast_mut().unwrap(),
1490+
&mut *self.untracked.cstore.write().untracked_as_any().downcast_mut().unwrap(),
14911491
self.untracked.definitions.read(),
14921492
&mut self.used_extern_options,
14931493
))
14941494
}
14951495

1496-
fn cstore(&self) -> &CStore {
1497-
self.untracked.cstore.as_any().downcast_ref().unwrap()
1496+
fn cstore(&self) -> MappedReadGuard<'_, CStore> {
1497+
ReadGuard::map(self.untracked.cstore.read(), |r| r.as_any().downcast_ref().unwrap())
14981498
}
14991499

15001500
fn dummy_ext(&self, macro_kind: MacroKind) -> Lrc<SyntaxExtension> {

compiler/rustc_session/src/cstore.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub trait MetadataLoader {
205205
fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String>;
206206
}
207207

208-
pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
208+
pub type MetadataLoaderDyn = dyn MetadataLoader + Send + Sync;
209209

210210
/// A store of Rust crates, through which their metadata can be accessed.
211211
///
@@ -250,11 +250,11 @@ pub trait CrateStore: std::fmt::Debug {
250250
fn import_source_files(&self, sess: &Session, cnum: CrateNum);
251251
}
252252

253-
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
253+
pub type CrateStoreDyn = dyn CrateStore + sync::Sync + sync::Send;
254254

255255
#[derive(Debug)]
256256
pub struct Untracked {
257-
pub cstore: Box<CrateStoreDyn>,
257+
pub cstore: RwLock<Box<CrateStoreDyn>>,
258258
/// Reference span for definitions.
259259
pub source_span: IndexVec<LocalDefId, Span>,
260260
pub definitions: RwLock<Definitions>,

0 commit comments

Comments
 (0)