Skip to content

Commit bd43458

Browse files
committed
Auto merge of #108992 - petrochenkov:qcstore2, r=cjgillot
resolve: Querify most cstore access methods (subset) A subset of #108346 that is not on a hot path in any way.
2 parents c54af45 + 4a61922 commit bd43458

File tree

11 files changed

+169
-250
lines changed

11 files changed

+169
-250
lines changed

compiler/rustc_interface/src/passes.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_plugin_impl as plugin;
2626
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
2727
use rustc_resolve::Resolver;
2828
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
29-
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, Untracked};
29+
use rustc_session::cstore::{MetadataLoader, Untracked};
3030
use rustc_session::output::filename_for_input;
3131
use rustc_session::search_paths::PathKind;
3232
use rustc_session::{Limit, Session};
@@ -442,13 +442,9 @@ fn escape_dep_env(symbol: Symbol) -> String {
442442
escaped
443443
}
444444

445-
fn write_out_deps(
446-
sess: &Session,
447-
cstore: &CrateStoreDyn,
448-
outputs: &OutputFilenames,
449-
out_filenames: &[PathBuf],
450-
) {
445+
fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) {
451446
// Write out dependency rules to the dep-info file if requested
447+
let sess = tcx.sess;
452448
if !sess.opts.output_types.contains_key(&OutputType::DepInfo) {
453449
return;
454450
}
@@ -496,9 +492,8 @@ fn write_out_deps(
496492
}
497493
}
498494

499-
let cstore = cstore.as_any().downcast_ref::<CStore>().unwrap();
500-
for cnum in cstore.crates_untracked() {
501-
let source = cstore.crate_source_untracked(cnum);
495+
for &cnum in tcx.crates(()) {
496+
let source = tcx.used_crate_source(cnum);
502497
if let Some((path, _)) = &source.dylib {
503498
files.push(escape_dep_filename(&path.display().to_string()));
504499
}
@@ -612,7 +607,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
612607
}
613608
}
614609

615-
write_out_deps(sess, &*tcx.cstore_untracked(), &outputs, &output_paths);
610+
write_out_deps(tcx, &outputs, &output_paths);
616611

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

compiler/rustc_metadata/src/creader.rs

+9-6
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::MappedReadGuard;
11+
use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, WriteGuard};
1212
use rustc_expand::base::SyntaxExtension;
1313
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1414
use rustc_hir::definitions::Definitions;
@@ -133,8 +133,14 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
133133

