1
Fork 0

Slightly simplify DiagCtxt::make_silent

This commit is contained in:
bjorn3 2025-02-02 15:12:33 +00:00
parent d237378cd1
commit aa2b870bb5
4 changed files with 25 additions and 37 deletions

View file

@ -35,8 +35,8 @@ use crate::snippet::{
use crate::styled_buffer::StyledBuffer; use crate::styled_buffer::StyledBuffer;
use crate::translation::{Translate, to_fluent_args}; use crate::translation::{Translate, to_fluent_args};
use crate::{ use crate::{
CodeSuggestion, DiagCtxt, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle, CodeSuggestion, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle, Level,
Level, MultiSpan, Subdiag, SubstitutionHighlight, SuggestionStyle, TerminalUrl, MultiSpan, Subdiag, SubstitutionHighlight, SuggestionStyle, TerminalUrl,
}; };
/// Default column width, used in tests and when terminal dimensions cannot be determined. /// Default column width, used in tests and when terminal dimensions cannot be determined.
@ -537,11 +537,11 @@ impl Emitter for HumanEmitter {
} }
/// An emitter that does nothing when emitting a non-fatal diagnostic. /// An emitter that does nothing when emitting a non-fatal diagnostic.
/// Fatal diagnostics are forwarded to `fatal_dcx` to avoid silent /// Fatal diagnostics are forwarded to `fatal_emitter` to avoid silent
/// failures of rustc, as witnessed e.g. in issue #89358. /// failures of rustc, as witnessed e.g. in issue #89358.
pub struct SilentEmitter { pub struct SilentEmitter {
pub fallback_bundle: LazyFallbackBundle, pub fallback_bundle: LazyFallbackBundle,
pub fatal_dcx: DiagCtxt, pub fatal_emitter: Box<dyn Emitter + DynSend>,
pub fatal_note: Option<String>, pub fatal_note: Option<String>,
pub emit_fatal_diagnostic: bool, pub emit_fatal_diagnostic: bool,
} }
@ -563,12 +563,12 @@ impl Emitter for SilentEmitter {
None None
} }
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) { fn emit_diagnostic(&mut self, mut diag: DiagInner, registry: &Registry) {
if self.emit_fatal_diagnostic && diag.level == Level::Fatal { if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
if let Some(fatal_note) = &self.fatal_note { if let Some(fatal_note) = &self.fatal_note {
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new()); diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
} }
self.fatal_dcx.handle().emit_diagnostic(diag); self.fatal_emitter.emit_diagnostic(diag, registry);
} }
} }
} }

View file

@ -59,7 +59,7 @@ use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
use rustc_data_structures::AtomicRef; use rustc_data_structures::AtomicRef;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::stable_hasher::{Hash128, StableHasher}; use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
use rustc_data_structures::sync::Lock; use rustc_data_structures::sync::{DynSend, Lock};
pub use rustc_error_messages::{ pub use rustc_error_messages::{
DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel, DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel,
SubdiagMessage, fallback_fluent_bundle, fluent_bundle, SubdiagMessage, fallback_fluent_bundle, fluent_bundle,
@ -682,51 +682,40 @@ impl DiagCtxt {
fatal_note: Option<String>, fatal_note: Option<String>,
emit_fatal_diagnostic: bool, emit_fatal_diagnostic: bool,
) { ) {
self.wrap_emitter(|old_dcx| { // An empty type that implements `Emitter` to temporarily swap in place of the real one,
Box::new(emitter::SilentEmitter { // which will be used in constructing its replacement.
fallback_bundle,
fatal_dcx: DiagCtxt { inner: Lock::new(old_dcx) },
fatal_note,
emit_fatal_diagnostic,
})
});
}
fn wrap_emitter<F>(&self, f: F)
where
F: FnOnce(DiagCtxtInner) -> Box<DynEmitter>,
{
// A empty type that implements `Emitter` so that a `DiagCtxtInner` can be constructed
// to temporarily swap in place of the real one, which will be used in constructing
// its replacement.
struct FalseEmitter; struct FalseEmitter;
impl Emitter for FalseEmitter { impl Emitter for FalseEmitter {
fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) { fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) {
unimplemented!("false emitter must only used during `wrap_emitter`") unimplemented!("false emitter must only used during `make_silent`")
} }
fn source_map(&self) -> Option<&SourceMap> { fn source_map(&self) -> Option<&SourceMap> {
unimplemented!("false emitter must only used during `wrap_emitter`") unimplemented!("false emitter must only used during `make_silent`")
} }
} }
impl translation::Translate for FalseEmitter { impl translation::Translate for FalseEmitter {
fn fluent_bundle(&self) -> Option<&FluentBundle> { fn fluent_bundle(&self) -> Option<&FluentBundle> {
unimplemented!("false emitter must only used during `wrap_emitter`") unimplemented!("false emitter must only used during `make_silent`")
} }
fn fallback_fluent_bundle(&self) -> &FluentBundle { fn fallback_fluent_bundle(&self) -> &FluentBundle {
unimplemented!("false emitter must only used during `wrap_emitter`") unimplemented!("false emitter must only used during `make_silent`")
} }
} }
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
let mut prev_dcx = DiagCtxtInner::new(Box::new(FalseEmitter)); let mut prev_emitter = Box::new(FalseEmitter) as Box<dyn Emitter + DynSend>;
std::mem::swap(&mut *inner, &mut prev_dcx); std::mem::swap(&mut inner.emitter, &mut prev_emitter);
let new_emitter = f(prev_dcx); let new_emitter = Box::new(emitter::SilentEmitter {
let mut new_dcx = DiagCtxtInner::new(new_emitter); fallback_bundle,
std::mem::swap(&mut *inner, &mut new_dcx); fatal_emitter: prev_emitter,
fatal_note,
emit_fatal_diagnostic,
});
inner.emitter = new_emitter;
} }
/// Translate `message` eagerly with `args` to `SubdiagMessage::Eager`. /// Translate `message` eagerly with `args` to `SubdiagMessage::Eager`.

View file

@ -277,14 +277,13 @@ impl ParseSess {
) -> Self { ) -> Self {
let fallback_bundle = fallback_fluent_bundle(locale_resources, false); let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
let emitter = Box::new(HumanEmitter::new( let fatal_emitter = Box::new(HumanEmitter::new(
stderr_destination(ColorConfig::Auto), stderr_destination(ColorConfig::Auto),
Lrc::clone(&fallback_bundle), Lrc::clone(&fallback_bundle),
)); ));
let fatal_dcx = DiagCtxt::new(emitter);
let dcx = DiagCtxt::new(Box::new(SilentEmitter { let dcx = DiagCtxt::new(Box::new(SilentEmitter {
fallback_bundle, fallback_bundle,
fatal_dcx, fatal_emitter,
fatal_note: Some(fatal_note), fatal_note: Some(fatal_note),
emit_fatal_diagnostic, emit_fatal_diagnostic,
})) }))

View file

@ -121,7 +121,7 @@ fn default_dcx(
let emitter: Box<DynEmitter> = if !show_parse_errors { let emitter: Box<DynEmitter> = if !show_parse_errors {
Box::new(SilentEmitter { Box::new(SilentEmitter {
fallback_bundle, fallback_bundle,
fatal_dcx: DiagCtxt::new(emitter), fatal_emitter: emitter,
fatal_note: None, fatal_note: None,
emit_fatal_diagnostic: false, emit_fatal_diagnostic: false,
}) })