Skip to content

Commit 0f56949

Browse files
authored
Unrolled build for rust-lang#127184
Rollup merge of rust-lang#127184 - bjorn3:interface_refactor2, r=Nadrieril More refactorings to rustc_interface Follow up to rust-lang#126834
2 parents aa1d4f6 + f276459 commit 0f56949

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

compiler/rustc_driver_impl/src/lib.rs

+4-17
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_errors::{
3030
};
3131
use rustc_feature::find_gated_cfg;
3232
use rustc_interface::util::{self, get_codegen_backend};
33-
use rustc_interface::{interface, passes, Queries};
33+
use rustc_interface::{interface, passes, Linker, Queries};
3434
use rustc_lint::unerased_lint_store;
3535
use rustc_metadata::creader::MetadataLoader;
3636
use rustc_metadata::locator;
@@ -41,7 +41,6 @@ use rustc_session::getopts::{self, Matches};
4141
use rustc_session::lint::{Lint, LintId};
4242
use rustc_session::output::collect_crate_types;
4343
use rustc_session::{config, filesearch, EarlyDiagCtxt, Session};
44-
use rustc_span::def_id::LOCAL_CRATE;
4544
use rustc_span::source_map::FileLoader;
4645
use rustc_span::symbol::sym;
4746
use rustc_span::FileName;
@@ -448,21 +447,9 @@ fn run_compiler(
448447
return early_exit();
449448
}
450449

451-
let linker = queries.codegen_and_build_linker()?;
452-
453-
// This must run after monomorphization so that all generic types
454-
// have been instantiated.
455-
if sess.opts.unstable_opts.print_type_sizes {
456-
sess.code_stats.print_type_sizes();
457-
}
458-
459-
if sess.opts.unstable_opts.print_vtable_sizes {
460-
let crate_name = queries.global_ctxt()?.enter(|tcx| tcx.crate_name(LOCAL_CRATE));
461-
462-
sess.code_stats.print_vtable_sizes(crate_name);
463-
}
464-
465-
Ok(Some(linker))
450+
queries.global_ctxt()?.enter(|tcx| {
451+
Ok(Some(Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)?))
452+
})
466453
})?;
467454

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

compiler/rustc_interface/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub mod util;
1616
pub use callbacks::setup_callbacks;
1717
pub use interface::{run_compiler, Config};
1818
pub use passes::DEFAULT_QUERY_PROVIDERS;
19-
pub use queries::Queries;
19+
pub use queries::{Linker, Queries};
2020

2121
#[cfg(test)]
2222
mod tests;

compiler/rustc_interface/src/queries.rs

+34-27
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,6 @@ impl<'a, 'tcx> QueryResult<'a, &'tcx GlobalCtxt<'tcx>> {
6565
}
6666
}
6767

68-
impl<T> Default for Query<T> {
69-
fn default() -> Self {
70-
Query { result: RefCell::new(None) }
71-
}
72-
}
73-
7468
pub struct Queries<'tcx> {
7569
compiler: &'tcx Compiler,
7670
gcx_cell: OnceLock<GlobalCtxt<'tcx>>,
@@ -90,8 +84,8 @@ impl<'tcx> Queries<'tcx> {
9084
gcx_cell: OnceLock::new(),
9185
arena: WorkerLocal::new(|_| Arena::default()),
9286
hir_arena: WorkerLocal::new(|_| rustc_hir::Arena::default()),
93-
parse: Default::default(),
94-
gcx: Default::default(),
87+
parse: Query { result: RefCell::new(None) },
88+
gcx: Query { result: RefCell::new(None) },
9589
}
9690
}
9791