134134
impl CStore {
135135
pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> {
136-
MappedReadGuard::map(tcx.cstore_untracked(), |c| {
137-
c.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
136+
ReadGuard::map(tcx.untracked().cstore.read(), |cstore| {
137+
cstore.as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
138+
})
139+
}
140+
141+
pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> MappedWriteGuard<'_, CStore> {
142+
WriteGuard::map(tcx.untracked().cstore.write(), |cstore| {
143+
cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`")
138144
})
139145
}
140146

@@ -268,9 +274,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
268274
) -> Self {
269275
CrateLoader { tcx, cstore, used_extern_options }
270276
}
271-
pub fn cstore(&self) -> &CStore {
272-
&self.cstore
273-
}
274277

275278
fn existing_match(&self, name: Symbol, hash: Option<Svh>, kind: PathKind) -> Option<CrateNum> {
276279
for (cnum, data) in self.cstore.iter_crate_data() {

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use rustc_session::cstore::{
3030
};
3131
use rustc_session::Session;
3232
use rustc_span::hygiene::ExpnIndex;
33-
use rustc_span::source_map::{respan, Spanned};
3433
use rustc_span::symbol::{kw, Ident, Symbol};
3534
use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP};
3635

@@ -930,7 +929,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
930929
self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess))
931930
}
932931

933-
fn get_visibility(self, id: DefIndex) -> ty::Visibility<DefId> {
932+
fn get_visibility(self, id: DefIndex) -> Visibility<DefId> {
934933
self.root
935934
.tables
936935
.visibility
@@ -1134,33 +1133,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11341133
.decode((self, sess))
11351134
}
11361135

1137-
fn get_struct_field_names(
1138-
self,
1139-
id: DefIndex,
1140-
sess: &'a Session,
1141-
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
1142-
self.root
1143-
.tables
1144-
.children
1145-
.get(self, id)
1146-
.expect("fields not encoded for a struct")
1147-
.decode(self)
1148-
.map(move |index| respan(self.get_span(index, sess), self.item_name(index)))
1149-
}
1150-
1151-
fn get_struct_field_visibilities(
1152-
self,
1153-
id: DefIndex,
1154-
) -> impl Iterator<Item = Visibility<DefId>> + 'a {
1155-
self.root
1156-
.tables
1157-
.children
1158-
.get(self, id)
1159-
.expect("fields not encoded for a struct")
1160-
.decode(self)
1161-
.map(move |field_index| self.get_visibility(field_index))
1162-
}
1163-
11641136
fn get_inherent_implementations_for_type(
11651137
self,
11661138
tcx: TyCtxt<'tcx>,

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

+6-35
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use rustc_middle::middle::exported_symbols::ExportedSymbol;
1515
use rustc_middle::middle::stability::DeprecationEntry;
1616
use rustc_middle::ty::fast_reject::SimplifiedType;
1717
use rustc_middle::ty::query::{ExternProviders, Providers};
18-
use rustc_middle::ty::{self, TyCtxt, Visibility};
19-
use rustc_session::cstore::{CrateSource, CrateStore};
18+
use rustc_middle::ty::{self, TyCtxt};
19+
use rustc_session::cstore::CrateStore;
2020
use rustc_session::{Session, StableCrateId};
2121
use rustc_span::hygiene::{ExpnHash, ExpnId};
22-
use rustc_span::source_map::{Span, Spanned};
2322
use rustc_span::symbol::{kw, Symbol};
23+
use rustc_span::Span;
2424

2525
use rustc_data_structures::sync::Lrc;
2626
use std::any::Any;
@@ -501,35 +501,18 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
501501
tcx.arena
502502
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
503503
},
504-
crates: |tcx, ()| tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).crates_untracked()),
504+
crates: |tcx, ()| {
505+
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
506+
},
505507
..*providers
506508
};
507509
}
508510

509511
impl CStore {
510-
pub fn struct_field_names_untracked<'a>(
511-
&'a self,
512-
def: DefId,
513-
sess: &'a Session,
514-
) -> impl Iterator<Item = Spanned<Symbol>> + 'a {
515-
self.get_crate_data(def.krate).get_struct_field_names(def.index, sess)
516-
}
517-
518-
pub fn struct_field_visibilities_untracked(
519-
&self,
520-
def: DefId,
521-
) -> impl Iterator<Item = Visibility<DefId>> + '_ {
522-
self.get_crate_data(def.krate).get_struct_field_visibilities(def.index)
523-
}
524-
525512
pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
526513
self.get_crate_data(def.krate).get_ctor(def.index)
527514
}
528515

529-
pub fn visibility_untracked(&self, def: DefId) -> Visibility<DefId> {
530-
self.get_crate_data(def.krate).get_visibility(def.index)
531-
}
532-
533516
pub fn module_children_untracked<'a>(
534517
&'a self,
535518
def_id: DefId,
@@ -566,14 +549,6 @@ impl CStore {
566549
)
567550
}
568551

569-
pub fn fn_has_self_parameter_untracked(&self, def: DefId, sess: &Session) -> bool {
570-
self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index, sess)
571-
}
572-
573-
pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc<CrateSource> {
574-
self.get_crate_data(cnum).source.clone()
575-
}
576-
577552
pub fn get_span_untracked(&self, def_id: DefId, sess: &Session) -> Span {
578553
self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
579554
}
@@ -582,10 +557,6 @@ impl CStore {
582557
self.get_crate_data(def.krate).def_kind(def.index)
583558
}
584559

585-
pub fn crates_untracked(&self) -> impl Iterator<Item = CrateNum> + '_ {
586-
self.iter_crate_data().map(|(cnum, _)| cnum)
587-
}
588-
589560
pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
590561
self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
591562
}

compiler/rustc_resolve/src/build_reduced_graph.rs

+13-47
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use rustc_middle::metadata::ModChild;
2929
use rustc_middle::{bug, ty};
3030
use rustc_session::cstore::CrateStore;
3131
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
32-
use rustc_span::source_map::respan;
3332
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3433
use rustc_span::Span;
3534

@@ -130,12 +129,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
130129
};
131130

132131
let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess);
133-
let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess);
134132
Some(self.new_module(
135133
parent,
136134
ModuleKind::Def(def_kind, def_id, name),
137135
expn_id,
138-
span,
136+
self.def_span(def_id),
139137
// FIXME: Account for `#[no_implicit_prelude]` attributes.
140138
parent.map_or(false, |module| module.no_implicit_prelude),
141139
))
@@ -328,13 +326,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
328326
}
329327
}
330328

331-
fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
332-
let field_names = vdata
333-
.fields()
334-
.iter()
335-
.map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name)))
336-
.collect();
337-
self.r.field_names.insert(def_id, field_names);
329+
fn insert_field_def_ids(&mut self, def_id: LocalDefId, vdata: &ast::VariantData) {
330+
if vdata.fields().iter().any(|field| field.is_placeholder) {
331+
// The fields are not expanded yet.
332+
return;
333+
}
334+
let def_ids = vdata.fields().iter().map(|field| self.r.local_def_id(field.id).to_def_id());
335+
self.r.field_def_ids.insert(def_id, self.r.tcx.arena.alloc_from_iter(def_ids));
338336
}
339337

