Skip to content

Commit 6742e2b

Browse files
committed
Auto merge of #114578 - petrochenkov:noplugin, r=cjgillot
rustc_interface: Dismantle `register_plugins` query It did three independent things: - Constructed `LintStore` - Prepared incremental directories and dep graph - Initialized some fields in `Session` The `LintStore` construction (now `passes::create_lint_store`) is more or less left in place. The incremental stuff is now moved into `fn dep_graph_future`. This helps us to start loading the dep graph a bit earlier. The `Session` field initialization is moved to tcx construction point. Now that tcx is constructed early these fields don't even need to live in `Session`, they can live in tcx instead and be initialized at its creation (see the FIXME). Three previously existing `rustc_interface` queries are de-querified (`register_plugins`, `dep_graph_future`, `dep_graph`) because they are only used locally in `fn global_ctxt` and their results don't need to be saved elsewhere. On the other hand, `crate_types` and `stable_crate_id` are querified. They are used from different places and their use is very similar to the existing `crate_name` query in this regard.
2 parents 8e7fd55 + b6ac576 commit 6742e2b

File tree

5 files changed

+117
-130
lines changed

5 files changed

+117
-130
lines changed

compiler/rustc_driver_impl/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_feature::find_gated_cfg;
3232
use rustc_fluent_macro::fluent_messages;
3333
use rustc_interface::util::{self, collect_crate_types, get_codegen_backend};
3434
use rustc_interface::{interface, Queries};
35-
use rustc_lint::LintStore;
35+
use rustc_lint::{unerased_lint_store, LintStore};
3636
use rustc_metadata::locator;
3737
use rustc_session::config::{nightly_options, CG_OPTIONS, Z_OPTIONS};
3838
use rustc_session::config::{ErrorOutputType, Input, OutFileName, OutputType, TrimmedDefPaths};
@@ -411,15 +411,11 @@ fn run_compiler(
411411
return early_exit();
412412
}
413413

414-
{
415-
let plugins = queries.register_plugins()?;
416-
let (.., lint_store) = &*plugins.borrow();
417-
418-
// Lint plugins are registered; now we can process command line flags.
419-
if sess.opts.describe_lints {
420-
describe_lints(sess, lint_store, true);
421-
return early_exit();
422-
}
414+
if sess.opts.describe_lints {
415+
queries
416+
.global_ctxt()?
417+
.enter(|tcx| describe_lints(sess, unerased_lint_store(tcx), true));
418+
return early_exit();
423419
}
424420

425421
// Make sure name resolution and macro expansion is run.

compiler/rustc_interface/src/passes.rs

+10-37
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1212
use rustc_errors::PResult;
1313
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1414
use rustc_fs_util::try_canonicalize;
15-
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
15+
use rustc_hir::def_id::LOCAL_CRATE;
1616
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
1717
use rustc_metadata::creader::CStore;
1818
use rustc_middle::arena::Arena;
@@ -72,43 +72,16 @@ fn count_nodes(krate: &ast::Crate) -> usize {
7272
counter.count
7373
}
7474

75-
pub fn register_plugins<'a>(
76-
sess: &'a Session,
77-
metadata_loader: &'a dyn MetadataLoader,
78-
register_lints: impl Fn(&Session, &mut LintStore),
75+
pub(crate) fn create_lint_store(
76+
sess: &Session,
77+
metadata_loader: &dyn MetadataLoader,
78+
register_lints: Option<impl Fn(&Session, &mut LintStore)>,
7979
pre_configured_attrs: &[ast::Attribute],
80-
crate_name: Symbol,
81-
) -> Result<LintStore> {
82-
// these need to be set "early" so that expansion sees `quote` if enabled.
83-
let features = rustc_expand::config::features(sess, pre_configured_attrs);
84-
sess.init_features(features);
85-
86-
let crate_types = util::collect_crate_types(sess, pre_configured_attrs);
87-
sess.init_crate_types(crate_types);
88-
89-
let stable_crate_id = StableCrateId::new(
90-
crate_name,
91-
sess.crate_types().contains(&CrateType::Executable),
92-
sess.opts.cg.metadata.clone(),
93-
sess.cfg_version,
94-
);
95-
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
96-
rustc_incremental::prepare_session_directory(sess, crate_name, stable_crate_id)?;
97-
98-
if sess.opts.incremental.is_some() {
99-
sess.time("incr_comp_garbage_collect_session_directories", || {
100-
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
101-
warn!(
102-
"Error while trying to garbage collect incremental \
103-
compilation cache directory: {}",
104-
e
105-
);
106-
}
107-
});
108-
}
109-
80+
) -> LintStore {
11081
let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
111-
register_lints(sess, &mut lint_store);
82+
if let Some(register_lints) = register_lints {
83+
register_lints(sess, &mut lint_store);
84+
}
11285

11386
let registrars = sess.time("plugin_loading", || {
11487
plugin::load::load_plugins(sess, metadata_loader, pre_configured_attrs)
@@ -120,7 +93,7 @@ pub fn register_plugins<'a>(
12093
}
12194
});
12295

