Skip to content

Commit aa2b870

Browse files
committed
Slightly simplify DiagCtxt::make_silent
1 parent d237378 commit aa2b870

File tree

4 files changed

+25
-37
lines changed

4 files changed

+25
-37
lines changed

compiler/rustc_errors/src/emitter.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use crate::snippet::{
3535
use crate::styled_buffer::StyledBuffer;
3636
use crate::translation::{Translate, to_fluent_args};
3737
use crate::{
38-
CodeSuggestion, DiagCtxt, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle,
39-
Level, MultiSpan, Subdiag, SubstitutionHighlight, SuggestionStyle, TerminalUrl,
38+
CodeSuggestion, DiagInner, DiagMessage, ErrCode, FluentBundle, LazyFallbackBundle, Level,
39+
MultiSpan, Subdiag, SubstitutionHighlight, SuggestionStyle, TerminalUrl,
4040
};
4141

4242
/// Default column width, used in tests and when terminal dimensions cannot be determined.
@@ -537,11 +537,11 @@ impl Emitter for HumanEmitter {
537537
}
538538

539539
/// An emitter that does nothing when emitting a non-fatal diagnostic.
540-
/// Fatal diagnostics are forwarded to `fatal_dcx` to avoid silent
540+
/// Fatal diagnostics are forwarded to `fatal_emitter` to avoid silent
541541
/// failures of rustc, as witnessed e.g. in issue #89358.
542542
pub struct SilentEmitter {
543543
pub fallback_bundle: LazyFallbackBundle,
544-
pub fatal_dcx: DiagCtxt,
544+
pub fatal_emitter: Box<dyn Emitter + DynSend>,
545545
pub fatal_note: Option<String>,
546546
pub emit_fatal_diagnostic: bool,
547547
}
@@ -563,12 +563,12 @@ impl Emitter for SilentEmitter {
563563
None
564564
}
565565

566-
fn emit_diagnostic(&mut self, mut diag: DiagInner, _registry: &Registry) {
566+
fn emit_diagnostic(&mut self, mut diag: DiagInner, registry: &Registry) {
567567
if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
568568
if let Some(fatal_note) = &self.fatal_note {
569569
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
570570
}
571-
self.fatal_dcx.handle().emit_diagnostic(diag);
571+
self.fatal_emitter.emit_diagnostic(diag, registry);
572572
}
573573
}
574574
}

compiler/rustc_errors/src/lib.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use emitter::{DynEmitter, Emitter, is_case_difference, is_different};
5959
use rustc_data_structures::AtomicRef;
6060
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
6161
use rustc_data_structures::stable_hasher::{Hash128, StableHasher};
62-
use rustc_data_structures::sync::Lock;
62+
use rustc_data_structures::sync::{DynSend, Lock};
6363
pub use rustc_error_messages::{
6464
DiagMessage, FluentBundle, LanguageIdentifier, LazyFallbackBundle, MultiSpan, SpanLabel,
6565
SubdiagMessage, fallback_fluent_bundle, fluent_bundle,
@@ -682,51 +682,40 @@ impl DiagCtxt {
682682
fatal_note: Option<String>,
683683
emit_fatal_diagnostic: bool,
684684
) {
685-
self.wrap_emitter(|old_dcx| {
686-
Box::new(emitter::SilentEmitter {
687-
fallback_bundle,
688-
fatal_dcx: DiagCtxt { inner: Lock::new(old_dcx) },
689-
fatal_note,
690-
emit_fatal_diagnostic,
691-
})
692-
});
693-
}
694-
695-
fn wrap_emitter<F>(&self, f: F)
696-
where
697-
F: FnOnce(DiagCtxtInner) -> Box<DynEmitter>,
698-
{
699-
// A empty type that implements `Emitter` so that a `DiagCtxtInner` can be constructed
700-
// to temporarily swap in place of the real one, which will be used in constructing
701-
// its replacement.
685+
// An empty type that implements `Emitter` to temporarily swap in place of the real one,
686+
// which will be used in constructing its replacement.
702687
struct FalseEmitter;
703688

704689
impl Emitter for FalseEmitter {
705690
fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) {
706-
unimplemented!("false emitter must only used during `wrap_emitter`")
691+
unimplemented!("false emitter must only used during `make_silent`")
707692
}
708693

709694
fn source_map(&self) -> Option<&SourceMap> {
710-
unimplemented!("false emitter must only used during `wrap_emitter`")
695+
unimplemented!("false emitter must only used during `make_silent`")
711696
}
712697
}
713698

714699
impl translation::Translate for FalseEmitter {
715700
fn fluent_bundle(&self) -> Option<&FluentBundle> {
716-
unimplemented!("false emitter must only used during `wrap_emitter`")
701+
unimplemented!("false emitter must only used during `make_silent`")
717702
}
718703

719704
fn fallback_fluent_bundle(&self) -> &FluentBundle {
720-
unimplemented!("false emitter must only used during `wrap_emitter`")
705+
unimplemented!("false emitter must only used during `make_silent`")
721706
}
722707
}
723708

724709
let mut inner = self.inner.borrow_mut();
725-
let mut prev_dcx = DiagCtxtInner::new(Box::new(FalseEmitter));
726-
std::mem::swap(&mut *inner, &mut prev_dcx);
727-
let new_emitter = f(prev_dcx);
728-
let mut new_dcx = DiagCtxtInner::new(new_emitter);
729-
std::mem::swap(&mut *inner, &mut new_dcx);
710+
let mut prev_emitter = Box::new(FalseEmitter) as Box<dyn Emitter + DynSend>;
711+
std::mem::swap(&mut inner.emitter, &mut prev_emitter);
712+
let new_emitter = Box::new(emitter::SilentEmitter {
713+
fallback_bundle,
714+
fatal_emitter: prev_emitter,
715+
fatal_note,
716+
emit_fatal_diagnostic,
717+
});
718+
inner.emitter = new_emitter;
730719
}
731720

732721
/// Translate `message` eagerly with `args` to `SubdiagMessage::Eager`.

compiler/rustc_session/src/parse.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,13 @@ impl ParseSess {
277277
) -> Self {
278278
let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
279279
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
280-
let emitter = Box::new(HumanEmitter::new(
280+
let fatal_emitter = Box::new(HumanEmitter::new(
281281
stderr_destination(ColorConfig::Auto),
282282
Lrc::clone(&fallback_bundle),
283283
));
284-
let fatal_dcx = DiagCtxt::new(emitter);
285284
let dcx = DiagCtxt::new(Box::new(SilentEmitter {
286285
fallback_bundle,
287-
fatal_dcx,
286+
fatal_emitter,
288287
fatal_note: Some(fatal_note),
289288
emit_fatal_diagnostic,
290289
}))

src/tools/rustfmt/src/parse/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn default_dcx(
121121
let emitter: Box<DynEmitter> = if !show_parse_errors {
122122
Box::new(SilentEmitter {
123123
fallback_bundle,
124-
fatal_dcx: DiagCtxt::new(emitter),
124+
fatal_emitter: emitter,
125125
fatal_note: None,
126126
emit_fatal_diagnostic: false,
127127
})

0 commit comments

Comments
 (0)