diff options
| author | David Wood <david.wood@huawei.com> | 2022-04-12 09:34:40 +0100 |
|---|---|---|
| committer | David Wood <david.wood@huawei.com> | 2022-04-13 02:44:59 +0100 |
| commit | 9bfe0e39e4214c9a76e6c38350286176fa73787a (patch) | |
| tree | d24caebeae01124af5e6ef78735dab042b3bc3ce /compiler/rustc_errors | |
| parent | f6cef572d6d75a3b7dea5f496d3bd981986e94ca (diff) | |
| download | rust-9bfe0e39e4214c9a76e6c38350286176fa73787a.tar.gz rust-9bfe0e39e4214c9a76e6c38350286176fa73787a.zip | |
errors: lazily load fallback fluent bundle
Loading the fallback bundle in compilation sessions that won't go on to emit any errors unnecessarily degrades compile time performance, so lazily create the Fluent bundle when it is first required. Signed-off-by: David Wood <david.wood@huawei.com>
Diffstat (limited to 'compiler/rustc_errors')
| -rw-r--r-- | compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs | 12 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/emitter.rs | 19 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json.rs | 16 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/json/tests.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_errors/src/lib.rs | 6 |
5 files changed, 29 insertions, 26 deletions
diff --git a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs index 003fd1eea3a..1f270fcf56b 100644 --- a/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs +++ b/compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs @@ -8,8 +8,8 @@ use crate::emitter::FileWithAnnotatedLines; use crate::snippet::Line; use crate::{ - CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Emitter, FluentBundle, Level, - MultiSpan, Style, SubDiagnostic, + CodeSuggestion, Diagnostic, DiagnosticId, DiagnosticMessage, Emitter, FluentBundle, + LazyFallbackBundle, Level, MultiSpan, Style, SubDiagnostic, }; use annotate_snippets::display_list::{DisplayList, FormatOptions}; use annotate_snippets::snippet::*; @@ -22,7 +22,7 @@ use rustc_span::SourceFile; pub struct AnnotateSnippetEmitterWriter { source_map: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, /// If true, hides the longer explanation text short_message: bool, @@ -67,8 +67,8 @@ impl Emitter for AnnotateSnippetEmitterWriter { self.fluent_bundle.as_ref() } - fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> { - &self.fallback_bundle + fn fallback_fluent_bundle(&self) -> &FluentBundle { + &**self.fallback_bundle } fn should_show_explain(&self) -> bool { @@ -101,7 +101,7 @@ impl AnnotateSnippetEmitterWriter { pub fn new( source_map: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, short_message: bool, macro_backtrace: bool, ) -> Self { diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 6a763d4d140..47cdf39cd52 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -16,7 +16,8 @@ use crate::snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, Styl use crate::styled_buffer::StyledBuffer; use crate::{ CodeSuggestion, Diagnostic, DiagnosticArg, DiagnosticId, DiagnosticMessage, FluentBundle, - Handler, Level, MultiSpan, SubDiagnostic, SubstitutionHighlight, SuggestionStyle, + Handler, LazyFallbackBundle, Level, MultiSpan, SubDiagnostic, SubstitutionHighlight, + SuggestionStyle, }; use rustc_lint_defs::pluralize; @@ -60,7 +61,7 @@ impl HumanReadableErrorType { dst: Box<dyn Write + Send>, source_map: Option<Lrc<SourceMap>>, bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, teach: bool, terminal_width: Option<usize>, macro_backtrace: bool, @@ -233,7 +234,7 @@ pub trait Emitter { /// Return `FluentBundle` with localized diagnostics for the default locale of the compiler. /// Used when the user has not requested a specific language or when a localized diagnostic is /// unavailable for the requested locale. - fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle>; + fn fallback_fluent_bundle(&self) -> &FluentBundle; /// Convert diagnostic arguments (a rustc internal type that exists to implement /// `Encodable`/`Decodable`) into `FluentArgs` which is necessary to perform translation. @@ -579,8 +580,8 @@ impl Emitter for EmitterWriter { self.fluent_bundle.as_ref() } - fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> { - &self.fallback_bundle + fn fallback_fluent_bundle(&self) -> &FluentBundle { + &**self.fallback_bundle } fn emit_diagnostic(&mut self, diag: &Diagnostic) { @@ -635,7 +636,7 @@ impl Emitter for SilentEmitter { None } - fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> { + fn fallback_fluent_bundle(&self) -> &FluentBundle { panic!("silent emitter attempted to translate message") } @@ -695,7 +696,7 @@ pub struct EmitterWriter { dst: Destination, sm: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, short_message: bool, teach: bool, ui_testing: bool, @@ -716,7 +717,7 @@ impl EmitterWriter { color_config: ColorConfig, source_map: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, short_message: bool, teach: bool, terminal_width: Option<usize>, @@ -740,7 +741,7 @@ impl EmitterWriter { dst: Box<dyn Write + Send>, source_map: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, short_message: bool, teach: bool, colored: bool, diff --git a/compiler/rustc_errors/src/json.rs b/compiler/rustc_errors/src/json.rs index f78490da245..0139007da42 100644 --- a/compiler/rustc_errors/src/json.rs +++ b/compiler/rustc_errors/src/json.rs @@ -15,7 +15,9 @@ use crate::emitter::{Emitter, HumanReadableErrorType}; use crate::registry::Registry; use crate::DiagnosticId; use crate::ToolMetadata; -use crate::{CodeSuggestion, FluentBundle, MultiSpan, SpanLabel, SubDiagnostic}; +use crate::{ + CodeSuggestion, FluentBundle, LazyFallbackBundle, MultiSpan, SpanLabel, SubDiagnostic, +}; use rustc_lint_defs::Applicability; use rustc_data_structures::sync::Lrc; @@ -38,7 +40,7 @@ pub struct JsonEmitter { registry: Option<Registry>, sm: Lrc<SourceMap>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, pretty: bool, ui_testing: bool, json_rendered: HumanReadableErrorType, @@ -51,7 +53,7 @@ impl JsonEmitter { registry: Option<Registry>, source_map: Lrc<SourceMap>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, pretty: bool, json_rendered: HumanReadableErrorType, terminal_width: Option<usize>, @@ -75,7 +77,7 @@ impl JsonEmitter { pretty: bool, json_rendered: HumanReadableErrorType, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, terminal_width: Option<usize>, macro_backtrace: bool, ) -> JsonEmitter { @@ -97,7 +99,7 @@ impl JsonEmitter { registry: Option<Registry>, source_map: Lrc<SourceMap>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, pretty: bool, json_rendered: HumanReadableErrorType, terminal_width: Option<usize>, @@ -192,8 +194,8 @@ impl Emitter for JsonEmitter { self.fluent_bundle.as_ref() } - fn fallback_fluent_bundle(&self) -> &Lrc<FluentBundle> { - &self.fallback_bundle + fn fallback_fluent_bundle(&self) -> &FluentBundle { + &**self.fallback_bundle } fn should_show_explain(&self) -> bool { diff --git a/compiler/rustc_errors/src/json/tests.rs b/compiler/rustc_errors/src/json/tests.rs index 0f175c732c1..7eb6a4975fe 100644 --- a/compiler/rustc_errors/src/json/tests.rs +++ b/compiler/rustc_errors/src/json/tests.rs @@ -40,7 +40,7 @@ fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { 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(false).expect("failed to load fallback fluent bundle"); + crate::fallback_fluent_bundle(rustc_error_messages::DEFAULT_LOCALE_RESOURCES, false); let output = Arc::new(Mutex::new(Vec::new())); let je = JsonEmitter::new( diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs index c927fcb2109..cfad1fc01ab 100644 --- a/compiler/rustc_errors/src/lib.rs +++ b/compiler/rustc_errors/src/lib.rs @@ -33,7 +33,7 @@ use rustc_data_structures::sync::{self, Lock, Lrc}; use rustc_data_structures::AtomicRef; pub use rustc_error_messages::{ fallback_fluent_bundle, fluent_bundle, DiagnosticMessage, FluentBundle, LanguageIdentifier, - MultiSpan, SpanLabel, + LazyFallbackBundle, MultiSpan, SpanLabel, DEFAULT_LOCALE_RESOURCES, }; pub use rustc_lint_defs::{pluralize, Applicability}; use rustc_serialize::json::Json; @@ -547,7 +547,7 @@ impl Handler { treat_err_as_bug: Option<NonZeroUsize>, sm: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, ) -> Self { Self::with_tty_emitter_and_flags( color_config, @@ -562,7 +562,7 @@ impl Handler { color_config: ColorConfig, sm: Option<Lrc<SourceMap>>, fluent_bundle: Option<Lrc<FluentBundle>>, - fallback_bundle: Lrc<FluentBundle>, + fallback_bundle: LazyFallbackBundle, flags: HandlerFlags, ) -> Self { let emitter = Box::new(EmitterWriter::stderr( |
