Rollup merge of #93936 - bjorn3:simplifications2, r=cjgillot
Couple of driver cleanups * Remove the `RustcDefaultCalls` struct, which hasn't been necessary since the introduction of `rustc_interface`. * Move the `setup_callbacks` call around for a tiny code deduplication. * Remove the `SPAN_DEBUG` global as it isn't actually necessary.
This commit is contained in:
commit
dff7d51fcc
7 changed files with 171 additions and 212 deletions
|
@ -263,7 +263,7 @@ fn run_compiler(
|
||||||
describe_lints(compiler.session(), &lint_store, registered_lints);
|
describe_lints(compiler.session(), &lint_store, registered_lints);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let should_stop = RustcDefaultCalls::print_crate_info(
|
let should_stop = print_crate_info(
|
||||||
&***compiler.codegen_backend(),
|
&***compiler.codegen_backend(),
|
||||||
compiler.session(),
|
compiler.session(),
|
||||||
None,
|
None,
|
||||||
|
@ -292,7 +292,7 @@ fn run_compiler(
|
||||||
|
|
||||||
interface::run_compiler(config, |compiler| {
|
interface::run_compiler(config, |compiler| {
|
||||||
let sess = compiler.session();
|
let sess = compiler.session();
|
||||||
let should_stop = RustcDefaultCalls::print_crate_info(
|
let should_stop = print_crate_info(
|
||||||
&***compiler.codegen_backend(),
|
&***compiler.codegen_backend(),
|
||||||
sess,
|
sess,
|
||||||
Some(compiler.input()),
|
Some(compiler.input()),
|
||||||
|
@ -301,13 +301,9 @@ fn run_compiler(
|
||||||
compiler.temps_dir(),
|
compiler.temps_dir(),
|
||||||
)
|
)
|
||||||
.and_then(|| {
|
.and_then(|| {
|
||||||
RustcDefaultCalls::list_metadata(
|
list_metadata(sess, &*compiler.codegen_backend().metadata_loader(), compiler.input())
|
||||||
sess,
|
|
||||||
&*compiler.codegen_backend().metadata_loader(),
|
|
||||||
compiler.input(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.and_then(|| RustcDefaultCalls::try_process_rlink(sess, compiler));
|
.and_then(|| try_process_rlink(sess, compiler));
|
||||||
|
|
||||||
if should_stop == Compilation::Stop {
|
if should_stop == Compilation::Stop {
|
||||||
return sess.compile_status();
|
return sess.compile_status();
|
||||||
|
@ -512,10 +508,6 @@ impl Compilation {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CompilerCalls instance for a regular rustc build.
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
pub struct RustcDefaultCalls;
|
|
||||||
|
|
||||||
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
|
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {
|
||||||
let upper_cased_code = code.to_ascii_uppercase();
|
let upper_cased_code = code.to_ascii_uppercase();
|
||||||
let normalised = if upper_cased_code.starts_with('E') {
|
let normalised = if upper_cased_code.starts_with('E') {
|
||||||
|
@ -588,7 +580,6 @@ fn show_content_with_pager(content: &str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RustcDefaultCalls {
|
|
||||||
pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
|
pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
|
||||||
if sess.opts.debugging_opts.link_only {
|
if sess.opts.debugging_opts.link_only {
|
||||||
if let Input::File(file) = compiler.input() {
|
if let Input::File(file) = compiler.input() {
|
||||||
|
@ -599,8 +590,7 @@ impl RustcDefaultCalls {
|
||||||
sess.fatal(&format!("failed to read rlink file: {}", err));
|
sess.fatal(&format!("failed to read rlink file: {}", err));
|
||||||
});
|
});
|
||||||
let mut decoder = rustc_serialize::opaque::Decoder::new(&rlink_data, 0);
|
let mut decoder = rustc_serialize::opaque::Decoder::new(&rlink_data, 0);
|
||||||
let codegen_results: CodegenResults =
|
let codegen_results: CodegenResults = rustc_serialize::Decodable::decode(&mut decoder);
|
||||||
rustc_serialize::Decodable::decode(&mut decoder);
|
|
||||||
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
|
let result = compiler.codegen_backend().link(sess, codegen_results, &outputs);
|
||||||
abort_on_err(result, sess);
|
abort_on_err(result, sess);
|
||||||
} else {
|
} else {
|
||||||
|
@ -622,8 +612,7 @@ impl RustcDefaultCalls {
|
||||||
Input::File(ref ifile) => {
|
Input::File(ref ifile) => {
|
||||||
let path = &(*ifile);
|
let path = &(*ifile);
|
||||||
let mut v = Vec::new();
|
let mut v = Vec::new();
|
||||||
locator::list_file_metadata(&sess.target, path, metadata_loader, &mut v)
|
locator::list_file_metadata(&sess.target, path, metadata_loader, &mut v).unwrap();
|
||||||
.unwrap();
|
|
||||||
println!("{}", String::from_utf8(v).unwrap());
|
println!("{}", String::from_utf8(v).unwrap());
|
||||||
}
|
}
|
||||||
Input::Str { .. } => {
|
Input::Str { .. } => {
|
||||||
|
@ -667,8 +656,7 @@ impl RustcDefaultCalls {
|
||||||
for req in &sess.opts.prints {
|
for req in &sess.opts.prints {
|
||||||
match *req {
|
match *req {
|
||||||
TargetList => {
|
TargetList => {
|
||||||
let mut targets =
|
let mut targets = rustc_target::spec::TARGETS.iter().copied().collect::<Vec<_>>();
|
||||||
rustc_target::spec::TARGETS.iter().copied().collect::<Vec<_>>();
|
|
||||||
targets.sort_unstable();
|
targets.sort_unstable();
|
||||||
println!("{}", targets.join("\n"));
|
println!("{}", targets.join("\n"));
|
||||||
}
|
}
|
||||||
|
@ -744,7 +732,6 @@ impl RustcDefaultCalls {
|
||||||
}
|
}
|
||||||
Compilation::Stop
|
Compilation::Stop
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Prints version information
|
/// Prints version information
|
||||||
pub fn version(binary: &str, matches: &getopts::Matches) {
|
pub fn version(binary: &str, matches: &getopts::Matches) {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
//! `rustc_data_structures::AtomicRef` type, which allows us to setup a global
|
//! `rustc_data_structures::AtomicRef` type, which allows us to setup a global
|
||||||
//! static which can then be set in this file at program startup.
|
//! static which can then be set in this file at program startup.
|
||||||
//!
|
//!
|
||||||
//! See `SPAN_DEBUG` for an example of how to set things up.
|
//! See `SPAN_TRACK` for an example of how to set things up.
|
||||||
//!
|
//!
|
||||||
//! The functions in this file should fall back to the default set in their
|
//! The functions in this file should fall back to the default set in their
|
||||||
//! origin crate when the `TyCtxt` is not present in TLS.
|
//! origin crate when the `TyCtxt` is not present in TLS.
|
||||||
|
@ -13,18 +13,6 @@ use rustc_errors::{Diagnostic, TRACK_DIAGNOSTICS};
|
||||||
use rustc_middle::ty::tls;
|
use rustc_middle::ty::tls;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// This is a callback from `rustc_ast` as it cannot access the implicit state
|
|
||||||
/// in `rustc_middle` otherwise.
|
|
||||||
fn span_debug(span: rustc_span::Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
tls::with_opt(|tcx| {
|
|
||||||
if let Some(tcx) = tcx {
|
|
||||||
rustc_span::debug_with_source_map(span, f, tcx.sess.source_map())
|
|
||||||
} else {
|
|
||||||
rustc_span::default_span_debug(span, f)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
|
fn track_span_parent(def_id: rustc_span::def_id::LocalDefId) {
|
||||||
tls::with_opt(|tcx| {
|
tls::with_opt(|tcx| {
|
||||||
if let Some(tcx) = tcx {
|
if let Some(tcx) = tcx {
|
||||||
|
@ -65,7 +53,6 @@ fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) ->
|
||||||
/// Sets up the callbacks in prior crates which we want to refer to the
|
/// Sets up the callbacks in prior crates which we want to refer to the
|
||||||
/// TyCtxt in.
|
/// TyCtxt in.
|
||||||
pub fn setup_callbacks() {
|
pub fn setup_callbacks() {
|
||||||
rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
|
||||||
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
|
rustc_span::SPAN_TRACK.swap(&(track_span_parent as fn(_)));
|
||||||
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
||||||
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
|
TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_)));
|
||||||
|
|
|
@ -186,6 +186,8 @@ pub struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R) -> R {
|
pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R) -> R {
|
||||||
|
crate::callbacks::setup_callbacks();
|
||||||
|
|
||||||
let registry = &config.registry;
|
let registry = &config.registry;
|
||||||
let (mut sess, codegen_backend) = util::create_session(
|
let (mut sess, codegen_backend) = util::create_session(
|
||||||
config.opts,
|
config.opts,
|
||||||
|
@ -238,7 +240,7 @@ pub fn create_compiler_and_run<R>(config: Config, f: impl FnOnce(&Compiler) -> R
|
||||||
pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
|
pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
|
||||||
tracing::trace!("run_compiler");
|
tracing::trace!("run_compiler");
|
||||||
let stderr = config.stderr.take();
|
let stderr = config.stderr.take();
|
||||||
util::setup_callbacks_and_run_in_thread_pool_with_globals(
|
util::run_in_thread_pool_with_globals(
|
||||||
config.opts.edition,
|
config.opts.edition,
|
||||||
config.opts.debugging_opts.threads,
|
config.opts.debugging_opts.threads,
|
||||||
&stderr,
|
&stderr,
|
||||||
|
|
|
@ -15,6 +15,7 @@ mod proc_macro_decls;
|
||||||
mod queries;
|
mod queries;
|
||||||
pub mod util;
|
pub mod util;
|
||||||
|
|
||||||
|
pub use callbacks::setup_callbacks;
|
||||||
pub use interface::{run_compiler, Config};
|
pub use interface::{run_compiler, Config};
|
||||||
pub use passes::{DEFAULT_EXTERN_QUERY_PROVIDERS, DEFAULT_QUERY_PROVIDERS};
|
pub use passes::{DEFAULT_EXTERN_QUERY_PROVIDERS, DEFAULT_QUERY_PROVIDERS};
|
||||||
pub use queries::Queries;
|
pub use queries::Queries;
|
||||||
|
|
|
@ -128,7 +128,7 @@ fn scoped_thread<F: FnOnce() -> R + Send, R: Send>(cfg: thread::Builder, f: F) -
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(parallel_compiler))]
|
#[cfg(not(parallel_compiler))]
|
||||||
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
_threads: usize,
|
_threads: usize,
|
||||||
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
|
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
|
||||||
|
@ -140,8 +140,6 @@ pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Se
|
||||||
cfg = cfg.stack_size(size);
|
cfg = cfg.stack_size(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
crate::callbacks::setup_callbacks();
|
|
||||||
|
|
||||||
let main_handler = move || {
|
let main_handler = move || {
|
||||||
rustc_span::create_session_globals_then(edition, || {
|
rustc_span::create_session_globals_then(edition, || {
|
||||||
io::set_output_capture(stderr.clone());
|
io::set_output_capture(stderr.clone());
|
||||||
|
@ -176,14 +174,12 @@ unsafe fn handle_deadlock() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(parallel_compiler)]
|
#[cfg(parallel_compiler)]
|
||||||
pub fn setup_callbacks_and_run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
pub fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
threads: usize,
|
threads: usize,
|
||||||
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
|
stderr: &Option<Arc<Mutex<Vec<u8>>>>,
|
||||||
f: F,
|
f: F,
|
||||||
) -> R {
|
) -> R {
|
||||||
crate::callbacks::setup_callbacks();
|
|
||||||
|
|
||||||
let mut config = rayon::ThreadPoolBuilder::new()
|
let mut config = rayon::ThreadPoolBuilder::new()
|
||||||
.thread_name(|_| "rustc".to_string())
|
.thread_name(|_| "rustc".to_string())
|
||||||
.acquire_thread_handler(jobserver::acquire_thread)
|
.acquire_thread_handler(jobserver::acquire_thread)
|
||||||
|
|
|
@ -1013,37 +1013,25 @@ pub fn with_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) ->
|
||||||
f()
|
f()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn debug_with_source_map(
|
impl fmt::Debug for Span {
|
||||||
span: Span,
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f: &mut fmt::Formatter<'_>,
|
|
||||||
source_map: &SourceMap,
|
|
||||||
) -> fmt::Result {
|
|
||||||
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(span), span.ctxt())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
with_session_globals(|session_globals| {
|
with_session_globals(|session_globals| {
|
||||||
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
if let Some(source_map) = &*session_globals.source_map.borrow() {
|
||||||
debug_with_source_map(span, f, source_map)
|
write!(f, "{} ({:?})", source_map.span_to_diagnostic_string(*self), self.ctxt())
|
||||||
} else {
|
} else {
|
||||||
f.debug_struct("Span")
|
f.debug_struct("Span")
|
||||||
.field("lo", &span.lo())
|
.field("lo", &self.lo())
|
||||||
.field("hi", &span.hi())
|
.field("hi", &self.hi())
|
||||||
.field("ctxt", &span.ctxt())
|
.field("ctxt", &self.ctxt())
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Span {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
(*SPAN_DEBUG)(*self, f)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for SpanData {
|
impl fmt::Debug for SpanData {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
(*SPAN_DEBUG)(Span::new(self.lo, self.hi, self.ctxt, self.parent), f)
|
fmt::Debug::fmt(&Span::new(self.lo, self.hi, self.ctxt, self.parent), f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2003,8 +1991,6 @@ pub struct FileLines {
|
||||||
pub lines: Vec<LineInfo>,
|
pub lines: Vec<LineInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static SPAN_DEBUG: AtomicRef<fn(Span, &mut fmt::Formatter<'_>) -> fmt::Result> =
|
|
||||||
AtomicRef::new(&(default_span_debug as fn(_, &mut fmt::Formatter<'_>) -> _));
|
|
||||||
pub static SPAN_TRACK: AtomicRef<fn(LocalDefId)> = AtomicRef::new(&((|_| {}) as fn(_)));
|
pub static SPAN_TRACK: AtomicRef<fn(LocalDefId)> = AtomicRef::new(&((|_| {}) as fn(_)));
|
||||||
|
|
||||||
// _____________________________________________________________________________
|
// _____________________________________________________________________________
|
||||||
|
|
|
@ -688,7 +688,7 @@ fn main_args(at_args: &[String]) -> MainResult {
|
||||||
Ok(opts) => opts,
|
Ok(opts) => opts,
|
||||||
Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
|
Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
|
||||||
};
|
};
|
||||||
rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
|
rustc_interface::util::run_in_thread_pool_with_globals(
|
||||||
options.edition,
|
options.edition,
|
||||||
1, // this runs single-threaded, even in a parallel compiler
|
1, // this runs single-threaded, even in a parallel compiler
|
||||||
&None,
|
&None,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue