Skip to content

Commit 3388d83

Browse files
committed
Extract SilentEmitter
1 parent 316f63b commit 3388d83

File tree

5 files changed

+30
-52
lines changed

5 files changed

+30
-52
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,6 @@ impl Emitter for HumanEmitter {
546546
pub struct FatalOnlyEmitter {
547547
pub fatal_emitter: Box<dyn Emitter + DynSend>,
548548
pub fatal_note: Option<String>,
549-
pub emit_fatal_diagnostic: bool,
550549
}
551550

552551
impl Emitter for FatalOnlyEmitter {
@@ -555,7 +554,7 @@ impl Emitter for FatalOnlyEmitter {
555554
}
556555

557556
fn emit_diagnostic(&mut self, mut diag: DiagInner, registry: &Registry) {
558-
if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
557+
if diag.level == Level::Fatal {
559558
if let Some(fatal_note) = &self.fatal_note {
560559
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
561560
}
@@ -568,6 +567,22 @@ impl Emitter for FatalOnlyEmitter {
568567
}
569568
}
570569

570+
pub struct SilentEmitter {
571+
pub translator: Translator,
572+
}
573+
574+
impl Emitter for SilentEmitter {
575+
fn source_map(&self) -> Option<&SourceMap> {
576+
None
577+
}
578+
579+
fn emit_diagnostic(&mut self, _diag: DiagInner, _registry: &Registry) {}
580+
581+
fn translator(&self) -> &Translator {
582+
&self.translator
583+
}
584+
}
585+
571586
/// Maximum number of suggestions to be shown
572587
///
573588
/// Arbitrary, but taken from trait import suggestion limit

compiler/rustc_errors/src/lib.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -748,34 +748,10 @@ impl DiagCtxt {
748748
Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
749749
}
750750

751-
pub fn make_silent(&self, fatal_note: Option<String>, emit_fatal_diagnostic: bool) {
752-
// An empty type that implements `Emitter` to temporarily swap in place of the real one,
753-
// which will be used in constructing its replacement.
754-
struct FalseEmitter;
755-
756-
impl Emitter for FalseEmitter {
757-
fn emit_diagnostic(&mut self, _: DiagInner, _: &Registry) {
758-
unimplemented!("false emitter must only used during `make_silent`")
759-
}
760-
761-
fn source_map(&self) -> Option<&SourceMap> {
762-
unimplemented!("false emitter must only used during `make_silent`")
763-
}
764-
765-
fn translator(&self) -> &translation::Translator {
766-
unimplemented!("false emitter must only used during `make_silent`")
767-
}
768-
}
769-
751+
pub fn make_silent(&self) {
770752
let mut inner = self.inner.borrow_mut();
771-
let mut prev_emitter = Box::new(FalseEmitter) as Box<dyn Emitter + DynSend>;
772-
std::mem::swap(&mut inner.emitter, &mut prev_emitter);
773-
let new_emitter = Box::new(emitter::FatalOnlyEmitter {
774-
fatal_emitter: prev_emitter,
775-
fatal_note,
776-
emit_fatal_diagnostic,
777-
});
778-
inner.emitter = new_emitter;
753+
let translator = inner.emitter.translator().clone();
754+
inner.emitter = Box::new(emitter::SilentEmitter { translator });
779755
}
780756

781757
pub fn set_emitter(&self, emitter: Box<dyn Emitter + DynSend>) {

compiler/rustc_interface/src/interface.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ pub(crate) fn parse_cfg(dcx: DiagCtxtHandle<'_>, cfgs: Vec<String>) -> Cfg {
5555
let psess = ParseSess::with_fatal_emitter(
5656
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
5757
format!("this error occurred on the command line: `--cfg={s}`"),
58-
true,
5958
);
6059
let filename = FileName::cfg_spec_source_code(&s);
6160

@@ -119,7 +118,6 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
119118
let psess = ParseSess::with_fatal_emitter(
120119
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
121120
format!("this error occurred on the command line: `--check-cfg={s}`"),
122-
true,
123121
);
124122
let filename = FileName::cfg_spec_source_code(&s);
125123

compiler/rustc_session/src/parse.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,20 +275,14 @@ impl ParseSess {
275275
}
276276
}
277277

278-
pub fn with_fatal_emitter(
279-
locale_resources: Vec<&'static str>,
280-
fatal_note: String,
281-
282-
emit_fatal_diagnostic: bool,
283-
) -> Self {
278+
pub fn with_fatal_emitter(locale_resources: Vec<&'static str>, fatal_note: String) -> Self {
284279
let translator = Translator::with_fallback_bundle(locale_resources, false);
285280
let sm = Arc::new(SourceMap::new(FilePathMapping::empty()));
286281
let fatal_emitter =
287282
Box::new(HumanEmitter::new(stderr_destination(ColorConfig::Auto), translator));
288283
let dcx = DiagCtxt::new(Box::new(FatalOnlyEmitter {
289284
fatal_emitter,
290285
fatal_note: Some(fatal_note),
291-
emit_fatal_diagnostic,
292286
}))
293287
.disable_warnings();
294288
ParseSess::with_dcx(dcx, sm)

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use std::sync::atomic::{AtomicBool, Ordering};
44

55
use rustc_data_structures::sync::IntoDynSyncSend;
6-
use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter, FatalOnlyEmitter, stderr_destination};
6+
use rustc_errors::emitter::{DynEmitter, Emitter, HumanEmitter, SilentEmitter, stderr_destination};
77
use rustc_errors::registry::Registry;
88
use rustc_errors::translation::Translator;
99
use rustc_errors::{ColorConfig, Diag, DiagCtxt, DiagInner, Level as DiagnosticLevel};
@@ -105,19 +105,14 @@ fn default_dcx(
105105
};
106106

107107
let translator = rustc_driver::default_translator();
108-
let emitter = Box::new(
109-
HumanEmitter::new(stderr_destination(emit_color), translator)
110-
.sm(Some(source_map.clone())),
111-
);
112-
113-
let emitter: Box<DynEmitter> = if !show_parse_errors {
114-
Box::new(FatalOnlyEmitter {
115-
fatal_emitter: emitter,
116-
fatal_note: None,
117-
emit_fatal_diagnostic: false,
118-
})
108+
109+
let emitter: Box<DynEmitter> = if show_parse_errors {
110+
Box::new(
111+
HumanEmitter::new(stderr_destination(emit_color), translator)
112+
.sm(Some(source_map.clone())),
113+
)
119114
} else {
120-
emitter
115+
Box::new(SilentEmitter { translator })
121116
};
122117
DiagCtxt::new(Box::new(SilentOnIgnoredFilesEmitter {
123118
has_non_ignorable_parser_errors: false,
@@ -196,7 +191,7 @@ impl ParseSess {
196191
}
197192

198193
pub(crate) fn set_silent_emitter(&mut self) {
199-
self.raw_psess.dcx().make_silent(None, false);
194+
self.raw_psess.dcx().make_silent();
200195
}
201196

202197
pub(crate) fn span_to_filename(&self, span: Span) -> FileName {

0 commit comments

Comments
 (0)