Skip to content

Commit 388da0a

Browse files
authored
Rollup merge of rust-lang#122737 - ytmimi:conditionally_ignore_fatal_diagnostic, r=davidtwco
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 rust-lang#121301 the `SilentEmitter` was shared between rustc and rustfmt. This changed rustfmt's behavior from ignoring all diagnostic to emitting fatal diagnostics, which lead to rust-lang/rustfmt#6109. These changes allow rustfmt to maintain its previous behaviour when using the `SilentEmitter`, while allowing rustc code to still emit fatal diagnostics.
2 parents 6a92312 + d49d136 commit 388da0a

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
@@ -612,12 +612,18 @@ impl DiagCtxt {
612612
Self { inner: Lock::new(DiagCtxtInner::new(emitter)) }
613613
}
614614

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

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)