Skip to content

Commit 8780253

Browse files
committed
Remove the parse query
1 parent 6ece803 commit 8780253

File tree

6 files changed

+103
-200
lines changed

6 files changed

+103
-200
lines changed

compiler/rustc_driver_impl/src/lib.rs

+44-55
Original file line numberDiff line numberDiff line change
@@ -387,80 +387,69 @@ fn run_compiler(
387387
return early_exit();
388388
}
389389

390-
let linker = compiler.enter(|queries| {
390+
// Parse the crate root source code (doesn't parse submodules yet)
391+
// Everything else is parsed during macro expansion.
392+
let krate = passes::parse(sess);
393+
394+
// If pretty printing is requested: Figure out the representation, print it and exit
395+
if let Some(pp_mode) = sess.opts.pretty {
396+
if pp_mode.needs_ast_map() {
397+
create_and_enter_global_ctxt(&compiler, krate, |tcx| {
398+
tcx.ensure().early_lint_checks(());
399+
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
400+
passes::write_dep_info(tcx);
401+
});
402+
} else {
403+
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing { krate: &krate });
404+
}
405+
trace!("finished pretty-printing");
406+
return early_exit();
407+
}
408+
409+
if callbacks.after_crate_root_parsing(compiler, &krate) == Compilation::Stop {
410+
return early_exit();
411+
}
412+
413+
if sess.opts.unstable_opts.parse_crate_root_only {
414+
return early_exit();
415+
}
416+
417+
let linker = create_and_enter_global_ctxt(&compiler, krate, |tcx| {
391418
let early_exit = || {
392419
sess.dcx().abort_if_errors();
393420
None
394421
};
395422

396-
// Parse the crate root source code (doesn't parse submodules yet)
397-
// Everything else is parsed during macro expansion.
398-
queries.parse();
423+
// Make sure name resolution and macro expansion is run.
424+
let _ = tcx.resolver_for_lowering();
399425

400-
// If pretty printing is requested: Figure out the representation, print it and exit
401-
if let Some(pp_mode) = sess.opts.pretty {
402-
if pp_mode.needs_ast_map() {
403-
let krate = queries.parse().steal();
426+
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
427+
dump_feature_usage_metrics(tcx, metrics_dir);
428+
}
404429

405-
create_and_enter_global_ctxt(&compiler, krate, |tcx| {
406-
tcx.ensure().early_lint_checks(());
407-
pretty::print(sess, pp_mode, pretty::PrintExtra::NeedsAstMap { tcx });
408-
passes::write_dep_info(tcx);
409-
});
410-
} else {
411-
let krate = queries.parse();
412-
pretty::print(sess, pp_mode, pretty::PrintExtra::AfterParsing {
413-
krate: &*krate.borrow(),
414-
});
415-
}
416-
trace!("finished pretty-printing");
430+
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
417431
return early_exit();
418432
}
419433

420-
if callbacks.after_crate_root_parsing(compiler, &*queries.parse().borrow())
421-
== Compilation::Stop
434+
passes::write_dep_info(tcx);
435+
436+
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
437+
&& sess.opts.output_types.len() == 1
422438
{
423439
return early_exit();
424440
}
425441

426-
if sess.opts.unstable_opts.parse_crate_root_only {
442+
if sess.opts.unstable_opts.no_analysis {
427443
return early_exit();
428444
}
429445

430-
let krate = queries.parse().steal();
446+
tcx.ensure().analysis(());
431447

432-
create_and_enter_global_ctxt(&compiler, krate, |tcx| {
433-
// Make sure name resolution and macro expansion is run.
434-
let _ = tcx.resolver_for_lowering();
435-
436-
if let Some(metrics_dir) = &sess.opts.unstable_opts.metrics_dir {
437-
dump_feature_usage_metrics(tcx, metrics_dir);
438-
}
439-
440-
if callbacks.after_expansion(compiler, tcx) == Compilation::Stop {
441-
return early_exit();
442-
}
443-
444-
passes::write_dep_info(tcx);
445-
446-
if sess.opts.output_types.contains_key(&OutputType::DepInfo)
447-
&& sess.opts.output_types.len() == 1
448-
{
449-
return early_exit();
450-
}
451-
452-
if sess.opts.unstable_opts.no_analysis {
453-
return early_exit();
454-
}
455-
456-
tcx.ensure().analysis(());
457-
458-
if callbacks.after_analysis(compiler, tcx) == Compilation::Stop {
459-
return early_exit();
460-
}
448+
if callbacks.after_analysis(compiler, tcx) == Compilation::Stop {
449+
return early_exit();
450+
}
461451

462-
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
463-
})
452+
Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend))
464453
});
465454

466455
// Linking is done outside the `compiler.enter()` so that the

compiler/rustc_interface/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ pub mod util;
1717

