Skip to content

Commit 40155f5

Browse files
Rollup merge of rust-lang#87117 - cjgillot:cstore, r=petrochenkov
Shrink the CrateStore dynamic interface. The information is either accessible through queries or by crates which already depend on rustc_metadata.
2 parents 57e021c + 3fba5a4 commit 40155f5

File tree

9 files changed

+39
-56
lines changed

9 files changed

+39
-56
lines changed

compiler/rustc_interface/src/passes.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_metadata::creader::CStore;
1818
use rustc_middle::arena::Arena;
1919
use rustc_middle::dep_graph::DepGraph;
2020
use rustc_middle::middle;
21-
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
21+
use rustc_middle::middle::cstore::{MetadataLoader, MetadataLoaderDyn};
2222
use rustc_middle::ty::query::Providers;
2323
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
2424
use rustc_mir as mir;
@@ -860,11 +860,7 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
860860
tcx.ensure().proc_macro_decls_static(())
861861
});
862862

863-
let cstore = tcx
864-
.cstore_as_any()
865-
.downcast_ref::<CStore>()
866-
.expect("`tcx.cstore` is not a `CStore`");
867-
cstore.report_unused_deps(tcx);
863+
CStore::from_tcx(tcx).report_unused_deps(tcx);
868864
},
869865
{
870866
par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {

compiler/rustc_metadata/src/creader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
130130
}
131131

132132
impl CStore {
133-
crate fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
133+
pub fn from_tcx(tcx: TyCtxt<'_>) -> &CStore {
134134
tcx.cstore_as_any().downcast_ref::<CStore>().expect("`tcx.cstore` is not a `CStore`")
135135
}
136136

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

+13-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::rmeta::encoder;
55

66
use rustc_ast as ast;
77
use rustc_data_structures::stable_map::FxHashMap;
8-
use rustc_data_structures::svh::Svh;
98
use rustc_hir as hir;
109
use rustc_hir::def::{CtorKind, DefKind};
1110
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -369,6 +368,7 @@ pub fn provide(providers: &mut Providers) {
369368
tcx.arena
370369
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
371370
},
371+
crates: |tcx, ()| tcx.arena.alloc_slice(&CStore::from_tcx(tcx).crates_untracked()),
372372

373373
..*providers
374374
};
@@ -451,6 +451,16 @@ impl CStore {
451451
self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
452452
}
453453

454+
pub fn def_kind(&self, def: DefId) -> DefKind {
455+
self.get_crate_data(def.krate).def_kind(def.index)
456+
}
457+
458+
pub fn crates_untracked(&self) -> Vec<CrateNum> {
459+
let mut result = vec![];
460+
self.iter_crate_data(|cnum, _| result.push(cnum));
461+
result
462+
}
463+
454464
pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize {
455465
self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes
456466
}
@@ -485,29 +495,21 @@ impl CrateStore for CStore {
485495
self
486496
}
487497

488-
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol {
498+
fn crate_name(&self, cnum: CrateNum) -> Symbol {
489499
self.get_crate_data(cnum).root.name
490500
}
491501

492-
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId {
502+
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId {
493503
self.get_crate_data(cnum).root.stable_crate_id
494504
}
495505

496-
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh {
497-
self.get_crate_data(cnum).root.hash
498-
}
499-
500506
/// Returns the `DefKey` for a given `DefId`. This indicates the
501507
/// parent `DefId` as well as some idea of what kind of data the
502508
/// `DefId` refers to.
503509
fn def_key(&self, def: DefId) -> DefKey {
504510
self.get_crate_data(def.krate).def_key(def.index)
505511
}
506512

507-
fn def_kind(&self, def: DefId) -> DefKind {
508-
self.get_crate_data(def.krate).def_kind(def.index)
509-
}
510-
511513
fn def_path(&self, def: DefId) -> DefPath {
512514
self.get_crate_data(def.krate).def_path(def.index)
513515
}
@@ -526,12 +528,6 @@ impl CrateStore for CStore {
526528
self.get_crate_data(cnum).def_path_hash_to_def_id(cnum, index_guess, hash)
527529
}
528530

529-
fn crates_untracked(&self) -> Vec<CrateNum> {
530-
let mut result = vec![];
531-
self.iter_crate_data(|cnum, _| result.push(cnum));
532-
result
533-
}
534-
535531
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata {
536532
encoder::encode_metadata(tcx)
537533
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1061,10 +1061,7 @@ impl EncodeContext<'a, 'tcx> {
10611061
Lazy::empty()
10621062
};
10631063

1064-
let data = ModData {
1065-
reexports,
1066-
expansion: tcx.resolutions(()).definitions.expansion_that_defined(local_def_id),
1067-
};
1064+
let data = ModData { reexports, expansion: tcx.expn_that_defined(local_def_id) };
10681065

10691066
record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
10701067
if self.is_proc_macro {

compiler/rustc_middle/src/hir/map/mod.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use self::collector::NodeCollector;
22

33
use crate::hir::{AttributeMap, IndexedHir};
4-
use crate::middle::cstore::CrateStore;
54
use crate::ty::TyCtxt;
65
use rustc_ast as ast;
76
use rustc_data_structures::fingerprint::Fingerprint;
@@ -991,7 +990,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
991990
},
992991
);
993992

994-
let upstream_crates = upstream_crates(&*tcx.untracked_resolutions.cstore);
993+
let upstream_crates = upstream_crates(tcx);
995994

996995
// We hash the final, remapped names of all local source files so we
997996
// don't have to include the path prefix remapping commandline args.
@@ -1021,13 +1020,13 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
10211020
Svh::new(crate_hash.to_smaller_hash())
10221021
}
10231022

1024-
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(StableCrateId, Svh)> {
1025-
let mut upstream_crates: Vec<_> = cstore
1026-
.crates_untracked()
1023+
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1024+
let mut upstream_crates: Vec<_> = tcx
1025+
.crates(())
10271026
.iter()
10281027
.map(|&cnum| {
1029-
let stable_crate_id = cstore.stable_crate_id_untracked(cnum);
1030-
let hash = cstore.crate_hash_untracked(cnum);
1028+
let stable_crate_id = tcx.resolutions(()).cstore.stable_crate_id(cnum);
1029+
let hash = tcx.crate_hash(cnum);
10311030
(stable_crate_id, hash)
10321031
})
10331032
.collect();

compiler/rustc_middle/src/middle/cstore.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
use crate::ty::TyCtxt;
66

77
use rustc_ast as ast;
8-
use rustc_data_structures::svh::Svh;
98
use rustc_data_structures::sync::{self, MetadataRef};
10-
use rustc_hir::def::DefKind;
119
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1210
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1311
use rustc_macros::HashStable;
@@ -190,27 +188,26 @@ pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
190188
pub trait CrateStore: std::fmt::Debug {
191189
fn as_any(&self) -> &dyn Any;
192190

193-
// resolve
191+
// Foreign definitions.
192+
// This information is safe to access, since it's hashed as part of the DefPathHash, which incr.
193+
// comp. uses to identify a DefId.
194194
fn def_key(&self, def: DefId) -> DefKey;
195-
fn def_kind(&self, def: DefId) -> DefKind;
196195
fn def_path(&self, def: DefId) -> DefPath;
197196
fn def_path_hash(&self, def: DefId) -> DefPathHash;
197+
198+
// This information is safe to access, since it's hashed as part of the StableCrateId, which
199+
// incr. comp. uses to identify a CrateNum.
200+
fn crate_name(&self, cnum: CrateNum) -> Symbol;
201+
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
202+
203+
/// Fetch a DefId from a DefPathHash for a foreign crate.
198204
fn def_path_hash_to_def_id(
199205
&self,
200206
cnum: CrateNum,
201207
index_guess: u32,
202208
hash: DefPathHash,
203209
) -> Option<DefId>;
204210

205-
// "queries" used in resolve that aren't tracked for incremental compilation
206-
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
207-
fn stable_crate_id_untracked(&self, cnum: CrateNum) -> StableCrateId;
208-
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
209-
210-
// This is basically a 1-based range of ints, which is a little
211-
// silly - I may fix that.
212-
fn crates_untracked(&self) -> Vec<CrateNum>;
213-
214211
// utility functions
215212
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
216213
}

compiler/rustc_middle/src/query/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ rustc_queries! {
213213
}
214214

215215
query expn_that_defined(key: DefId) -> rustc_span::ExpnId {
216+
// This query reads from untracked data in definitions.
217+
eval_always
216218
desc { |tcx| "expansion that defined `{}`", tcx.def_path_str(key) }
217219
}
218220

@@ -1446,6 +1448,7 @@ rustc_queries! {
14461448
desc { "calculating the stability index for the local crate" }
14471449
}
14481450
query crates(_: ()) -> &'tcx [CrateNum] {
1451+
eval_always
14491452
desc { "fetching all foreign CrateNum instances" }
14501453
}
14511454

compiler/rustc_middle/src/ty/context.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ impl<'tcx> TyCtxt<'tcx> {
12771277
if crate_num == LOCAL_CRATE {
12781278
self.sess.local_stable_crate_id()
12791279
} else {
1280-
self.untracked_resolutions.cstore.stable_crate_id_untracked(crate_num)
1280+
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
12811281
}
12821282
}
12831283

@@ -1290,10 +1290,7 @@ impl<'tcx> TyCtxt<'tcx> {
12901290
(self.crate_name, self.sess.local_stable_crate_id())
12911291
} else {
12921292
let cstore = &self.untracked_resolutions.cstore;
1293-
(
1294-
cstore.crate_name_untracked(def_id.krate),
1295-
cstore.stable_crate_id_untracked(def_id.krate),
1296-
)
1293+
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
12971294
};
12981295

12991296
format!(
@@ -2831,8 +2828,6 @@ pub fn provide(providers: &mut ty::query::Providers) {
28312828
};
28322829
providers.extern_mod_stmt_cnum =
28332830
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
2834-
providers.crates =
2835-
|tcx, ()| tcx.arena.alloc_slice(&tcx.resolutions(()).cstore.crates_untracked());
28362831
providers.output_filenames = |tcx, ()| tcx.output_filenames.clone();
28372832
providers.features_query = |tcx, ()| tcx.sess.features_untracked();
28382833
providers.is_panic_runtime = |tcx, cnum| {

compiler/rustc_resolve/src/build_reduced_graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a> Resolver<'a> {
128128

129129
let (name, parent) = if def_id.index == CRATE_DEF_INDEX {
130130
// This is the crate root
131-
(self.cstore().crate_name_untracked(def_id.krate), None)
131+
(self.cstore().crate_name(def_id.krate), None)
132132
} else {
133133
let def_key = self.cstore().def_key(def_id);
134134
let name = def_key

0 commit comments

Comments
 (0)