Skip to content

Commit 124808b

Browse files
committed
errors: share SilentEmitter between rustc and rustfmt
Signed-off-by: David Wood <[email protected]>
1 parent fe9ceab commit 124808b

File tree

3 files changed

+31
-49
lines changed

3 files changed

+31
-49
lines changed

src/parse/session.rs

+25-48
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::borrow::Cow;
21
use std::path::Path;
32
use std::sync::atomic::{AtomicBool, Ordering};
43

54
use rustc_data_structures::sync::{IntoDynSyncSend, Lrc};
6-
use rustc_errors::emitter::{stderr_destination, DynEmitter, Emitter, HumanEmitter};
5+
use rustc_errors::emitter::{stderr_destination, DynEmitter, Emitter, HumanEmitter, SilentEmitter};
76
use rustc_errors::translation::Translate;
87
use rustc_errors::{ColorConfig, Diag, DiagCtxt, DiagInner, Level as DiagnosticLevel};
98
use rustc_session::parse::ParseSess as RawParseSess;
@@ -28,41 +27,6 @@ pub(crate) struct ParseSess {
2827
can_reset_errors: Lrc<AtomicBool>,
2928
}
3029

31-
/// Emitter which discards every error.
32-
struct SilentEmitter;
33-
34-
impl Translate for SilentEmitter {
35-
fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
36-
None
37-
}
38-
39-
fn fallback_fluent_bundle(&self) -> &rustc_errors::FluentBundle {
40-
panic!("silent emitter attempted to translate a diagnostic");
41-
}
42-
43-
// Override `translate_message` for the silent emitter because eager translation of
44-
// subdiagnostics result in a call to this.
45-
fn translate_message<'a>(
46-
&'a self,
47-
message: &'a rustc_errors::DiagMessage,
48-
_: &'a rustc_errors::translation::FluentArgs<'_>,
49-
) -> Result<Cow<'_, str>, rustc_errors::error::TranslateError<'_>> {
50-
rustc_errors::emitter::silent_translate(message)
51-
}
52-
}
53-
54-
impl Emitter for SilentEmitter {
55-
fn source_map(&self) -> Option<&Lrc<SourceMap>> {
56-
None
57-
}
58-
59-
fn emit_diagnostic(&mut self, _diag: DiagInner) {}
60-
}
61-
62-
fn silent_emitter() -> Box<DynEmitter> {
63-
Box::new(SilentEmitter {})
64-
}
65-
6630
/// Emit errors against every files expect ones specified in the `ignore_path_set`.
6731
struct SilentOnIgnoredFilesEmitter {
6832
ignore_path_set: IntoDynSyncSend<Lrc<IgnorePathSet>>,
@@ -143,17 +107,23 @@ fn default_dcx(
143107
ColorConfig::Never
144108
};
145109

146-
let emitter = if hide_parse_errors {
147-
silent_emitter()
110+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
111+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
112+
false,
113+
);
114+
let emitter = Box::new(
115+
HumanEmitter::new(stderr_destination(emit_color), fallback_bundle.clone())
116+
.sm(Some(source_map.clone())),
117+
);
118+
119+
let emitter: Box<DynEmitter> = if hide_parse_errors {
120+
Box::new(SilentEmitter {
121+
fallback_bundle,
122+
fatal_dcx: DiagCtxt::new(emitter),
123+
fatal_note: None,
124+
})
148125
} else {
149-
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
150-
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
151-
false,
152-
);
153-
Box::new(
154-
HumanEmitter::new(stderr_destination(emit_color), fallback_bundle)
155-
.sm(Some(source_map.clone())),
156-
)
126+
emitter
157127
};
158128
DiagCtxt::new(Box::new(SilentOnIgnoredFilesEmitter {
159129
has_non_ignorable_parser_errors: false,
@@ -232,7 +202,14 @@ impl ParseSess {
232202
}
233203

234204
pub(crate) fn set_silent_emitter(&mut self) {
235-
self.raw_psess.dcx = DiagCtxt::new(silent_emitter());
205+
// Ideally this invocation wouldn't be necessary and the fallback bundle in
206+
// `self.parse_sess.dcx` could be used, but the lock in `DiagCtxt` prevents this.
207+
// See `<rustc_errors::SilentEmitter as Translate>::fallback_fluent_bundle`.
208+
let fallback_bundle = rustc_errors::fallback_fluent_bundle(
209+
rustc_driver::DEFAULT_LOCALE_RESOURCES.to_vec(),
210+
false,
211+
);
212+
self.raw_psess.dcx.make_silent(fallback_bundle, None);
236213
}
237214

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

tests/rustfmt/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ fn rustfmt_emits_error_on_line_overflow_true() {
176176
#[test]
177177
#[allow(non_snake_case)]
178178
fn dont_emit_ICE() {
179-
let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs"];
179+
let files = ["tests/target/issue_5728.rs", "tests/target/issue_5729.rs", "tests/target/issue_6082.rs"];
180180

181181
for file in files {
182182
let args = [file];

tests/target/issue_6082.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
macro_rules! test {
2+
($T:ident, $b:lifetime) => {
3+
Box<$T<$b>>
4+
};
5+
}

0 commit comments

Comments
 (0)