1818
pub use callbacks::setup_callbacks;
1919
pub use interface::{Config, run_compiler};
20-
pub use passes::{DEFAULT_QUERY_PROVIDERS, create_and_enter_global_ctxt};
21-
pub use queries::{Linker, Queries};
20+
pub use passes::{DEFAULT_QUERY_PROVIDERS, create_and_enter_global_ctxt, parse};
21+
pub use queries::Linker;
2222

2323
#[cfg(test)]
2424
mod tests;

compiler/rustc_interface/src/passes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,10 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
709709
*providers
710710
});
711711

712-
pub fn create_and_enter_global_ctxt<'tcx, T>(
713-
compiler: &'tcx Compiler,
712+
pub fn create_and_enter_global_ctxt<T>(
713+
compiler: &Compiler,
714714
krate: rustc_ast::Crate,
715-
f: impl for<'a> FnOnce(TyCtxt<'a>) -> T,
715+
f: impl for<'tcx> FnOnce(TyCtxt<'tcx>) -> T,
716716
) -> T {
717717
let gcx_cell = OnceLock::new();
718718
let arena = WorkerLocal::new(|_| Arena::default());
+1-75
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,18 @@
11
use std::any::Any;
2-
use std::cell::{RefCell, RefMut};
32
use std::sync::Arc;
43

5-
use rustc_ast as ast;
64
use rustc_codegen_ssa::CodegenResults;
75
use rustc_codegen_ssa::traits::CodegenBackend;
8-
use rustc_data_structures::steal::Steal;
96
use rustc_data_structures::svh::Svh;
107
use rustc_hir::def_id::LOCAL_CRATE;
118
use rustc_middle::dep_graph::DepGraph;
12-
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
9+
use rustc_middle::ty::TyCtxt;
1310
use rustc_session::Session;
1411
use rustc_session::config::{self, OutputFilenames, OutputType};
1512

1613
use crate::errors::FailedWritingFile;
17-
use crate::interface::Compiler;
1814
use crate::passes;
1915

20-
/// Represent the result of a query.
21-
///
22-
/// This result can be stolen once with the [`steal`] method and generated with the [`compute`] method.
23-
///
24-
/// [`steal`]: Steal::steal
25-
/// [`compute`]: Self::compute
26-
pub struct Query<T> {
27-
/// `None` means no value has been computed yet.
28-
result: RefCell<Option<Steal<T>>>,
29-
}
30-
31-
impl<T> Query<T> {
32-
fn compute<F: FnOnce() -> T>(&self, f: F) -> QueryResult<'_, T> {
33-
QueryResult(RefMut::map(
34-
self.result.borrow_mut(),
35-
|r: &mut Option<Steal<T>>| -> &mut Steal<T> {
36-
r.get_or_insert_with(|| Steal::new(f()))
37-
},
38-
))
39-
}
40-
}
41-
42-
pub struct QueryResult<'a, T>(RefMut<'a, Steal<T>>);
43-
44-
impl<'a, T> std::ops::Deref for QueryResult<'a, T> {
45-
type Target = RefMut<'a, Steal<T>>;
46-
47-
fn deref(&self) -> &Self::Target {
48-
&self.0
49-
}
50-
}
51-
52-
impl<'a, T> std::ops::DerefMut for QueryResult<'a, T> {
53-
fn deref_mut(&mut self) -> &mut Self::Target {
54-
&mut self.0
55-
}
56-
}
57-
58-
impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
59-
pub fn enter<T>(&mut self, f: impl FnOnce(TyCtxt<'tcx>) -> T) -> T {
60-
(*self.0).borrow().enter(f)
61-
}
62-
}
63-
64-
pub struct Queries<'tcx> {
65-
compiler: &'tcx Compiler,
66-
67-
parse: Query<ast::Crate>,
68-
}
69-
70-
impl<'tcx> Queries<'tcx> {
71-
pub fn new(compiler: &'tcx Compiler) -> Queries<'tcx> {
72-
Queries { compiler, parse: Query { result: RefCell::new(None) } }
73-
}
74-
75-
pub fn parse(&self) -> QueryResult<'_, ast::Crate> {
76-
self.parse.compute(|| passes::parse(&self.compiler.sess))
77-
}
78-
}
79-
8016
pub struct Linker {
8117
dep_graph: DepGraph,
8218
output_filenames: Arc<OutputFilenames>,
@@ -151,13 +87,3 @@ impl Linker {
15187
codegen_backend.link(sess, codegen_results, &self.output_filenames)
15288
}
15389
}
154-
155-
impl Compiler {
156-
pub fn enter<F, T>(&self, f: F) -> T
157-
where
158-
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
159-
{
160-
let queries = Queries::new(self);
161-
f(&queries)
162-
}
163-
}

src/librustdoc/doctest.rs