123-
Ok(lint_store)
96+
lint_store
12497
}
12598

12699
fn pre_expansion_lint<'a>(

compiler/rustc_interface/src/queries.rs

+90-73
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
22
use crate::interface::{Compiler, Result};
3-
use crate::passes;
3+
use crate::{passes, util};
44

55
use rustc_ast as ast;
66
use rustc_codegen_ssa::traits::CodegenBackend;
@@ -9,15 +9,14 @@ use rustc_data_structures::fx::FxIndexMap;
99
use rustc_data_structures::steal::Steal;
1010
use rustc_data_structures::svh::Svh;
1111
use rustc_data_structures::sync::{AppendOnlyIndexVec, Lrc, OnceCell, RwLock, WorkerLocal};
12-
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
12+
use rustc_hir::def_id::{StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
1313
use rustc_hir::definitions::Definitions;
1414
use rustc_incremental::DepGraphFuture;
15-
use rustc_lint::LintStore;
1615
use rustc_metadata::creader::CStore;
1716
use rustc_middle::arena::Arena;
1817
use rustc_middle::dep_graph::DepGraph;
1918
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
20-
use rustc_session::config::{self, OutputFilenames, OutputType};
19+
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
2120
use rustc_session::cstore::Untracked;
2221
use rustc_session::{output::find_crate_name, Session};
2322
use rustc_span::symbol::sym;
@@ -85,12 +84,11 @@ pub struct Queries<'tcx> {
8584
arena: WorkerLocal<Arena<'tcx>>,
8685
hir_arena: WorkerLocal<rustc_hir::Arena<'tcx>>,
8786

88-
dep_graph_future: Query<Option<DepGraphFuture>>,
8987
parse: Query<ast::Crate>,
9088
pre_configure: Query<(ast::Crate, ast::AttrVec)>,
9189
crate_name: Query<Symbol>,
92-
register_plugins: Query<(ast::Crate, ast::AttrVec, Lrc<LintStore>)>,
93-
dep_graph: Query<DepGraph>,
90+
crate_types: Query<Vec<CrateType>>,
91+
stable_crate_id: Query<StableCrateId>,
9492
// This just points to what's in `gcx_cell`.
9593
gcx: Query<&'tcx GlobalCtxt<'tcx>>,
9694
}
@@ -102,12 +100,11 @@ impl<'tcx> Queries<'tcx> {
102100
gcx_cell: OnceCell::new(),
103101
arena: WorkerLocal::new(|_| Arena::default()),
104102
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
105-
dep_graph_future: Default::default(),
106103
parse: Default::default(),
107104
pre_configure: Default::default(),
108105
crate_name: Default::default(),
109-
register_plugins: Default::default(),
110-
dep_graph: Default::default(),
106+
crate_types: Default::default(),
107+
stable_crate_id: Default::default(),
111108
gcx: Default::default(),
112109
}
113110
}
@@ -119,13 +116,6 @@ impl<'tcx> Queries<'tcx> {
119116
self.compiler.codegen_backend()
120117
}
121118

122-
fn dep_graph_future(&self) -> Result<QueryResult<'_, Option<DepGraphFuture>>> {
123-
self.dep_graph_future.compute(|| {
124-
let sess = self.session();
125-
Ok(sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess)))
126-
})
127-
}
128-
129119
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
130120
self.parse
131121
.compute(|| passes::parse(self.session()).map_err(|mut parse_error| parse_error.emit()))
@@ -148,84 +138,111 @@ impl<'tcx> Queries<'tcx> {
148138
})
149139
}
150140

