diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:11:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:11:28 +0000 |
commit | 94a0819fe3a0d679c3042a77bfe6a2afc505daea (patch) | |
tree | 2b827afe6a05f3538db3f7803a88c4587fe85648 /compiler/rustc_driver | |
parent | Adding upstream version 1.64.0+dfsg1. (diff) | |
download | rustc-94a0819fe3a0d679c3042a77bfe6a2afc505daea.tar.xz rustc-94a0819fe3a0d679c3042a77bfe6a2afc505daea.zip |
Adding upstream version 1.66.0+dfsg1.upstream/1.66.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_driver')
-rw-r--r-- | compiler/rustc_driver/Cargo.toml | 5 | ||||
-rw-r--r-- | compiler/rustc_driver/src/lib.rs | 120 | ||||
-rw-r--r-- | compiler/rustc_driver/src/pretty.rs | 20 | ||||
-rw-r--r-- | compiler/rustc_driver/src/session_diagnostics.rs | 40 |
4 files changed, 127 insertions, 58 deletions
diff --git a/compiler/rustc_driver/Cargo.toml b/compiler/rustc_driver/Cargo.toml index 08d5d4f34..59e937777 100644 --- a/compiler/rustc_driver/Cargo.toml +++ b/compiler/rustc_driver/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" crate-type = ["dylib"] [dependencies] -tracing = { version = "0.1.28" } +tracing = { version = "0.1.35" } serde_json = "1.0.59" rustc_log = { path = "../rustc_log" } rustc_middle = { path = "../rustc_middle" } @@ -19,6 +19,7 @@ rustc_errors = { path = "../rustc_errors" } rustc_feature = { path = "../rustc_feature" } rustc_hir = { path = "../rustc_hir" } rustc_hir_pretty = { path = "../rustc_hir_pretty" } +rustc_macros = { path = "../rustc_macros" } rustc_metadata = { path = "../rustc_metadata" } rustc_parse = { path = "../rustc_parse" } rustc_plugin_impl = { path = "../rustc_plugin_impl" } @@ -29,7 +30,7 @@ rustc_error_codes = { path = "../rustc_error_codes" } rustc_interface = { path = "../rustc_interface" } rustc_ast = { path = "../rustc_ast" } rustc_span = { path = "../rustc_span" } -rustc_typeck = { path = "../rustc_typeck" } +rustc_hir_analysis = { path = "../rustc_hir_analysis" } [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index 53ae913f9..cfa734c7d 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -5,10 +5,11 @@ //! This API is completely unstable and subject to change. #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")] -#![feature(let_else)] #![feature(once_cell)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] +#![deny(rustc::untranslatable_diagnostic)] +#![deny(rustc::diagnostic_outside_of_impl)] #[macro_use] extern crate tracing; @@ -16,7 +17,7 @@ extern crate tracing; pub extern crate rustc_plugin_impl as plugin; use rustc_ast as ast; -use rustc_codegen_ssa::{traits::CodegenBackend, CodegenResults}; +use rustc_codegen_ssa::{traits::CodegenBackend, CodegenErrors, CodegenResults}; use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry}; use rustc_data_structures::sync::SeqCst; use rustc_errors::registry::{InvalidErrorCode, Registry}; @@ -34,7 +35,7 @@ use rustc_session::config::{ErrorOutputType, Input, OutputType, PrintRequest, Tr use rustc_session::cstore::MetadataLoader; use rustc_session::getopts; use rustc_session::lint::{Lint, LintId}; -use rustc_session::{config, DiagnosticOutput, Session}; +use rustc_session::{config, Session}; use rustc_session::{early_error, early_error_no_abort, early_warn}; use rustc_span::source_map::{FileLoader, FileName}; use rustc_span::symbol::sym; @@ -56,6 +57,12 @@ use std::time::Instant; pub mod args; pub mod pretty; +mod session_diagnostics; + +use crate::session_diagnostics::{ + RLinkEmptyVersionNumber, RLinkEncodingVersionMismatch, RLinkRustcVersionMismatch, + RLinkWrongFileType, RlinkNotAFile, RlinkUnableToRead, +}; /// Exit status code used for successful compilation and help output. pub const EXIT_SUCCESS: i32 = 0; @@ -120,10 +127,13 @@ pub struct TimePassesCallbacks { } impl Callbacks for TimePassesCallbacks { + // JUSTIFICATION: the session doesn't exist at this point. + #[allow(rustc::bad_opt_access)] fn config(&mut self, config: &mut interface::Config) { - // If a --prints=... option has been given, we don't print the "total" - // time because it will mess up the --prints output. See #64339. - self.time_passes = config.opts.prints.is_empty() && config.opts.time_passes(); + // If a --print=... option has been given, we don't print the "total" + // time because it will mess up the --print output. See #64339. + // + self.time_passes = config.opts.prints.is_empty() && config.opts.unstable_opts.time_passes; config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath; } } @@ -137,19 +147,21 @@ pub struct RunCompiler<'a, 'b> { at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send), file_loader: Option<Box<dyn FileLoader + Send + Sync>>, - emitter: Option<Box<dyn Write + Send>>, make_codegen_backend: Option<Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>>, } impl<'a, 'b> RunCompiler<'a, 'b> { pub fn new(at_args: &'a [String], callbacks: &'b mut (dyn Callbacks + Send)) -> Self { - Self { at_args, callbacks, file_loader: None, emitter: None, make_codegen_backend: None } + Self { at_args, callbacks, file_loader: None, make_codegen_backend: None } } /// Set a custom codegen backend. /// - /// Used by cg_clif. + /// Has no uses within this repository, but is used by bjorn3 for "the + /// hotswapping branch of cg_clif" for "setting the codegen backend from a + /// custom driver where the custom codegen backend has arbitrary data." + /// (See #102759.) pub fn set_make_codegen_backend( &mut self, make_codegen_backend: Option< @@ -160,17 +172,11 @@ impl<'a, 'b> RunCompiler<'a, 'b> { self } - /// Emit diagnostics to the specified location. - /// - /// Used by RLS. - pub fn set_emitter(&mut self, emitter: Option<Box<dyn Write + Send>>) -> &mut Self { - self.emitter = emitter; - self - } - /// Load files from sources other than the file system. /// - /// Used by RLS. + /// Has no uses within this repository, but may be used in the future by + /// bjorn3 for "hooking rust-analyzer's VFS into rustc at some point for + /// running rustc without having to save". (See #102759.) pub fn set_file_loader( &mut self, file_loader: Option<Box<dyn FileLoader + Send + Sync>>, @@ -181,27 +187,20 @@ impl<'a, 'b> RunCompiler<'a, 'b> { /// Parse args and run the compiler. pub fn run(self) -> interface::Result<()> { - run_compiler( - self.at_args, - self.callbacks, - self.file_loader, - self.emitter, - self.make_codegen_backend, - ) + run_compiler(self.at_args, self.callbacks, self.file_loader, self.make_codegen_backend) } } + fn run_compiler( at_args: &[String], callbacks: &mut (dyn Callbacks + Send), file_loader: Option<Box<dyn FileLoader + Send + Sync>>, - emitter: Option<Box<dyn Write + Send>>, make_codegen_backend: Option< Box<dyn FnOnce(&config::Options) -> Box<dyn CodegenBackend> + Send>, >, ) -> interface::Result<()> { let args = args::arg_expand_all(at_args); - let diagnostic_output = emitter.map_or(DiagnosticOutput::Default, DiagnosticOutput::Raw); let Some(matches) = handle_options(&args) else { return Ok(()) }; let sopts = config::build_session_options(&matches); @@ -223,7 +222,6 @@ fn run_compiler( output_file: ofile, output_dir: odir, file_loader, - diagnostic_output, lint_caps: Default::default(), parse_sess_created: None, register_lints: None, @@ -429,18 +427,6 @@ fn run_compiler( }) } -#[cfg(unix)] -pub fn set_sigpipe_handler() { - unsafe { - // Set the SIGPIPE signal handler, so that an EPIPE - // will cause rustc to terminate, as expected. - assert_ne!(libc::signal(libc::SIGPIPE, libc::SIG_DFL), libc::SIG_ERR); - } -} - -#[cfg(windows)] -pub fn set_sigpipe_handler() {} - // Extract output directory and file from matches. fn make_output(matches: &getopts::Matches) -> (Option<PathBuf>, Option<PathBuf>) { let odir = matches.opt_str("out-dir").map(|o| PathBuf::from(&o)); @@ -581,18 +567,35 @@ pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Comp sess.init_crate_types(collect_crate_types(sess, &[])); let outputs = compiler.build_output_filenames(sess, &[]); let rlink_data = fs::read(file).unwrap_or_else(|err| { - sess.fatal(&format!("failed to read rlink file: {}", err)); + sess.emit_fatal(RlinkUnableToRead { err }); }); let codegen_results = match CodegenResults::deserialize_rlink(rlink_data) { Ok(codegen) => codegen, - Err(error) => { - sess.fatal(&format!("Could not deserialize .rlink file: {error}")); + Err(err) => { + match err { + CodegenErrors::WrongFileType => sess.emit_fatal(RLinkWrongFileType), + CodegenErrors::EmptyVersionNumber => { + sess.emit_fatal(RLinkEmptyVersionNumber) + } + CodegenErrors::EncodingVersionMismatch { version_array, rlink_version } => { + sess.emit_fatal(RLinkEncodingVersionMismatch { + version_array, + rlink_version, + }) + } + CodegenErrors::RustcVersionMismatch { rustc_version, current_version } => { + sess.emit_fatal(RLinkRustcVersionMismatch { + rustc_version, + current_version, + }) + } + }; } }; let result = compiler.codegen_backend().link(sess, codegen_results, &outputs); abort_on_err(result, sess); } else { - sess.fatal("rlink must be a file") + sess.emit_fatal(RlinkNotAFile {}) } Compilation::Stop } else { @@ -717,6 +720,11 @@ fn print_crate_info( println!("{}", cfg); } } + CallingConventions => { + let mut calling_conventions = rustc_target::spec::abi::all_names(); + calling_conventions.sort_unstable(); + println!("{}", calling_conventions.join("\n")); + } RelocationModels | CodeModels | TlsModels @@ -1070,7 +1078,7 @@ pub fn handle_options(args: &[String]) -> Option<getopts::Matches> { Some(matches) } -fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, Vec<ast::Attribute>> { +fn parse_crate_attrs<'a>(sess: &'a Session, input: &Input) -> PResult<'a, ast::AttrVec> { match input { Input::File(ifile) => rustc_parse::parse_crate_attrs_from_file(ifile, &sess.parse_sess), Input::Str { name, input } => rustc_parse::parse_crate_attrs_from_source_str( @@ -1094,22 +1102,25 @@ fn extra_compiler_flags() -> Option<(Vec<String>, bool)> { while let Some(arg) = args.next() { if let Some(a) = ICE_REPORT_COMPILER_FLAGS.iter().find(|a| arg.starts_with(*a)) { let content = if arg.len() == a.len() { + // A space-separated option, like `-C incremental=foo` or `--crate-type rlib` match args.next() { Some(arg) => arg.to_string(), None => continue, } } else if arg.get(a.len()..a.len() + 1) == Some("=") { + // An equals option, like `--crate-type=rlib` arg[a.len() + 1..].to_string() } else { + // A non-space option, like `-Cincremental=foo` arg[a.len()..].to_string() }; - if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.iter().any(|exc| content.starts_with(exc)) { + let option = content.split_once('=').map(|s| s.0).unwrap_or(&content); + if ICE_REPORT_COMPILER_FLAGS_EXCLUDE.iter().any(|exc| option == *exc) { excluded_cargo_defaults = true; } else { result.push(a.to_string()); - match ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.iter().find(|s| content.starts_with(*s)) - { - Some(s) => result.push(s.to_string()), + match ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.iter().find(|s| option == **s) { + Some(s) => result.push(format!("{}=[REDACTED]", s)), None => result.push(content), } } @@ -1148,6 +1159,17 @@ static DEFAULT_HOOK: LazyLock<Box<dyn Fn(&panic::PanicInfo<'_>) + Sync + Send + LazyLock::new(|| { let hook = panic::take_hook(); panic::set_hook(Box::new(|info| { + // If the error was caused by a broken pipe then this is not a bug. + // Write the error and return immediately. See #98700. + #[cfg(windows)] + if let Some(msg) = info.payload().downcast_ref::<String>() { + if msg.starts_with("failed printing to stdout: ") && msg.ends_with("(os error 232)") + { + early_error_no_abort(ErrorOutputType::default(), &msg); + return; + } + }; + // Invoke the default handler, which prints the actual panic message and optionally a backtrace (*DEFAULT_HOOK)(info); diff --git a/compiler/rustc_driver/src/pretty.rs b/compiler/rustc_driver/src/pretty.rs index f66b1a297..f9b1316d2 100644 --- a/compiler/rustc_driver/src/pretty.rs +++ b/compiler/rustc_driver/src/pretty.rs @@ -1,5 +1,6 @@ //! The various pretty-printing routines. +use crate::session_diagnostics::UnprettyDumpFail; use rustc_ast as ast; use rustc_ast_pretty::pprust; use rustc_errors::ErrorGuaranteed; @@ -328,7 +329,7 @@ impl<'tcx> pprust_hir::PpAnn for TypedAnnotation<'tcx> { let typeck_results = self.maybe_typeck_results.get().or_else(|| { self.tcx .hir() - .maybe_body_owned_by(expr.hir_id.owner) + .maybe_body_owned_by(expr.hir_id.owner.def_id) .map(|body_id| self.tcx.typeck_body(body_id)) }); @@ -357,12 +358,15 @@ fn get_source(input: &Input, sess: &Session) -> (String, FileName) { (src, src_name) } -fn write_or_print(out: &str, ofile: Option<&Path>) { +fn write_or_print(out: &str, ofile: Option<&Path>, sess: &Session) { match ofile { None => print!("{}", out), Some(p) => { if let Err(e) = std::fs::write(p, out) { - panic!("print-print failed to write {} due to {}", p.display(), e); + sess.emit_fatal(UnprettyDumpFail { + path: p.display().to_string(), + err: e.to_string(), + }); } } } @@ -392,6 +396,7 @@ pub fn print_after_parsing( annotation.pp_ann(), false, parse.edition, + &sess.parse_sess.attr_id_generator, ) }) } @@ -402,7 +407,7 @@ pub fn print_after_parsing( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, sess); } pub fn print_after_hir_lowering<'tcx>( @@ -434,6 +439,7 @@ pub fn print_after_hir_lowering<'tcx>( annotation.pp_ann(), true, parse.edition, + &sess.parse_sess.attr_id_generator, ) }) } @@ -468,7 +474,7 @@ pub fn print_after_hir_lowering<'tcx>( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, tcx.sess); } // In an ideal world, this would be a public function called by the driver after @@ -496,7 +502,7 @@ fn print_with_analysis( ThirTree => { let mut out = String::new(); - abort_on_err(rustc_typeck::check_crate(tcx), tcx.sess); + abort_on_err(rustc_hir_analysis::check_crate(tcx), tcx.sess); debug!("pretty printing THIR tree"); for did in tcx.hir().body_owners() { let _ = writeln!( @@ -512,7 +518,7 @@ fn print_with_analysis( _ => unreachable!(), }; - write_or_print(&out, ofile); + write_or_print(&out, ofile, tcx.sess); Ok(()) } diff --git a/compiler/rustc_driver/src/session_diagnostics.rs b/compiler/rustc_driver/src/session_diagnostics.rs new file mode 100644 index 000000000..c1bc10891 --- /dev/null +++ b/compiler/rustc_driver/src/session_diagnostics.rs @@ -0,0 +1,40 @@ +use rustc_macros::Diagnostic; + +#[derive(Diagnostic)] +#[diag(driver_rlink_unable_to_read)] +pub(crate) struct RlinkUnableToRead { + pub err: std::io::Error, +} + +#[derive(Diagnostic)] +#[diag(driver_rlink_wrong_file_type)] +pub(crate) struct RLinkWrongFileType; + +#[derive(Diagnostic)] +#[diag(driver_rlink_empty_version_number)] +pub(crate) struct RLinkEmptyVersionNumber; + +#[derive(Diagnostic)] +#[diag(driver_rlink_encoding_version_mismatch)] +pub(crate) struct RLinkEncodingVersionMismatch { + pub version_array: String, + pub rlink_version: u32, +} + +#[derive(Diagnostic)] +#[diag(driver_rlink_rustc_version_mismatch)] +pub(crate) struct RLinkRustcVersionMismatch<'a> { + pub rustc_version: String, + pub current_version: &'a str, +} + +#[derive(Diagnostic)] +#[diag(driver_rlink_no_a_file)] +pub(crate) struct RlinkNotAFile; + +#[derive(Diagnostic)] +#[diag(driver_unpretty_dump_fail)] +pub(crate) struct UnprettyDumpFail { + pub path: String, + pub err: String, +} |