340338
fn insert_field_visibilities_local(&mut self, def_id: DefId, vdata: &ast::VariantData) {
@@ -346,12 +344,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
346344
self.r.field_visibility_spans.insert(def_id, field_vis);
347345
}
348346

349-
fn insert_field_names_extern(&mut self, def_id: DefId) {
350-
let field_names =
351-
self.r.cstore().struct_field_names_untracked(def_id, self.r.tcx.sess).collect();
352-
self.r.field_names.insert(def_id, field_names);
353-
}
354-
355347
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
356348
// If any statements are items, we need to create an anonymous module
357349
block
@@ -749,7 +741,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
749741
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
750742

751743
// Record field names for error reporting.
752-
self.insert_field_names_local(def_id, vdata);
744+
self.insert_field_def_ids(local_def_id, vdata);
753745
self.insert_field_visibilities_local(def_id, vdata);
754746

755747
// If this is a tuple or unit struct, define a name
@@ -789,7 +781,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
789781

790782
self.r
791783
.struct_constructors
792-
.insert(def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
784+
.insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
793785
}
794786
}
795787

@@ -798,7 +790,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
798790
self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion));
799791

800792
// Record field names for error reporting.
801-
self.insert_field_names_local(def_id, vdata);
793+
self.insert_field_def_ids(local_def_id, vdata);
802794
self.insert_field_visibilities_local(def_id, vdata);
803795
}
804796

@@ -1004,32 +996,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> {
1004996
| Res::SelfCtor(..)
1005997
| Res::Err => bug!("unexpected resolution: {:?}", res),
1006998
}
1007-
// Record some extra data for better diagnostics.
1008-
match res {
1009-
Res::Def(DefKind::Struct, def_id) => {
1010-
let cstore = self.r.cstore();
1011-
if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) {
1012-
let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
1013-
let ctor_vis = cstore.visibility_untracked(ctor_def_id);
1014-
let field_visibilities =
1015-
cstore.struct_field_visibilities_untracked(def_id).collect();
1016-
drop(cstore);
1017-
self.r
1018-
.struct_constructors
1019-
.insert(def_id, (ctor_res, ctor_vis, field_visibilities));
1020-
} else {
1021-
drop(cstore);
1022-
}
1023-
self.insert_field_names_extern(def_id)
1024-
}
1025-
Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id),
1026-
Res::Def(DefKind::AssocFn, def_id) => {
1027-
if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) {
1028-
self.r.has_self.insert(def_id);
1029-
}
1030-
}
1031-
_ => {}
1032-
}
1033999
}
10341000

10351001
fn add_macro_use_binding(
@@ -1426,7 +1392,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
14261392
AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS),
14271393
AssocItemKind::Fn(box Fn { ref sig, .. }) => {
14281394
if sig.decl.has_self() {
1429-
self.r.has_self.insert(def_id);
1395+
self.r.has_self.insert(local_def_id);
14301396
}
14311397
(DefKind::AssocFn, ValueNS)
14321398
}
@@ -1540,7 +1506,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> {
15401506
}
15411507

15421508
// Record field names for error reporting.
1543-
self.insert_field_names_local(def_id.to_def_id(), &variant.data);
1509+
self.insert_field_def_ids(def_id, &variant.data);
15441510
self.insert_field_visibilities_local(def_id.to_def_id(), &variant.data);
15451511

15461512
visit::walk_variant(self, variant);

0 commit comments

Comments
 (0)