Skip to content

Commit 4a86853

Browse files
committed
Extract SilentEmitter
1 parent 400a4d4 commit 4a86853

File tree

5 files changed

+33
-57
lines changed

5 files changed

+33
-57
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,6 @@ impl Emitter for HumanEmitter {
536536
pub struct FatalEmitter {
537537
pub fatal_emitter: Box<dyn Emitter + DynSend>,
538538
pub fatal_note: Option<String>,
539-
pub emit_fatal_diagnostic: bool,
540539
}
541540

542541
impl Emitter for FatalEmitter {
@@ -545,7 +544,7 @@ impl Emitter for FatalEmitter {
545544
}
546545

547546
fn emit_diagnostic(&mut self, mut diag: DiagInner, registry: &Registry) {
548-
if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
547+
if diag.level == Level::Fatal {
549548
if let Some(fatal_note) = &self.fatal_note {
550549
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
551550
}
@@ -558,6 +557,22 @@ impl Emitter for FatalEmitter {
558557
}
559558
}
560559

560+
pub struct SilentEmitter {
561+
pub translator: Translator,
562+
}
563+
564+
impl Emitter for SilentEmitter {
565+
fn source_map(&self) -> Option<&SourceMap> {
566+
None
567+
}
568+
569+
fn emit_diagnostic(&mut self, _diag: DiagInner, _registry: &Registry) {}
570+
571+
fn translator(&self) -> &Translator {
572+
&self.translator
573+
}
574+
}
575+
561576
/// Maximum number of suggestions to be shown
562577
///
563578
/// 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
@@ -744,34 +744,10 @@ impl DiagCtxt {
744744
Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
745745
}
746746

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

777753
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: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,22 +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));
288-
let dcx = DiagCtxt::new(Box::new(FatalEmitter {
289-
fatal_emitter,
290-
fatal_note: Some(fatal_note),
291-
emit_fatal_diagnostic,
292-
}))
293-
.disable_warnings();
283+
let dcx =
284+
DiagCtxt::new(Box::new(FatalEmitter { fatal_emitter, fatal_note: Some(fatal_note) }))
285+
.disable_warnings();
294286
ParseSess::with_dcx(dcx, sm)
295287
}
296288

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, FatalEmitter, 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(FatalEmitter {
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)