Skip to content

Commit 6eea2a8

Browse files
committed
errors: implement fallback diagnostic translation
This commit updates the signatures of all diagnostic functions to accept types that can be converted into a `DiagnosticMessage`. This enables existing diagnostic calls to continue to work as before and Fluent identifiers to be provided. The `SessionDiagnostic` derive just generates normal diagnostic calls, so these APIs had to be modified to accept Fluent identifiers. In addition, loading of the "fallback" Fluent bundle, which contains the built-in English messages, has been implemented. Each diagnostic now has "arguments" which correspond to variables in the Fluent messages (necessary to render a Fluent message) but no API for adding arguments has been added yet. Therefore, diagnostics (that do not require interpolation) can be converted to use Fluent identifiers and will be output as before.
1 parent 11d0bae commit 6eea2a8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/parse/session.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ impl Emitter for SilentEmitter {
3333
None
3434
}
3535
fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
36+
fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
37+
None
38+
}
39+
fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
40+
panic!("silent emitter attempted to translate a diagnostic");
41+
}
3642
}
3743

3844
fn silent_emitter() -> Box<dyn Emitter + Send> {
@@ -82,6 +88,14 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
8288
}
8389
self.handle_non_ignoreable_error(db);
8490
}
91+
92+
fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
93+
self.emitter.fluent_bundle()
94+
}
95+
96+
fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
97+
self.emitter.fallback_fluent_bundle()
98+
}
8599
}
86100

87101
fn default_handler(
@@ -100,9 +114,11 @@ fn default_handler(
100114
let emitter = if hide_parse_errors {
101115
silent_emitter()
102116
} else {
117+
let fallback_bundle = rustc_errors::fallback_fluent_bundle();
103118
Box::new(EmitterWriter::stderr(
104119
color_cfg,
105120
Some(source_map.clone()),
121+
fallback_bundle,
106122
false,
107123
false,
108124
None,
@@ -329,6 +345,12 @@ mod tests {
329345
fn emit_diagnostic(&mut self, _db: &Diagnostic) {
330346
self.num_emitted_errors.fetch_add(1, Ordering::Release);
331347
}
348+
fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
349+
None
350+
}
351+
fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
352+
panic!("test emitter attempted to translate a diagnostic");
353+
}
332354
}
333355

334356
fn build_diagnostic(level: DiagnosticLevel, span: Option<MultiSpan>) -> Diagnostic {

0 commit comments

Comments
 (0)