about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Wood <david.wood@huawei.com>2022-03-26 07:27:43 +0000
committerDavid Wood <david.wood@huawei.com>2022-04-05 07:01:02 +0100
commit6eea2a88c16d44dd6d81cdabcbf2c15ffa424fdc (patch)
tree8e5021b8f6b03f546bf95310feee3f51b4e8aeff
parent11d0bae9f1682c215693e71d2d738b58639ebdff (diff)
downloadrust-6eea2a88c16d44dd6d81cdabcbf2c15ffa424fdc.tar.gz
rust-6eea2a88c16d44dd6d81cdabcbf2c15ffa424fdc.zip
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.
-rw-r--r--src/parse/session.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/parse/session.rs b/src/parse/session.rs
index 7f11cddd882..4563dbf6c16 100644
--- a/src/parse/session.rs
+++ b/src/parse/session.rs
@@ -33,6 +33,12 @@ impl Emitter for SilentEmitter {
         None
     }
     fn emit_diagnostic(&mut self, _db: &Diagnostic) {}
+    fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
+        None
+    }
+    fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
+        panic!("silent emitter attempted to translate a diagnostic");
+    }
 }
 
 fn silent_emitter() -> Box<dyn Emitter + Send> {
@@ -82,6 +88,14 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
         }
         self.handle_non_ignoreable_error(db);
     }
+
+    fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
+        self.emitter.fluent_bundle()
+    }
+
+    fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
+        self.emitter.fallback_fluent_bundle()
+    }
 }
 
 fn default_handler(
@@ -100,9 +114,11 @@ fn default_handler(
     let emitter = if hide_parse_errors {
         silent_emitter()
     } else {
+        let fallback_bundle = rustc_errors::fallback_fluent_bundle();
         Box::new(EmitterWriter::stderr(
             color_cfg,
             Some(source_map.clone()),
+            fallback_bundle,
             false,
             false,
             None,
@@ -329,6 +345,12 @@ mod tests {
             fn emit_diagnostic(&mut self, _db: &Diagnostic) {
                 self.num_emitted_errors.fetch_add(1, Ordering::Release);
             }
+            fn fluent_bundle(&self) -> Option<&Lrc<rustc_errors::FluentBundle>> {
+                None
+            }
+            fn fallback_fluent_bundle(&self) -> &Lrc<rustc_errors::FluentBundle> {
+                panic!("test emitter attempted to translate a diagnostic");
+            }
         }
 
         fn build_diagnostic(level: DiagnosticLevel, span: Option<MultiSpan>) -> Diagnostic {