about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorDavid Wood <david.wood@huawei.com>2022-03-28 09:36:20 +0100
committerDavid Wood <david.wood@huawei.com>2022-04-05 07:01:02 +0100
commitd5119c5b9f1f71090d078e945ea6b5d39d08cffa (patch)
treed63f8049bf2692b7fe2d34bf9e86ec7c26af6fe3 /compiler/rustc_errors/src
parent7f91697b5035f8620df4de47057024c3539b55a6 (diff)
downloadrust-d5119c5b9f1f71090d078e945ea6b5d39d08cffa.tar.gz
rust-d5119c5b9f1f71090d078e945ea6b5d39d08cffa.zip
errors: implement sysroot/testing bundle loading
Extend loading of Fluent bundles so that bundles can be loaded from the
sysroot based on the language requested by the user, or using a nightly
flag.

Sysroot bundles are loaded from `$sysroot/share/locale/$locale/*.ftl`.

Signed-off-by: David Wood <david.wood@huawei.com>
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs13
-rw-r--r--compiler/rustc_errors/src/emitter.rs9
-rw-r--r--compiler/rustc_errors/src/json.rs10
-rw-r--r--compiler/rustc_errors/src/json/tests.rs4
-rw-r--r--compiler/rustc_errors/src/lib.rs7
5 files changed, 37 insertions, 6 deletions
diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
index 4dc99b8dd0f..003fd1eea3a 100644
--- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
+++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs
@@ -21,6 +21,7 @@ use rustc_span::SourceFile;
 /// Generates diagnostics using annotate-snippet
 pub struct AnnotateSnippetEmitterWriter {
     source_map: Option<Lrc<SourceMap>>,
+    fluent_bundle: Option<Lrc<FluentBundle>>,
     fallback_bundle: Lrc<FluentBundle>,
 
     /// If true, hides the longer explanation text
@@ -63,7 +64,7 @@ impl Emitter for AnnotateSnippetEmitterWriter {
     }
 
     fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
-        None
+        self.fluent_bundle.as_ref()
     }
 
     fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> {
@@ -99,11 +100,19 @@ fn annotation_type_for_level(level: Level) -> AnnotationType {
 impl AnnotateSnippetEmitterWriter {
     pub fn new(
         source_map: Option<Lrc<SourceMap>>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         short_message: bool,
         macro_backtrace: bool,
     ) -> Self {
-        Self { source_map, fallback_bundle, short_message, ui_testing: false, macro_backtrace }
+        Self {
+            source_map,
+            fluent_bundle,
+            fallback_bundle,
+            short_message,
+            ui_testing: false,
+            macro_backtrace,
+        }
     }
 
     /// Allows to modify `Self` to enable or disable the `ui_testing` flag.
diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs
index 43bcaa646f3..c8281bd37d1 100644
--- a/compiler/rustc_errors/src/emitter.rs
+++ b/compiler/rustc_errors/src/emitter.rs
@@ -59,6 +59,7 @@ impl HumanReadableErrorType {
         self,
         dst: Box<dyn Write + Send>,
         source_map: Option<Lrc<SourceMap>>,
+        bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         teach: bool,
         terminal_width: Option<usize>,
@@ -69,6 +70,7 @@ impl HumanReadableErrorType {
         EmitterWriter::new(
             dst,
             source_map,
+            bundle,
             fallback_bundle,
             short,
             teach,
@@ -568,7 +570,7 @@ impl Emitter for EmitterWriter {
     }
 
     fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
-        None
+        self.fluent_bundle.as_ref()
     }
 
     fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> {
@@ -686,6 +688,7 @@ impl ColorConfig {
 pub struct EmitterWriter {
     dst: Destination,
     sm: Option<Lrc<SourceMap>>,
+    fluent_bundle: Option<Lrc<FluentBundle>>,
     fallback_bundle: Lrc<FluentBundle>,
     short_message: bool,
     teach: bool,
@@ -706,6 +709,7 @@ impl EmitterWriter {
     pub fn stderr(
         color_config: ColorConfig,
         source_map: Option<Lrc<SourceMap>>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         short_message: bool,
         teach: bool,
@@ -716,6 +720,7 @@ impl EmitterWriter {
         EmitterWriter {
             dst,
             sm: source_map,
+            fluent_bundle,
             fallback_bundle,
             short_message,
             teach,
@@ -728,6 +733,7 @@ impl EmitterWriter {
     pub fn new(
         dst: Box<dyn Write + Send>,
         source_map: Option<Lrc<SourceMap>>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         short_message: bool,
         teach: bool,
@@ -738,6 +744,7 @@ impl EmitterWriter {
         EmitterWriter {
             dst: Raw(dst, colored),
             sm: source_map,
+            fluent_bundle,
             fallback_bundle,
             short_message,
             teach,
diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs
index 3e7bcf2d37e..f78490da245 100644
--- a/compiler/rustc_errors/src/json.rs
+++ b/compiler/rustc_errors/src/json.rs
@@ -37,6 +37,7 @@ pub struct JsonEmitter {
     dst: Box<dyn Write + Send>,
     registry: Option<Registry>,
     sm: Lrc<SourceMap>,
+    fluent_bundle: Option<Lrc<FluentBundle>>,
     fallback_bundle: Lrc<FluentBundle>,
     pretty: bool,
     ui_testing: bool,
@@ -49,6 +50,7 @@ impl JsonEmitter {
     pub fn stderr(
         registry: Option<Registry>,
         source_map: Lrc<SourceMap>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         pretty: bool,
         json_rendered: HumanReadableErrorType,
@@ -59,6 +61,7 @@ impl JsonEmitter {
             dst: Box::new(io::BufWriter::new(io::stderr())),
             registry,
             sm: source_map,
+            fluent_bundle,
             fallback_bundle,
             pretty,
             ui_testing: false,
@@ -71,6 +74,7 @@ impl JsonEmitter {
     pub fn basic(
         pretty: bool,
         json_rendered: HumanReadableErrorType,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         terminal_width: Option<usize>,
         macro_backtrace: bool,
@@ -79,6 +83,7 @@ impl JsonEmitter {
         JsonEmitter::stderr(
             None,
             Lrc::new(SourceMap::new(file_path_mapping)),
+            fluent_bundle,
             fallback_bundle,
             pretty,
             json_rendered,
@@ -91,6 +96,7 @@ impl JsonEmitter {
         dst: Box<dyn Write + Send>,
         registry: Option<Registry>,
         source_map: Lrc<SourceMap>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         pretty: bool,
         json_rendered: HumanReadableErrorType,
@@ -101,6 +107,7 @@ impl JsonEmitter {
             dst,
             registry,
             sm: source_map,
+            fluent_bundle,
             fallback_bundle,
             pretty,
             ui_testing: false,
@@ -182,7 +189,7 @@ impl Emitter for JsonEmitter {
     }
 
     fn fluent_bundle(&self) -> Option<&Lrc<FluentBundle>> {
-        None
+        self.fluent_bundle.as_ref()
     }
 
     fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> {
@@ -395,6 +402,7 @@ impl Diagnostic {
             .new_emitter(
                 Box::new(buf),
                 Some(je.sm.clone()),
+                je.fluent_bundle.clone(),
                 je.fallback_bundle.clone(),
                 false,
                 je.terminal_width,
diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs
index fa0ccd65d06..4174a85204f 100644
--- a/compiler/rustc_errors/src/json/tests.rs
+++ b/compiler/rustc_errors/src/json/tests.rs
@@ -39,13 +39,15 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
     rustc_span::create_default_session_globals_then(|| {
         let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
         sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
-        let fallback_bundle = crate::fallback_fluent_bundle();
+        let fallback_bundle =
+            crate::fallback_fluent_bundle().expect("failed to load fallback fluent bundle");
 
         let output = Arc::new(Mutex::new(Vec::new()));
         let je = JsonEmitter::new(
             Box::new(Shared { data: output.clone() }),
             None,
             sm,
+            None,
             fallback_bundle,
             true,
             HumanReadableErrorType::Short(ColorConfig::Never),
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index 04a0a9f7b73..5db6614c141 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -32,7 +32,8 @@ use rustc_data_structures::stable_hasher::StableHasher;
 use rustc_data_structures::sync::{self, Lock, Lrc};
 use rustc_data_structures::AtomicRef;
 pub use rustc_error_messages::{
-    fallback_fluent_bundle, DiagnosticMessage, FluentBundle, MultiSpan, SpanLabel,
+    fallback_fluent_bundle, fluent_bundle, DiagnosticMessage, FluentBundle, LanguageIdentifier,
+    MultiSpan, SpanLabel,
 };
 pub use rustc_lint_defs::{pluralize, Applicability};
 use rustc_serialize::json::Json;
@@ -544,11 +545,13 @@ impl Handler {
         can_emit_warnings: bool,
         treat_err_as_bug: Option<NonZeroUsize>,
         sm: Option<Lrc<SourceMap>>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
     ) -> Self {
         Self::with_tty_emitter_and_flags(
             color_config,
             sm,
+            fluent_bundle,
             fallback_bundle,
             HandlerFlags { can_emit_warnings, treat_err_as_bug, ..Default::default() },
         )
@@ -557,12 +560,14 @@ impl Handler {
     pub fn with_tty_emitter_and_flags(
         color_config: ColorConfig,
         sm: Option<Lrc<SourceMap>>,
+        fluent_bundle: Option<Lrc<FluentBundle>>,
         fallback_bundle: Lrc<FluentBundle>,
         flags: HandlerFlags,
     ) -> Self {
         let emitter = Box::new(EmitterWriter::stderr(
             color_config,
             sm,
+            fluent_bundle,
             fallback_bundle,
             false,
             false,