+20-23
Original file line numberDiff line numberDiff line change
@@ -174,31 +174,28 @@ pub(crate) fn run(dcx: DiagCtxtHandle<'_>, input: Input, options: RustdocOptions
174174
compiling_test_count,
175175
..
176176
} = interface::run_compiler(config, |compiler| {
177-
compiler.enter(|queries| {
178-
let krate = queries.parse().steal();
179-
180-
let collector =
181-
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
182-
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
183-
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
184-
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
185-
let enable_per_target_ignores = options.enable_per_target_ignores;
186-
187-
let mut collector = CreateRunnableDocTests::new(options, opts);
188-
let hir_collector = HirCollector::new(
189-
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
190-
enable_per_target_ignores,
191-
tcx,
192-
);
193-
let tests = hir_collector.collect_crate();
194-
tests.into_iter().for_each(|t| collector.add_test(t));
195-
196-
collector
197-
});
198-
compiler.sess.dcx().abort_if_errors();
177+
let krate = rustc_interface::passes::parse(&compiler.sess);
178+
179+
let collector = rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
180+
let crate_name = tcx.crate_name(LOCAL_CRATE).to_string();
181+
let crate_attrs = tcx.hir().attrs(CRATE_HIR_ID);
182+
let opts = scrape_test_config(crate_name, crate_attrs, args_path);
183+
let enable_per_target_ignores = options.enable_per_target_ignores;
184+
185+
let mut collector = CreateRunnableDocTests::new(options, opts);
186+
let hir_collector = HirCollector::new(
187+
ErrorCodes::from(compiler.sess.opts.unstable_features.is_nightly_build()),
188+
enable_per_target_ignores,
189+
tcx,
190+
);
191+
let tests = hir_collector.collect_crate();
192+
tests.into_iter().for_each(|t| collector.add_test(t));
199193

200194
collector
201-
})
195+
});
196+
compiler.sess.dcx().abort_if_errors();
197+
198+
collector
202199
});
203200

204201
run_tests(opts, &rustdoc_options, &unused_extern_reports, standalone_tests, mergeable_tests);

src/librustdoc/lib.rs

+33-42
Original file line numberDiff line numberDiff line change
@@ -856,50 +856,41 @@ fn main_args(
856856
return;
857857
}
858858

859-
compiler.enter(|queries| {
860-
let krate = queries.parse().steal();
861-
if sess.dcx().has_errors().is_some() {
862-
sess.dcx().fatal("Compilation failed, aborting rustdoc");
859+
let krate = rustc_interface::passes::parse(sess);
860+
if sess.dcx().has_errors().is_some() {
861+
sess.dcx().fatal("Compilation failed, aborting rustdoc");
862+
}
863+
864+
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
865+
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
866+
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
867+
});
868+
info!("finished with rustc");
869+
870+
if let Some(options) = scrape_examples_options {
871+
return scrape_examples::run(krate, render_opts, cache, tcx, options, bin_crate);
863872
}
864873

865-
rustc_interface::create_and_enter_global_ctxt(&compiler, krate, |tcx| {
866-
let (krate, render_opts, mut cache) = sess.time("run_global_ctxt", || {
867-
core::run_global_ctxt(tcx, show_coverage, render_options, output_format)
868-
});
869-
info!("finished with rustc");
870-
871-
if let Some(options) = scrape_examples_options {
872-
return scrape_examples::run(
873-
krate,
874-
render_opts,
875-
cache,
876-
tcx,
877-
options,
878-
bin_crate,
879-
);
880-
}
881-
882-
cache.crate_version = crate_version;
883-
884-
if show_coverage {
885-
// if we ran coverage, bail early, we don't need to also generate docs at this point
886-
// (also we didn't load in any of the useful passes)
887-
return;
888-
} else if run_check {
889-
// Since we're in "check" mode, no need to generate anything beyond this point.
890-
return;
891-
}
892-
893-
info!("going to format");
894-
match output_format {
895-
config::OutputFormat::Html => sess.time("render_html", || {
896-
run_renderer::<html::render::Context<'_>>(krate, render_opts, cache, tcx)
897-
}),
898-
config::OutputFormat::Json => sess.time("render_json", || {
899-
run_renderer::<json::JsonRenderer<'_>>(krate, render_opts, cache, tcx)
900-
}),
901-
}
902-
})
874+
cache.crate_version = crate_version;
875+
876+
if show_coverage {
877+
// if we ran coverage, bail early, we don't need to also generate docs at this point
878+
// (also we didn't load in any of the useful passes)
879+
return;
880+
} else if run_check {
881+
// Since we're in "check" mode, no need to generate anything beyond this point.
882+
return;
883+
}
884+
885+
info!("going to format");
886+
match output_format {
887+
config::OutputFormat::Html => sess.time("render_html", || {
888+
run_renderer::<html::render::Context<'_>>(krate, render_opts, cache, tcx)
889+
}),
890+
config::OutputFormat::Json => sess.time("render_json", || {
891+
run_renderer::<json::JsonRenderer<'_>>(krate, render_opts, cache, tcx)
892+
}),
893+
}
903894
})
904895
})
905896
}

0 commit comments

Comments
 (0)