From d5119c5b9f1f71090d078e945ea6b5d39d08cffa Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 28 Mar 2022 09:36:20 +0100 Subject: 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 --- .../rustc_errors/src/annotate_snippet_emitter_writer.rs | 13 +++++++++++-- compiler/rustc_errors/src/emitter.rs | 9 ++++++++- compiler/rustc_errors/src/json.rs | 10 +++++++++- compiler/rustc_errors/src/json/tests.rs | 4 +++- compiler/rustc_errors/src/lib.rs | 7 ++++++- 5 files changed, 37 insertions(+), 6 deletions(-) (limited to 'compiler/rustc_errors/src') 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>, + fluent_bundle: Option>, fallback_bundle: Lrc, /// If true, hides the longer explanation text @@ -63,7 +64,7 @@ impl Emitter for AnnotateSnippetEmitterWriter { } fn fluent_bundle(&self) -> Option<&Lrc> { - None + self.fluent_bundle.as_ref() } fn fallback_fluent_bundle(&self) -> &Lrc { @@ -99,11 +100,19 @@ fn annotation_type_for_level(level: Level) -> AnnotationType { impl AnnotateSnippetEmitterWriter { pub fn new( source_map: Option>, + fluent_bundle: Option>, fallback_bundle: Lrc, 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, source_map: Option>, + bundle: Option>, fallback_bundle: Lrc, teach: bool, terminal_width: Option, @@ -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> { - None + self.fluent_bundle.as_ref() } fn fallback_fluent_bundle(&self) -> &Lrc { @@ -686,6 +688,7 @@ impl ColorConfig { pub struct EmitterWriter { dst: Destination, sm: Option>, + fluent_bundle: Option>, fallback_bundle: Lrc, short_message: bool, teach: bool, @@ -706,6 +709,7 @@ impl EmitterWriter { pub fn stderr( color_config: ColorConfig, source_map: Option>, + fluent_bundle: Option>, fallback_bundle: Lrc, 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, source_map: Option>, + fluent_bundle: Option>, fallback_bundle: Lrc, 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, registry: Option, sm: Lrc, + fluent_bundle: Option>, fallback_bundle: Lrc, pretty: bool, ui_testing: bool, @@ -49,6 +50,7 @@ impl JsonEmitter { pub fn stderr( registry: Option, source_map: Lrc, + fluent_bundle: Option>, fallback_bundle: Lrc, 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>, fallback_bundle: Lrc, terminal_width: Option, 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, registry: Option, source_map: Lrc, + fluent_bundle: Option>, fallback_bundle: Lrc, 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> { - None + self.fluent_bundle.as_ref() } fn fallback_fluent_bundle(&self) -> &Lrc { @@ -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, sm: Option>, + fluent_bundle: Option>, fallback_bundle: Lrc, ) -> 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>, + fluent_bundle: Option>, fallback_bundle: Lrc, flags: HandlerFlags, ) -> Self { let emitter = Box::new(EmitterWriter::stderr( color_config, sm, + fluent_bundle, fallback_bundle, false, false, -- cgit 1.4.1-3-g733a5