Skip to content

Commit 53ccee0

Browse files
authored
Rollup merge of #107096 - clubby789:fluent-bad-messageref, r=compiler-errors
Detect references to non-existant messages in Fluent resources Should help with cases like #107091, where `{variable}` (a message reference) is accidentally typed, rather than `{$variable}` (a variable reference) Fixes #107370 ```@rustbot``` label +A-translation
2 parents 45430a5 + 0ae0d87 commit 53ccee0

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

compiler/rustc_error_messages/locales/en-US/codegen_gcc.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ codegen_gcc_invalid_monomorphization_unsupported_element =
2323
invalid monomorphization of `{$name}` intrinsic: unsupported {$name} from `{$in_ty}` with element `{$elem_ty}` to `{$ret_ty}`
2424
2525
codegen_gcc_invalid_monomorphization_invalid_bitmask =
26-
invalid monomorphization of `{$name}` intrinsic: invalid bitmask `{ty}`, expected `u{$expected_int_bits}` or `[u8; {$expected_bytes}]`
26+
invalid monomorphization of `{$name}` intrinsic: invalid bitmask `{$ty}`, expected `u{$expected_int_bits}` or `[u8; {$expected_bytes}]`
2727
2828
codegen_gcc_invalid_monomorphization_simd_shuffle =
2929
invalid monomorphization of `{$name}` intrinsic: simd_shuffle index must be an array of `u32`, got `{$ty}`

compiler/rustc_error_messages/locales/en-US/codegen_ssa.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ codegen_ssa_extract_bundled_libs_write_file = failed to write file '{$rlib}': {$
179179
180180
codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
181181
182-
codegen_ssa_apple_sdk_error_sdk_path = failed to get {$sdk_name} SDK path: {error}
182+
codegen_ssa_apple_sdk_error_sdk_path = failed to get {$sdk_name} SDK path: {$error}
183183
184-
codegen_ssa_read_file = failed to read file: {message}
184+
codegen_ssa_read_file = failed to read file: {$message}
185185
186186
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
187187

compiler/rustc_macros/src/diagnostics/fluent.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use annotate_snippets::{
44
};
55
use fluent_bundle::{FluentBundle, FluentError, FluentResource};
66
use fluent_syntax::{
7-
ast::{Attribute, Entry, Identifier, Message},
7+
ast::{
8+
Attribute, Entry, Expression, Identifier, InlineExpression, Message, Pattern,
9+
PatternElement,
10+
},
811
parser::ParserError,
912
};
1013
use proc_macro::{Diagnostic, Level, Span};
@@ -185,9 +188,12 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
185188
};
186189

187190
let mut constants = TokenStream::new();
191+
let mut messagerefs = Vec::new();
188192
for entry in resource.entries() {
189193
let span = res.krate.span();
190-
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
194+
if let Entry::Message(Message { id: Identifier { name }, attributes, value, .. }) =
195+
entry
196+
{
191197
let _ = previous_defns.entry(name.to_string()).or_insert(path_span);
192198

193199
if name.contains('-') {
@@ -200,6 +206,18 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
200206
.emit();
201207
}
202208

209+
if let Some(Pattern { elements }) = value {
210+
for elt in elements {
211+
if let PatternElement::Placeable {
212+
expression:
213+
Expression::Inline(InlineExpression::MessageReference { id, .. }),
214+
} = elt
215+
{
216+
messagerefs.push((id.name, *name));
217+
}
218+
}
219+
}
220+
203221
// Require that the message name starts with the crate name
204222
// `hir_typeck_foo_bar` (in `hir_typeck.ftl`)
205223
// `const_eval_baz` (in `const_eval.ftl`)
@@ -258,6 +276,18 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
258276
}
259277
}
260278

279+
for (mref, name) in messagerefs.into_iter() {
280+
if !previous_defns.contains_key(mref) {
281+
Diagnostic::spanned(
282+
path_span,
283+
Level::Error,
284+
format!("referenced message `{mref}` does not exist (in message `{name}`)"),
285+
)
286+
.help(&format!("you may have meant to use a variable reference (`{{${mref}}}`)"))
287+
.emit();
288+
}
289+
}
290+
261291
if let Err(errs) = bundle.add_resource(resource) {
262292
for e in errs {
263293
match e {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
missing_message_ref = {message}

tests/ui-fulldeps/fluent-messages/test.rs

+9
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ mod missing_crate_name {
9696

9797
use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, test_crate_foo, with_hyphens};
9898
}
99+
100+
mod missing_message_ref {
101+
use super::fluent_messages;
102+
103+
fluent_messages! {
104+
missing => "./missing-message-ref.ftl"
105+
//~^ ERROR referenced message `message` does not exist
106+
}
107+
}

tests/ui-fulldeps/fluent-messages/test.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ LL | test_crate => "./missing-crate-name.ftl",
9393
|
9494
= help: replace any '-'s with '_'s
9595

96-
error: aborting due to 10 previous errors
96+
error: referenced message `message` does not exist (in message `missing_message_ref`)
97+
--> $DIR/test.rs:104:20
98+
|
99+
LL | missing => "./missing-message-ref.ftl"
100+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
101+
|
102+
= help: you may have meant to use a variable reference (`{$message}`)
103+
104+
error: aborting due to 11 previous errors
97105

98106
For more information about this error, try `rustc --explain E0428`.

0 commit comments

Comments
 (0)