Skip to content

Commit 43f1e58

Browse files
committed
conditionally ignore fatal diagnostic in the SilentEmitter
This change is primarily meant to allow rustfmt to ignore all diagnostics when using the `SilentEmitter`. Back in PR 121301 the `SilentEmitter` was shared between rustc and rustfmt. This changed rustfmt's behavior from ignoring all diagnostic to emitting fatal diagnostics. These changes allow rustfmt to maintain it's previous behaviour when using the SilentEmitter, while allowing rustc code to still emit fatal diagnostics.
1 parent 200e3f7 commit 43f1e58

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

compiler/rustc_errors/src/emitter.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ pub struct SilentEmitter {
541541
pub fallback_bundle: LazyFallbackBundle,
542542
pub fatal_dcx: DiagCtxt,
543543
pub fatal_note: Option<String>,
544+
pub emit_fatal_diagnostic: bool,
544545
}
545546

546547
impl Translate for SilentEmitter {
@@ -561,7 +562,7 @@ impl Emitter for SilentEmitter {
561562
}
562563

563564
fn emit_diagnostic(&mut self, mut diag: DiagInner) {
564-
if diag.level == Level::Fatal {
565+
if self.emit_fatal_diagnostic && diag.level == Level::Fatal {
565566
if let Some(fatal_note) = &self.fatal_note {
566567
diag.sub(Level::Note, fatal_note.clone(), MultiSpan::new());
567568
}

compiler/rustc_errors/src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -613,12 +613,18 @@ impl DiagCtxt {
613613
Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
614614
}
615615

616-
pub fn make_silent(&mut self, fallback_bundle: LazyFallbackBundle, fatal_note: Option<String>) {
616+
pub fn make_silent(
617+
&mut self,
618+
fallback_bundle: LazyFallbackBundle,
619+
fatal_note: Option<String>,
620+
emit_fatal_diagnostic: bool,
621+
) {
617622
self.wrap_emitter(|old_dcx| {
618623
Box::new(emitter::SilentEmitter {
619624
fallback_bundle,
620625
fatal_dcx: DiagCtxt { inner: Lock::new(old_dcx) },
621626
fatal_note,
627+
emit_fatal_diagnostic,
622628
})
623629
});
624630
}

compiler/rustc_interface/src/interface.rs

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg {
4848
let psess = ParseSess::with_silent_emitter(
4949
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
5050
format!("this error occurred on the command line: `--cfg={s}`"),
51+
true,
5152
);
5253
let filename = FileName::cfg_spec_source_code(&s);
5354

@@ -111,6 +112,7 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
111112
let psess = ParseSess::with_silent_emitter(
112113
vec![crate::DEFAULT_LOCALE_RESOURCE, rustc_parse::DEFAULT_LOCALE_RESOURCE],
113114
format!("this error occurred on the command line: `--check-cfg={s}`"),
115+
true,
114116
);
115117
let filename = FileName::cfg_spec_source_code(&s);
116118

compiler/rustc_session/src/parse.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,11 @@ impl ParseSess {
269269
}
270270
}
271271

272-
pub fn with_silent_emitter(locale_resources: Vec<&'static str>, fatal_note: String) -> Self {
272+
pub fn with_silent_emitter(
273+
locale_resources: Vec<&'static str>,
274+
fatal_note: String,
275+
emit_fatal_diagnostic: bool,
276+
) -> Self {
273277
let fallback_bundle = fallback_fluent_bundle(locale_resources, false);
274278
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
275279
let emitter = Box::new(HumanEmitter::new(
@@ -281,6 +285,7 @@ impl ParseSess {
281285
fallback_bundle,
282286
fatal_dcx,
283287
fatal_note: Some(fatal_note),
288+
emit_fatal_diagnostic,
284289
}))
285290
.disable_warnings();
286291
ParseSess::with_dcx(dcx, sm)

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ fn default_dcx(
121121
fallback_bundle,
122122
fatal_dcx: DiagCtxt::new(emitter),
123123
fatal_note: None,
124+
emit_fatal_diagnostic: false
124125
})
125126
} else {
126127
emitter
@@ -209,7 +210,7 @@ impl ParseSess {
209210
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
210211
false,
211212
);
212-
self.raw_psess.dcx.make_silent(fallback_bundle, None);
213+
self.raw_psess.dcx.make_silent(fallback_bundle, None, false);
213214
}
214215

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

0 commit comments

Comments
 (0)