Rollup merge of #104217 - Nilstrieb:funny-dollar-syntax, r=TaKO8Ki
Display help message when fluent arg was referenced incorrectly The fluent argument syntax is a little special and easy to get wrong, so we emit a small help message when someone gets it wrong. Example: ``` parser_mismatched_closing_delimiter = mismatched closing delimiter: `${delimiter}` ``` panics with ``` thread 'rustc' panicked at 'Encountered errors while formatting message for `parser_mismatched_closing_delimiter` help: Argument `delimiter` exists but was not referenced correctly. Try using `{$delimiter}` instead attr: `None` args: `FluentArgs([("delimiter", String("}"))])` errors: `[ResolverError(Reference(Message { id: "delimiter", attribute: None }))]`', compiler/rustc_errors/src/translation.rs:123:21 ``` fixes #103539
This commit is contained in:
commit
6026785b7a
2 changed files with 31 additions and 10 deletions
|
@ -30,7 +30,8 @@ use intl_memoizer::concurrent::IntlLangMemoizer;
|
||||||
#[cfg(not(parallel_compiler))]
|
#[cfg(not(parallel_compiler))]
|
||||||
use intl_memoizer::IntlLangMemoizer;
|
use intl_memoizer::IntlLangMemoizer;
|
||||||
|
|
||||||
pub use fluent_bundle::{FluentArgs, FluentError, FluentValue};
|
pub use fluent_bundle::{self, FluentArgs, FluentError, FluentValue};
|
||||||
|
|
||||||
pub use unic_langid::{langid, LanguageIdentifier};
|
pub use unic_langid::{langid, LanguageIdentifier};
|
||||||
|
|
||||||
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
|
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
use crate::snippet::Style;
|
use crate::snippet::Style;
|
||||||
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
|
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
use rustc_error_messages::FluentArgs;
|
use rustc_error_messages::{
|
||||||
|
fluent_bundle::resolver::errors::{ReferenceKind, ResolverError},
|
||||||
|
FluentArgs, FluentError,
|
||||||
|
};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// Convert diagnostic arguments (a rustc internal type that exists to implement
|
/// Convert diagnostic arguments (a rustc internal type that exists to implement
|
||||||
|
@ -102,14 +105,31 @@ pub trait Translate {
|
||||||
.or_else(|| translate_with_bundle(self.fallback_fluent_bundle()))
|
.or_else(|| translate_with_bundle(self.fallback_fluent_bundle()))
|
||||||
.map(|(translated, errs)| {
|
.map(|(translated, errs)| {
|
||||||
// Always bail out for errors with the fallback bundle.
|
// Always bail out for errors with the fallback bundle.
|
||||||
assert!(
|
|
||||||
errs.is_empty(),
|
let mut help_messages = vec![];
|
||||||
"identifier: {:?}, attr: {:?}, args: {:?}, errors: {:?}",
|
|
||||||
identifier,
|
if !errs.is_empty() {
|
||||||
attr,
|
for error in &errs {
|
||||||
args,
|
match error {
|
||||||
errs
|
FluentError::ResolverError(ResolverError::Reference(
|
||||||
|
ReferenceKind::Message { id, .. },
|
||||||
|
)) if args.iter().any(|(arg_id, _)| arg_id == id) => {
|
||||||
|
help_messages.push(format!("Argument `{id}` exists but was not referenced correctly. Try using `{{${id}}}` instead"));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
panic!(
|
||||||
|
"Encountered errors while formatting message for `{identifier}`\n\
|
||||||
|
help: {}\n\
|
||||||
|
attr: `{attr:?}`\n\
|
||||||
|
args: `{args:?}`\n\
|
||||||
|
errors: `{errs:?}`",
|
||||||
|
help_messages.join("\nhelp: ")
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
translated
|
translated
|
||||||
})
|
})
|
||||||
.expect("failed to find message in primary or fallback fluent bundles")
|
.expect("failed to find message in primary or fallback fluent bundles")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue