Skip to content

Commit cdb7634

Browse files
authored
Rollup merge of #65625 - petrochenkov:cstore, r=Mark-Simulacrum,Zoxc
Turn crate store into a resolver output Crate store (`CStore`) is a vector of data (`CrateMetadata`) associated with extern crates loaded during the current compilation session. All crates are loaded in the resolver when resolving either paths pointing to extern prelude or `extern crate` items. (There are also a couple of crates like panic runtime that are loaded kind of like implicit `extern crate`s, but that also happens in resolve.) The use of `CStore` from `rustc_plugin` (which is outside of the resolver) was unnecessary because legacy plugins are not added to the crate store and don't use `CrateNum`s. So, `CStore` can be produced by the resolver instead of being kept in some really global data (`rustc_interface::Compiler`) like now. As a result of crate store being more "local" we can now remove some locks and `Lrc`s.
2 parents 8e0007f + 94216ce commit cdb7634

File tree

22 files changed

+346
-415
lines changed

22 files changed

+346
-415
lines changed

src/librustc/hir/lowering.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ pub struct LoweringContext<'a> {
8383
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
8484
sess: &'a Session,
8585

86-
cstore: &'a dyn CrateStore,
87-
8886
resolver: &'a mut dyn Resolver,
8987

9088
/// HACK(Centril): there is a cyclic dependency between the parser and lowering
@@ -160,6 +158,8 @@ pub struct LoweringContext<'a> {
160158
}
161159

