From 9a833de62a1de598ea54ad0b8ac01369f8d23c85 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:01:17 +0000 Subject: [PATCH 1/3] Construct DiagCtxt a bit earlier in build_session --- compiler/rustc_interface/src/interface.rs | 1 - compiler/rustc_interface/src/tests.rs | 1 - compiler/rustc_session/src/session.rs | 18 ++++++------------ 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 97fc542910c0b..b35703d8e737b 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -442,7 +442,6 @@ pub fn run_compiler(config: Config, f: impl FnOnce(&Compiler) -> R + Se locale_resources.push(codegen_backend.locale_resource()); let mut sess = rustc_session::build_session( - early_dcx, config.opts, CompilerIO { input: config.input, diff --git a/compiler/rustc_interface/src/tests.rs b/compiler/rustc_interface/src/tests.rs index fb69dd548119c..46d6f37a91cb5 100644 --- a/compiler/rustc_interface/src/tests.rs +++ b/compiler/rustc_interface/src/tests.rs @@ -65,7 +65,6 @@ where static USING_INTERNAL_FEATURES: AtomicBool = AtomicBool::new(false); let sess = build_session( - early_dcx, sessopts, io, None, diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 1a3e80d665916..0851e859a0fed 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -965,7 +965,6 @@ fn default_emitter( #[allow(rustc::bad_opt_access)] #[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable pub fn build_session( - early_dcx: EarlyDiagCtxt, sopts: config::Options, io: CompilerIO, bundle: Option>, @@ -990,14 +989,6 @@ pub fn build_session( let cap_lints_allow = sopts.lint_cap.is_some_and(|cap| cap == lint::Allow); let can_emit_warnings = !(warnings_allow || cap_lints_allow); - let host_triple = TargetTuple::from_tuple(config::host_tuple()); - let (host, target_warnings) = Target::search(&host_triple, &sysroot).unwrap_or_else(|e| { - early_dcx.early_fatal(format!("Error loading host specification: {e}")) - }); - for warning in target_warnings.warning_messages() { - early_dcx.early_warn(warning) - } - let fallback_bundle = fallback_fluent_bundle( fluent_resources, sopts.unstable_opts.translate_directionality_markers, @@ -1012,9 +1003,12 @@ pub fn build_session( dcx = dcx.with_ice_file(ice_file); } - // Now that the proper handler has been constructed, drop early_dcx to - // prevent accidental use. - drop(early_dcx); + let host_triple = TargetTuple::from_tuple(config::host_tuple()); + let (host, target_warnings) = Target::search(&host_triple, &sysroot) + .unwrap_or_else(|e| dcx.handle().fatal(format!("Error loading host specification: {e}"))); + for warning in target_warnings.warning_messages() { + dcx.handle().warn(warning) + } let self_profiler = if let SwitchWithOptPath::Enabled(ref d) = sopts.unstable_opts.self_profile { From 8a0adec05bf99d229e33545ba16c65c210e40142 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:09:15 +0000 Subject: [PATCH 2/3] Avoid manually producing FatalError in a couple of places --- compiler/rustc_driver_impl/src/lib.rs | 2 +- compiler/rustc_errors/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_driver_impl/src/lib.rs b/compiler/rustc_driver_impl/src/lib.rs index 6ea14d15c1440..370aba413ed69 100644 --- a/compiler/rustc_driver_impl/src/lib.rs +++ b/compiler/rustc_driver_impl/src/lib.rs @@ -1024,7 +1024,7 @@ pub fn describe_flag_categories(early_dcx: &EarlyDiagCtxt, matches: &Matches) -> let wall = matches.opt_strs("W"); if wall.iter().any(|x| *x == "all") { print_wall_help(); - rustc_errors::FatalError.raise(); + return true; } // Don't handle -W help here, because we might first load additional lints. diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index 7a02e0dd2f093..9af17db9a6e6c 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -1048,8 +1048,8 @@ impl<'a> DiagCtxtHandle<'a> { /// bad results, such as spurious/uninteresting additional errors -- when /// returning an error `Result` is difficult. pub fn abort_if_errors(&self) { - if self.has_errors().is_some() { - FatalError.raise(); + if let Some(guar) = self.has_errors() { + guar.raise_fatal(); } } From b9b2c3affcc51e85d793f51875db067513642863 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 5 Feb 2025 15:16:41 +0000 Subject: [PATCH 3/3] Stop passing the same resource multiple times when building ParseSess --- compiler/rustc_parse/src/parser/tests.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_parse/src/parser/tests.rs b/compiler/rustc_parse/src/parser/tests.rs index 76b7dc0e48a25..8b8c81a77a01a 100644 --- a/compiler/rustc_parse/src/parser/tests.rs +++ b/compiler/rustc_parse/src/parser/tests.rs @@ -26,7 +26,7 @@ use crate::parser::{ForceCollect, Parser}; use crate::{new_parser_from_source_str, source_str_to_stream, unwrap_or_emit_fatal}; fn psess() -> ParseSess { - ParseSess::new(vec![crate::DEFAULT_LOCALE_RESOURCE, crate::DEFAULT_LOCALE_RESOURCE]) + ParseSess::new(vec![crate::DEFAULT_LOCALE_RESOURCE]) } /// Map string to parser (via tts). @@ -41,10 +41,8 @@ fn string_to_parser(psess: &ParseSess, source_str: String) -> Parser<'_> { fn create_test_handler(theme: OutputTheme) -> (DiagCtxt, Arc, Arc>>) { let output = Arc::new(Mutex::new(Vec::new())); let source_map = Arc::new(SourceMap::new(FilePathMapping::empty())); - let fallback_bundle = rustc_errors::fallback_fluent_bundle( - vec![crate::DEFAULT_LOCALE_RESOURCE, crate::DEFAULT_LOCALE_RESOURCE], - false, - ); + let fallback_bundle = + rustc_errors::fallback_fluent_bundle(vec![crate::DEFAULT_LOCALE_RESOURCE], false); let mut emitter = HumanEmitter::new(Box::new(Shared { data: output.clone() }), fallback_bundle) .sm(Some(source_map.clone())) .diagnostic_width(Some(140));