Skip to content

Commit 6026785

Browse files
authored
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
2 parents e83b476 + bd3c4fb commit 6026785

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

compiler/rustc_error_messages/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use intl_memoizer::concurrent::IntlLangMemoizer;
3030
#[cfg(not(parallel_compiler))]
3131
use intl_memoizer::IntlLangMemoizer;
3232

33-
pub use fluent_bundle::{FluentArgs, FluentError, FluentValue};
33+
pub use fluent_bundle::{self, FluentArgs, FluentError, FluentValue};
34+
3435
pub use unic_langid::{langid, LanguageIdentifier};
3536

3637
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.

compiler/rustc_errors/src/translation.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use crate::snippet::Style;
22
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
33
use rustc_data_structures::sync::Lrc;
4-
use rustc_error_messages::FluentArgs;
4+
use rustc_error_messages::{
5+
fluent_bundle::resolver::errors::{ReferenceKind, ResolverError},
6+
FluentArgs, FluentError,
7+
};
58
use std::borrow::Cow;
69

710
/// Convert diagnostic arguments (a rustc internal type that exists to implement
@@ -102,14 +105,31 @@ pub trait Translate {
102105
.or_else(|| translate_with_bundle(self.fallback_fluent_bundle()))
103106
.map(|(translated, errs)| {
104107
// Always bail out for errors with the fallback bundle.
105-
assert!(
106-
errs.is_empty(),
107-
"identifier: {:?}, attr: {:?}, args: {:?}, errors: {:?}",
108-
identifier,
109-
attr,
110-
args,
111-
errs
112-
);
108+
109+
let mut help_messages = vec![];
110+
111+
if !errs.is_empty() {
112+
for error in &errs {
113+
match error {
114+
FluentError::ResolverError(ResolverError::Reference(
115+
ReferenceKind::Message { id, .. },
116+
)) if args.iter().any(|(arg_id, _)| arg_id == id) => {
117+
help_messages.push(format!("Argument `{id}` exists but was not referenced correctly. Try using `{{${id}}}` instead"));
118+
}
119+
_ => {}
120+
}
121+
}
122+
123+
panic!(
124+
"Encountered errors while formatting message for `{identifier}`\n\
125+
help: {}\n\
126+
attr: `{attr:?}`\n\
127+
args: `{args:?}`\n\
128+
errors: `{errs:?}`",
129+
help_messages.join("\nhelp: ")
130+
);
131+
}
132+
113133
translated
114134
})
115135
.expect("failed to find message in primary or fallback fluent bundles")

0 commit comments

Comments
 (0)