From c5d8679627eee471301897da0f7d4ec5e90ae018 Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 19 Feb 2024 15:07:08 +0000 Subject: [PATCH] errors: share `SilentEmitter` between rustc and rustfmt Signed-off-by: David Wood --- compiler/rustc_errors/src/emitter.rs | 34 +++-------- compiler/rustc_interface/src/interface.rs | 14 +++-- compiler/rustc_session/src/parse.rs | 15 +++-- src/tools/rustfmt/src/parse/session.rs | 72 +++++++++-------------- 4 files changed, 53 insertions(+), 82 deletions(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index e09c041c1d063..2a21b845cf2fb 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -10,7 +10,6 @@ use rustc_span::source_map::SourceMap; use rustc_span::{FileLines, FileName, SourceFile, Span}; -use crate::error::TranslateError; use crate::snippet::{ Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString, }; @@ -556,20 +555,9 @@ impl Emitter for HumanEmitter { /// Fatal diagnostics are forwarded to `fatal_dcx` to avoid silent /// failures of rustc, as witnessed e.g. in issue #89358. pub struct SilentEmitter { + pub fallback_bundle: LazyFallbackBundle, pub fatal_dcx: DiagCtxt, - pub fatal_note: String, -} - -pub fn silent_translate<'a>( - message: &'a DiagnosticMessage, -) -> Result, TranslateError<'_>> { - match message { - DiagnosticMessage::Str(msg) | DiagnosticMessage::Translated(msg) => Ok(Cow::Borrowed(msg)), - DiagnosticMessage::FluentIdentifier(identifier, _) => { - // Any value works here. - Ok(identifier.clone()) - } - } + pub fatal_note: Option, } impl Translate for SilentEmitter { @@ -578,17 +566,9 @@ impl Translate for SilentEmitter { } fn fallback_fluent_bundle(&self) -> &FluentBundle { - panic!("silent emitter attempted to translate message") - } - - // Override `translate_message` for the silent emitter because eager translation of - // subdiagnostics result in a call to this. - fn translate_message<'a>( - &'a self, - message: &'a DiagnosticMessage, - _: &'a FluentArgs<'_>, - ) -> Result, TranslateError<'_>> { - silent_translate(message) + // Ideally this field wouldn't be necessary and the fallback bundle in `fatal_dcx` would be + // used but the lock prevents this. + &self.fallback_bundle } } @@ -599,7 +579,9 @@ impl Emitter for SilentEmitter { fn emit_diagnostic(&mut self, mut diag: Diagnostic) { if diag.level == Level::Fatal { - diag.note(self.fatal_note.clone()); + if let Some(fatal_note) = &self.fatal_note { + diag.note(fatal_note.clone()); + } self.fatal_dcx.emit_diagnostic(diag); } } diff --git a/compiler/rustc_interface/src/interface.rs b/compiler/rustc_interface/src/interface.rs index 8a4705e0056e1..be9c325b0c0d8 100644 --- a/compiler/rustc_interface/src/interface.rs +++ b/compiler/rustc_interface/src/interface.rs @@ -45,9 +45,10 @@ pub struct Compiler { pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec) -> Cfg { cfgs.into_iter() .map(|s| { - let sess = ParseSess::with_silent_emitter(format!( - "this error occurred on the command line: `--cfg={s}`" - )); + let sess = ParseSess::with_silent_emitter( + vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE], + format!("this error occurred on the command line: `--cfg={s}`"), + ); let filename = FileName::cfg_spec_source_code(&s); macro_rules! error { @@ -107,9 +108,10 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec) -> CheckCfg { let mut check_cfg = CheckCfg { exhaustive_names, exhaustive_values, ..CheckCfg::default() }; for s in specs { - let sess = ParseSess::with_silent_emitter(format!( - "this error occurred on the command line: `--check-cfg={s}`" - )); + let sess = ParseSess::with_silent_emitter( + vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE], + format!("this error occurred on the command line: `--check-cfg={s}`"), + ); let filename = FileName::cfg_spec_source_code(&s); macro_rules! error { diff --git a/compiler/rustc_session/src/parse.rs b/compiler/rustc_session/src/parse.rs index 701c5bf375f6e..1ef6cc82b838d 100644 --- a/compiler/rustc_session/src/parse.rs +++ b/compiler/rustc_session/src/parse.rs @@ -258,12 +258,17 @@ impl ParseSess { } } - pub fn with_silent_emitter(fatal_note: String) -> Self { - let fallback_bundle = fallback_fluent_bundle(Vec::new(), false); + pub fn with_silent_emitter(locale_resources: Vec<&'static str>, fatal_note: String) -> Self { + let fallback_bundle = fallback_fluent_bundle(locale_resources, false); let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); - let fatal_dcx = DiagCtxt::with_tty_emitter(None, fallback_bundle).disable_warnings(); - let dcx = DiagCtxt::with_emitter(Box::new(SilentEmitter { fatal_dcx, fatal_note })) - .disable_warnings(); + let fatal_dcx = + DiagCtxt::with_tty_emitter(None, fallback_bundle.clone()).disable_warnings(); + let dcx = DiagCtxt::with_emitter(Box::new(SilentEmitter { + fallback_bundle, + fatal_dcx, + fatal_note: Some(fatal_note), + })) + .disable_warnings(); ParseSess::with_dcx(dcx, sm) } diff --git a/src/tools/rustfmt/src/parse/session.rs b/src/tools/rustfmt/src/parse/session.rs index cff025cf2ab3a..25488170792bf 100644 --- a/src/tools/rustfmt/src/parse/session.rs +++ b/src/tools/rustfmt/src/parse/session.rs @@ -1,9 +1,8 @@ -use std::borrow::Cow; use std::path::Path; use std::sync::atomic::{AtomicBool, Ordering}; use rustc_data_structures::sync::{IntoDynSyncSend, Lrc}; -use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter}; +use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter, SilentEmitter}; use rustc_errors::translation::Translate; use rustc_errors::{ ColorConfig, DiagCtxt, Diagnostic, DiagnosticBuilder, Level as DiagnosticLevel, @@ -30,41 +29,6 @@ pub(crate) struct ParseSess { can_reset_errors: Lrc, } -/// Emitter which discards every error. -struct SilentEmitter; - -impl Translate for SilentEmitter { - fn fluent_bundle(&self) -> Option<&Lrc> { - None - } - - fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle { - panic!("silent emitter attempted to translate a diagnostic"); - } - - // Override `translate_message` for the silent emitter because eager translation of - // subdiagnostics result in a call to this. - fn translate_message<'a>( - &'a self, - message: &'a rustc_errors::DiagnosticMessage, - _: &'a rustc_errors::translation::FluentArgs<'_>, - ) -> Result, rustc_errors::error::TranslateError<'_>> { - rustc_errors::emitter::silent_translate(message) - } -} - -impl Emitter for SilentEmitter { - fn source_map(&self) -> Option<&Lrc> { - None - } - - fn emit_diagnostic(&mut self, _db: Diagnostic) {} -} - -fn silent_emitter() -> Box { - Box::new(SilentEmitter {}) -} - /// Emit errors against every files expect ones specified in the `ignore_path_set`. struct SilentOnIgnoredFilesEmitter { ignore_path_set: IntoDynSyncSend>, @@ -145,15 +109,19 @@ fn default_dcx( ColorConfig::Never }; - let emitter = if hide_parse_errors { - silent_emitter() + let fallback_bundle = rustc_errors::fallback_fluent_bundle( + rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), + false, + ); + let emitter = + Box::new(HumanEmitter::stderr(emit_color, fallback_bundle.clone()).sm(Some(source_map.clone()))); + + let emitter: Box = if hide_parse_errors { + Box::new(SilentEmitter { fallback_bundle, fatal_dcx: DiagCtxt::with_emitter(emitter), fatal_note: None }) } else { - let fallback_bundle = rustc_errors::fallback_fluent_bundle( - rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), - false, - ); - Box::new(HumanEmitter::stderr(emit_color, fallback_bundle).sm(Some(source_map.clone()))) + emitter }; + DiagCtxt::with_emitter(Box::new(SilentOnIgnoredFilesEmitter { has_non_ignorable_parser_errors: false, source_map, @@ -231,7 +199,21 @@ impl ParseSess { } pub(crate) fn set_silent_emitter(&mut self) { - self.parse_sess.dcx = DiagCtxt::with_emitter(silent_emitter()); + // Ideally this invocation wouldn't be necessary and the fallback bundle in + // `self.parse_sess.dcx` could be used. + let fallback_bundle = rustc_errors::fallback_fluent_bundle( + rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(), + false, + ); + // SAFETY: `fatal_dcx` will not be dropped while it is referenced by `self.parse_sess.dcx`, + // and `self.parse_sess.dcx` is written to by `ptr::write` so won't drop `fatal_dcx`. + unsafe { + let fatal_dcx = std::ptr::read(&self.parse_sess.dcx); + std::ptr::write( + &mut self.parse_sess.dcx, + DiagCtxt::with_emitter(Box::new(SilentEmitter { fallback_bundle, fatal_dcx, fatal_note: None })) + ); + } } pub(crate) fn span_to_filename(&self, span: Span) -> FileName {