@@ -116,23 +110,6 @@ impl<'tcx> Queries<'tcx> {
116110
)
117111
})
118112
}
119-
120-
pub fn codegen_and_build_linker(&'tcx self) -> Result<Linker> {
121-
self.global_ctxt()?.enter(|tcx| {
122-
let ongoing_codegen = passes::start_codegen(&*self.compiler.codegen_backend, tcx)?;
123-
124-
Ok(Linker {
125-
dep_graph: tcx.dep_graph.clone(),
126-
output_filenames: tcx.output_filenames(()).clone(),
127-
crate_hash: if tcx.needs_crate_hash() {
128-
Some(tcx.crate_hash(LOCAL_CRATE))
129-
} else {
130-
None
131-
},
132-
ongoing_codegen,
133-
})
134-
})
135-
}
136113
}
137114

138115
pub struct Linker {
@@ -144,6 +121,36 @@ pub struct Linker {
144121
}
145122

146123
impl Linker {
124+
pub fn codegen_and_build_linker(
125+
tcx: TyCtxt<'_>,
126+
codegen_backend: &dyn CodegenBackend,
127+
) -> Result<Linker> {
128+
let ongoing_codegen = passes::start_codegen(codegen_backend, tcx)?;
129+
130+
// This must run after monomorphization so that all generic types
131+
// have been instantiated.
132+
if tcx.sess.opts.unstable_opts.print_type_sizes {
133+
tcx.sess.code_stats.print_type_sizes();
134+
}
135+
136+
if tcx.sess.opts.unstable_opts.print_vtable_sizes {
137+
let crate_name = tcx.crate_name(LOCAL_CRATE);
138+
139+
tcx.sess.code_stats.print_vtable_sizes(crate_name);
140+
}
141+
142+
Ok(Linker {
143+
dep_graph: tcx.dep_graph.clone(),
144+
output_filenames: tcx.output_filenames(()).clone(),
145+
crate_hash: if tcx.needs_crate_hash() {
146+
Some(tcx.crate_hash(LOCAL_CRATE))
147+
} else {
148+
None
149+
},
150+
ongoing_codegen,
151+
})
152+
}
153+
147154
pub fn link(self, sess: &Session, codegen_backend: &dyn CodegenBackend) -> Result<()> {
148155
let (codegen_results, work_products) =
149156
codegen_backend.join_codegen(self.ongoing_codegen, sess, &self.output_filenames);
@@ -197,7 +204,7 @@ impl Compiler {
197204
F: for<'tcx> FnOnce(&'tcx Queries<'tcx>) -> T,
198205
{
199206
// Must declare `_timer` first so that it is dropped after `queries`.
200-
let mut _timer = None;
207+
let _timer;
201208
let queries = Queries::new(self);
202209
let ret = f(&queries);
203210

@@ -220,7 +227,7 @@ impl Compiler {
220227

221228
// The timer's lifetime spans the dropping of `queries`, which contains
222229
// the global context.
223-
_timer = Some(self.sess.timer("free_global_ctxt"));
230+
_timer = self.sess.timer("free_global_ctxt");
224231
if let Err((path, error)) = queries.finish() {
225232
self.sess.dcx().emit_fatal(errors::FailedWritingFile { path: &path, error });
226233
}

tests/ui-fulldeps/run-compiler-twice.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate rustc_span;
1717

1818
use std::path::{Path, PathBuf};
1919

20+
use rustc_interface::Linker;
2021
use rustc_interface::interface;
2122
use rustc_session::config::{Input, Options, OutFileName, OutputType, OutputTypes};
2223
use rustc_span::FileName;
@@ -78,8 +79,10 @@ fn compile(code: String, output: PathBuf, sysroot: PathBuf, linker: Option<&Path
7879

7980
interface::run_compiler(config, |compiler| {
8081
let linker = compiler.enter(|queries| {
81-
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;
82-
queries.codegen_and_build_linker()
82+
queries.global_ctxt()?.enter(|tcx| {
83+
tcx.analysis(())?;
84+
Linker::codegen_and_build_linker(tcx, &*compiler.codegen_backend)
85+
})
8386
});
8487
linker.unwrap().link(&compiler.sess, &*compiler.codegen_backend).unwrap();
8588
});

0 commit comments

Comments
 (0)