1
Fork 0

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>
This commit is contained in:
David Wood 2022-03-28 09:36:20 +01:00
parent 7f91697b50
commit d5119c5b9f
23 changed files with 322 additions and 46 deletions

View file

@ -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.

View file

@ -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,

View file

@ -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,

View file

@ -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),

View file

@ -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,