151-
pub fn register_plugins(
152-
&self,
153-
) -> Result<QueryResult<'_, (ast::Crate, ast::AttrVec, Lrc<LintStore>)>> {
154-
self.register_plugins.compute(|| {
155-
let crate_name = *self.crate_name()?.borrow();
156-
let (krate, pre_configured_attrs) = self.pre_configure()?.steal();
157-
158-
let empty: &(dyn Fn(&Session, &mut LintStore) + Sync + Send) = &|_, _| {};
159-
let lint_store = passes::register_plugins(
160-
self.session(),
161-
&*self.codegen_backend().metadata_loader(),
162-
self.compiler.register_lints.as_deref().unwrap_or_else(|| empty),
163-
&pre_configured_attrs,
164-
crate_name,
165-
)?;
166-
167-
// Compute the dependency graph (in the background). We want to do
168-
// this as early as possible, to give the DepGraph maximum time to
169-
// load before dep_graph() is called, but it also can't happen
170-
// until after rustc_incremental::prepare_session_directory() is
171-
// called, which happens within passes::register_plugins().
172-
self.dep_graph_future().ok();
173-
174-
Ok((krate, pre_configured_attrs, Lrc::new(lint_store)))
141+
fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
142+
self.crate_name.compute(|| {
143+
let pre_configure_result = self.pre_configure()?;
144+
let (_, pre_configured_attrs) = &*pre_configure_result.borrow();
145+
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
146+
Ok(find_crate_name(self.session(), pre_configured_attrs))
175147
})
176148
}
177149

178-
fn crate_name(&self) -> Result<QueryResult<'_, Symbol>> {
179-
self.crate_name.compute(|| {
180-
Ok({
181-
let pre_configure_result = self.pre_configure()?;
182-
let (_, pre_configured_attrs) = &*pre_configure_result.borrow();
183-
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
184-
find_crate_name(self.session(), pre_configured_attrs)
185-
})
150+
fn crate_types(&self) -> Result<QueryResult<'_, Vec<CrateType>>> {
151+
self.crate_types.compute(|| {
152+
let pre_configure_result = self.pre_configure()?;
153+
let (_, pre_configured_attrs) = &*pre_configure_result.borrow();
154+
Ok(util::collect_crate_types(&self.session(), &pre_configured_attrs))
186155
})
187156
}
188157

189-
fn dep_graph(&self) -> Result<QueryResult<'_, DepGraph>> {
190-
self.dep_graph.compute(|| {
158+
fn stable_crate_id(&self) -> Result<QueryResult<'_, StableCrateId>> {
159+
self.stable_crate_id.compute(|| {
191160
let sess = self.session();
192-
let future_opt = self.dep_graph_future()?.steal();
193-
let dep_graph = future_opt
194-
.and_then(|future| {
195-
let (prev_graph, mut prev_work_products) =
196-
sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
197-
// Convert from UnordMap to FxIndexMap by sorting
198-
let prev_work_product_ids =
199-
prev_work_products.items().map(|x| *x.0).into_sorted_stable_ord();
200-
let prev_work_products = prev_work_product_ids
201-
.into_iter()
202-
.map(|x| (x, prev_work_products.remove(&x).unwrap()))
203-
.collect::<FxIndexMap<_, _>>();
204-
rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
205-
})
206-
.unwrap_or_else(DepGraph::new_disabled);
207-
Ok(dep_graph)
161+
Ok(StableCrateId::new(
162+
*self.crate_name()?.borrow(),
163+
self.crate_types()?.borrow().contains(&CrateType::Executable),
164+
sess.opts.cg.metadata.clone(),
165+
sess.cfg_version,
166+
))
208167
})
209168
}
210169

170+
fn dep_graph_future(&self) -> Result<Option<DepGraphFuture>> {
171+
let sess = self.session();
172+
let crate_name = *self.crate_name()?.borrow();
173+
let stable_crate_id = *self.stable_crate_id()?.borrow();
174+
175+
// `load_dep_graph` can only be called after `prepare_session_directory`.
176+
rustc_incremental::prepare_session_directory(sess, crate_name, stable_crate_id)?;
177+
let res = sess.opts.build_dep_graph().then(|| rustc_incremental::load_dep_graph(sess));
178+
179+
if sess.opts.incremental.is_some() {
180+
sess.time("incr_comp_garbage_collect_session_directories", || {
181+
if let Err(e) = rustc_incremental::garbage_collect_session_directories(sess) {
182+
warn!(
183+
"Error while trying to garbage collect incremental \
184+
compilation cache directory: {}",
185+
e
186+
);
187+
}
188+
});
189+
}
190+
191+
Ok(res)
192+
}
193+
194+
fn dep_graph(&self, dep_graph_future: Option<DepGraphFuture>) -> DepGraph {
195+
dep_graph_future
196+
.and_then(|future| {
197+
let sess = self.session();
198+
let (prev_graph, mut prev_work_products) =
199+
sess.time("blocked_on_dep_graph_loading", || future.open().open(sess));
200+
// Convert from UnordMap to FxIndexMap by sorting
201+
let prev_work_product_ids =
202+
prev_work_products.items().map(|x| *x.0).into_sorted_stable_ord();
203+
let prev_work_products = prev_work_product_ids
204+
.into_iter()
205+
.map(|x| (x, prev_work_products.remove(&x).unwrap()))
206+
.collect::<FxIndexMap<_, _>>();
207+
rustc_incremental::build_dep_graph(sess, prev_graph, prev_work_products)
208+
})
209+
.unwrap_or_else(DepGraph::new_disabled)
210+
}
211+
211212
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
212213
self.gcx.compute(|| {
213-
let crate_name = *self.crate_name()?.borrow();
214-
let (krate, pre_configured_attrs, lint_store) = self.register_plugins()?.steal();
214+
// Compute the dependency graph (in the background). We want to do this as early as
215+
// possible, to give the DepGraph maximum time to load before `dep_graph` is called.
216+
let dep_graph_future = self.dep_graph_future()?;
215217

216-
let sess = self.session();
218+
let crate_name = self.crate_name()?.steal();
219+
let crate_types = self.crate_types()?.steal();
220+
let stable_crate_id = self.stable_crate_id()?.steal();
221+
let (krate, pre_configured_attrs) = self.pre_configure()?.steal();
217222

218-
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
219-
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
223+
let sess = self.session();
224+
let lint_store = Lrc::new(passes::create_lint_store(
225+
sess,
226+
&*self.codegen_backend().metadata_loader(),
227+
self.compiler.register_lints.as_deref(),
228+
&pre_configured_attrs,
229+
));
230+
let cstore = RwLock::new(Box::new(CStore::new(stable_crate_id)) as _);
231+
let definitions = RwLock::new(Definitions::new(stable_crate_id));
220232
let source_span = AppendOnlyIndexVec::new();
221233
let _id = source_span.push(krate.spans.inner_span);
222234
debug_assert_eq!(_id, CRATE_DEF_ID);
223235
let untracked = Untracked { cstore, source_span, definitions };
224236

237+
// FIXME: Move these fields from session to tcx and make them immutable.
238+
sess.init_crate_types(crate_types);
239+
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
240+
sess.init_features(rustc_expand::config::features(sess, &pre_configured_attrs));
241+
225242
let qcx = passes::create_global_ctxt(
226243
self.compiler,
227244
lint_store,
228-
self.dep_graph()?.steal(),
245+
self.dep_graph(dep_graph_future),
229246
untracked,
230247
&self.gcx_cell,
231248
&self.arena,

compiler/rustc_metadata/src/creader.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_session::cstore::{CrateDepKind, CrateSource, ExternCrate};
2020
use rustc_session::lint;
2121
use rustc_session::output::validate_crate_name;
2222
use rustc_session::search_paths::PathKind;
23-
use rustc_session::Session;
2423
use rustc_span::edition::Edition;
2524
use rustc_span::symbol::{sym, Symbol};
2625
use rustc_span::{Span, DUMMY_SP};
@@ -262,9 +261,9 @@ impl CStore {
262261
}
263262
}
264263

265-
pub fn new(sess: &Session) -> CStore {
264+
pub fn new(local_stable_crate_id: StableCrateId) -> CStore {
266265
let mut stable_crate_ids = StableCrateIdMap::default();
267-
stable_crate_ids.insert(sess.local_stable_crate_id(), LOCAL_CRATE);
266+
stable_crate_ids.insert(local_stable_crate_id, LOCAL_CRATE);
268267
CStore {
269268
// We add an empty entry for LOCAL_CRATE (which maps to zero) in
270269
// order to make array indices in `metas` match with the
@@ -544,6 +543,9 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
544543
self.sess,
545544
&**metadata_loader,
546545
name,
546+
// The all loop is because `--crate-type=rlib --crate-type=rlib` is
547+
// legal and produces both inside this type.
548+
self.sess.crate_types().iter().all(|c| *c == CrateType::Rlib),
547549
hash,
548550
extra_filename,
549551
false, // is_host

0 commit comments

Comments
 (0)