Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display help message when fluent arg was referenced incorrectly #104217

Merged
merged 1 commit into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_error_messages/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ use intl_memoizer::concurrent::IntlLangMemoizer;
#[cfg(not(parallel_compiler))]
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};

// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
Expand Down
38 changes: 29 additions & 9 deletions compiler/rustc_errors/src/translation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::snippet::Style;
use crate::{DiagnosticArg, DiagnosticMessage, FluentBundle};
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;

/// Convert diagnostic arguments (a rustc internal type that exists to implement
Expand Down Expand Up @@ -102,14 +105,31 @@ pub trait Translate {
.or_else(|| translate_with_bundle(self.fallback_fluent_bundle()))
.map(|(translated, errs)| {
// Always bail out for errors with the fallback bundle.
assert!(
errs.is_empty(),
"identifier: {:?}, attr: {:?}, args: {:?}, errors: {:?}",
identifier,
attr,
args,
errs
);

let mut help_messages = vec![];

if !errs.is_empty() {
for error in &errs {
match error {
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
})
.expect("failed to find message in primary or fallback fluent bundles")
Expand Down