162160
pub trait Resolver {
161+
fn cstore(&self) -> &dyn CrateStore;
162+
163163
/// Obtains resolution for a `NodeId` with a single resolution.
164164
fn get_partial_res(&mut self, id: NodeId) -> Option<PartialRes>;
165165

@@ -240,7 +240,6 @@ impl<'a> ImplTraitContext<'a> {
240240

241241
pub fn lower_crate(
242242
sess: &Session,
243-
cstore: &dyn CrateStore,
244243
dep_graph: &DepGraph,
245244
krate: &Crate,
246245
resolver: &mut dyn Resolver,
@@ -256,7 +255,6 @@ pub fn lower_crate(
256255
LoweringContext {
257256
crate_root: sess.parse_sess.injected_crate_name.try_get().copied(),
258257
sess,
259-
cstore,
260258
resolver,
261259
nt_to_tokenstream,
262260
items: BTreeMap::new(),
@@ -980,7 +978,7 @@ impl<'a> LoweringContext<'a> {
980978
if id.is_local() {
981979
self.resolver.definitions().def_key(id.index)
982980
} else {
983-
self.cstore.def_key(id)
981+
self.resolver.cstore().def_key(id)
984982
}
985983
}
986984

@@ -1727,8 +1725,8 @@ impl<'a> LoweringContext<'a> {
17271725
return n;
17281726
}
17291727
assert!(!def_id.is_local());
1730-
let item_generics =
1731-
self.cstore.item_generics_cloned_untracked(def_id, self.sess);
1728+
let item_generics = self.resolver.cstore()
1729+
.item_generics_cloned_untracked(def_id, self.sess);
17321730
let n = item_generics.own_counts().lifetimes;
17331731
self.type_def_lifetime_params.insert(def_id, n);
17341732
n

src/librustc/middle/cstore.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use syntax::ast;
1616
use syntax::symbol::Symbol;
1717
use syntax_pos::Span;
1818
use rustc_target::spec::Target;
19-
use rustc_data_structures::sync::{self, MetadataRef, Lrc};
19+
use rustc_data_structures::sync::{self, MetadataRef};
2020
use rustc_macros::HashStable;
2121

2222
pub use self::NativeLibraryKind::*;
@@ -191,6 +191,8 @@ pub trait MetadataLoader {
191191
-> Result<MetadataRef, String>;
192192
}
193193

194+
pub type MetadataLoaderDyn = dyn MetadataLoader + Sync;
195+
194196
/// A store of Rust crates, through which their metadata can be accessed.
195197
///
196198
/// Note that this trait should probably not be expanding today. All new
@@ -201,13 +203,13 @@ pub trait MetadataLoader {
201203
/// (it'd break incremental compilation) and should only be called pre-HIR (e.g.
202204
/// during resolve)
203205
pub trait CrateStore {
204-
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Lrc<dyn Any>;
206+
fn crate_data_as_any(&self, cnum: CrateNum) -> &dyn Any;
205207

206208
// resolve
207209
fn def_key(&self, def: DefId) -> DefKey;
208210
fn def_path(&self, def: DefId) -> hir_map::DefPath;
209211
fn def_path_hash(&self, def: DefId) -> hir_map::DefPathHash;
210-
fn def_path_table(&self, cnum: CrateNum) -> Lrc<DefPathTable>;
212+
fn def_path_table(&self, cnum: CrateNum) -> &DefPathTable;
211213

212214
// "queries" used in resolve that aren't tracked for incremental compilation
213215
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;

src/librustc/ty/context.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ pub struct GlobalCtxt<'tcx> {
10271027

10281028
interners: CtxtInterners<'tcx>,
10291029

1030-
cstore: &'tcx CrateStoreDyn,
1030+
cstore: Box<CrateStoreDyn>,
10311031

10321032
pub sess: &'tcx Session,
10331033

@@ -1195,11 +1195,10 @@ impl<'tcx> TyCtxt<'tcx> {
11951195
pub fn create_global_ctxt(
11961196
s: &'tcx Session,
11971197
lint_store: Lrc<lint::LintStore>,
1198-
cstore: &'tcx CrateStoreDyn,
11991198
local_providers: ty::query::Providers<'tcx>,
12001199
extern_providers: ty::query::Providers<'tcx>,
12011200
arenas: &'tcx AllArenas,
1202-
resolutions: ty::Resolutions,
1201+
resolutions: ty::ResolverOutputs,
12031202
hir: hir_map::Map<'tcx>,
12041203
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
12051204
crate_name: &str,
@@ -1213,34 +1212,28 @@ impl<'tcx> TyCtxt<'tcx> {
12131212
let common_lifetimes = CommonLifetimes::new(&interners);
12141213
let common_consts = CommonConsts::new(&interners, &common_types);
12151214
let dep_graph = hir.dep_graph.clone();
1216-
let max_cnum = cstore.crates_untracked().iter().map(|c| c.as_usize()).max().unwrap_or(0);
1215+
let cstore = resolutions.cstore;
1216+
let crates = cstore.crates_untracked();
1217+
let max_cnum = crates.iter().map(|c| c.as_usize()).max().unwrap_or(0);
12171218
let mut providers = IndexVec::from_elem_n(extern_providers, max_cnum + 1);
12181219
providers[LOCAL_CRATE] = local_providers;
12191220

12201221
let def_path_hash_to_def_id = if s.opts.build_dep_graph() {
1221-
let upstream_def_path_tables: Vec<(CrateNum, Lrc<_>)> = cstore
1222-
.crates_untracked()
1222+
let def_path_tables = crates
12231223
.iter()
12241224
.map(|&cnum| (cnum, cstore.def_path_table(cnum)))
1225-
.collect();
1226-
1227-
let def_path_tables = || {
1228-
upstream_def_path_tables
1229-
.iter()
1230-
.map(|&(cnum, ref rc)| (cnum, &**rc))
1231-
.chain(iter::once((LOCAL_CRATE, hir.definitions().def_path_table())))
1232-
};
1225+
.chain(iter::once((LOCAL_CRATE, hir.definitions().def_path_table())));
12331226

12341227
// Precompute the capacity of the hashmap so we don't have to
12351228
// re-allocate when populating it.
1236-
let capacity = def_path_tables().map(|(_, t)| t.size()).sum::<usize>();
1229+
let capacity = def_path_tables.clone().map(|(_, t)| t.size()).sum::<usize>();
12371230

12381231
let mut map: FxHashMap<_, _> = FxHashMap::with_capacity_and_hasher(
12391232
capacity,
12401233
::std::default::Default::default()
12411234
);
12421235

1243-
for (cnum, def_path_table) in def_path_tables() {
1236+
for (cnum, def_path_table) in def_path_tables {
12441237
def_path_table.add_def_path_hashes_to(cnum, &mut map);
12451238
}
12461239

@@ -1417,8 +1410,8 @@ impl<'tcx> TyCtxt<'tcx> {
14171410

14181411
// Note that this is *untracked* and should only be used within the query
14191412
// system if the result is otherwise tracked through queries
1420-
pub fn crate_data_as_rc_any(self, cnum: CrateNum) -> Lrc<dyn Any> {
1421-
self.cstore.crate_data_as_rc_any(cnum)
1413+
pub fn crate_data_as_any(self, cnum: CrateNum) -> &'tcx dyn Any {
1414+
self.cstore.crate_data_as_any(cnum)
14221415
}
14231416

14241417
#[inline(always)]
@@ -1428,7 +1421,7 @@ impl<'tcx> TyCtxt<'tcx> {
14281421
StableHashingContext::new(self.sess,
14291422
krate,
14301423
self.hir().definitions(),
1431-
self.cstore)
1424+
&*self.cstore)
14321425
}
14331426

14341427
// This method makes sure that we have a DepNode and a Fingerprint for

src/librustc/ty/mod.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_macros::HashStable;
1515
use crate::ich::Fingerprint;
1616
use crate::ich::StableHashingContext;
1717
use crate::infer::canonical::Canonical;
18+
use crate::middle::cstore::CrateStoreDyn;
1819
use crate::middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
1920
use crate::middle::resolve_lifetime::ObjectLifetimeDefault;
2021
use crate::mir::Body;
@@ -119,8 +120,9 @@ mod sty;
119120

120121
// Data types
121122

122-
#[derive(Clone)]
123-
pub struct Resolutions {
123+
pub struct ResolverOutputs {
124+
pub definitions: hir_map::Definitions,
125+
pub cstore: Box<CrateStoreDyn>,
124126
pub extern_crate_map: NodeMap<CrateNum>,
125127
pub trait_map: TraitMap,
126128
pub maybe_unused_trait_imports: NodeSet,

src/librustc_codegen_llvm/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use std::sync::Arc;
5656
use std::ffi::CStr;
5757

5858
use rustc::dep_graph::DepGraph;
59-
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
59+
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
6060
use rustc::session::Session;
6161
use rustc::session::config::{OutputFilenames, OutputType, PrintRequest, OptLevel};
6262
use rustc::ty::{self, TyCtxt};
@@ -260,7 +260,7 @@ impl CodegenBackend for LlvmCodegenBackend {
260260
target_features(sess)
261261
}
262262

263-
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
263+
fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
264264
box metadata::LlvmMetadataLoader
265265
}
266266

src/librustc_codegen_utils/codegen_backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc::util::common::ErrorReported;
1414
use rustc::session::config::{OutputFilenames, PrintRequest};
1515
use rustc::ty::TyCtxt;
1616
use rustc::ty::query::Providers;
17-
use rustc::middle::cstore::{EncodedMetadata, MetadataLoader};
17+
use rustc::middle::cstore::{EncodedMetadata, MetadataLoaderDyn};
1818
use rustc::dep_graph::DepGraph;
1919

2020
pub use rustc_data_structures::sync::MetadataRef;
@@ -26,7 +26,7 @@ pub trait CodegenBackend {
2626
fn print_passes(&self) {}
2727
fn print_version(&self) {}
2828

29-
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync>;
29+
fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
3030
fn provide(&self, _providers: &mut Providers<'_>);
3131
fn provide_extern(&self, _providers: &mut Providers<'_>);
3232
fn codegen_crate<'tcx>(

src/librustc_driver/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ use rustc::session::config::nightly_options;
3636
use rustc::session::{early_error, early_warn};
3737
use rustc::lint::Lint;
3838
use rustc::lint;
39+
use rustc::middle::cstore::MetadataLoader;
3940
use rustc::hir::def_id::LOCAL_CRATE;
4041
use rustc::ty::TyCtxt;
4142
use rustc::util::common::{set_time_depth, time, print_time_passes_entry, ErrorReported};
4243
use rustc_metadata::locator;
43-
use rustc_metadata::cstore::CStore;
4444
use rustc_codegen_utils::codegen_backend::CodegenBackend;
4545
use rustc_interface::interface;
4646
use rustc_interface::util::get_codegen_sysroot;
@@ -277,7 +277,7 @@ pub fn run_compiler(
277277
compiler.output_file(),
278278
).and_then(|| RustcDefaultCalls::list_metadata(
279279
sess,
280-
compiler.cstore(),
280+
&*compiler.codegen_backend().metadata_loader(),
281281
&matches,
282282
compiler.input()
283283
));
@@ -614,7 +614,7 @@ fn show_content_with_pager(content: &String) {
614614

615615
impl RustcDefaultCalls {
616616
pub fn list_metadata(sess: &Session,
617-
cstore: &CStore,
617+
metadata_loader: &dyn MetadataLoader,
618618
matches: &getopts::Matches,
619619
input: &Input)
620620
-> Compilation {
@@ -626,7 +626,7 @@ impl RustcDefaultCalls {
626626
let mut v = Vec::new();
627627
locator::list_file_metadata(&sess.target.target,
628628
path,
629-
cstore,
629+
metadata_loader,
630630
&mut v)
631631
.unwrap();
632632
println!("{}", String::from_utf8(v).unwrap());

src/librustc_interface/interface.rs

-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use rustc_codegen_utils::codegen_backend::CodegenBackend;
1111
use rustc_data_structures::OnDrop;
1212
use rustc_data_structures::sync::Lrc;
1313
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
14-
use rustc_metadata::cstore::CStore;
1514
use std::path::PathBuf;
1615
use std::result;
1716
use std::sync::{Arc, Mutex};
@@ -37,7 +36,6 @@ pub struct Compiler {
3736
pub(crate) output_dir: Option<PathBuf>,
3837
pub(crate) output_file: Option<PathBuf>,
3938
pub(crate) queries: Queries,
40-
pub(crate) cstore: Lrc<CStore>,
4139
pub(crate) crate_name: Option<String>,
4240
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
4341
}
@@ -49,9 +47,6 @@ impl Compiler {
4947
pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
5048
&self.codegen_backend
5149
}
52-
pub fn cstore(&self) -> &Lrc<CStore> {
53-
&self.cstore
54-
}
5550
pub fn source_map(&self) -> &Lrc<SourceMap> {
5651
&self.source_map
5752
}
@@ -160,13 +155,10 @@ where
160155
config.lint_caps,
161156
);
162157

163-
let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
164-
165158
let compiler = Compiler {
166159
sess,
167160
codegen_backend,
168161
source_map,
169-
cstore,
170162
input: config.input,
171163
input_path: config.input_path,
172164
output_dir: config.output_dir,

0 commit comments

Comments
 (0)