Skip to content

Commit ca16a8d

Browse files
committed
Change fluent_messages macro to expect _ slugs instead of - slugs
For the most part, the macro actually worked with _ slugs, but the prefix_something -> prefix::something conversion was not implemented. We don't want to accept - slugs for consistency reasons. We thus error if a name is found with - inside. This ensures a consistent style.
1 parent 75d78b9 commit ca16a8d

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

compiler/rustc_macros/src/diagnostics/fluent.rs

+27-5
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,25 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
189189
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
190190
let _ = previous_defns.entry(name.to_string()).or_insert(ident_span);
191191

192-
// `typeck-foo-bar` => `foo_bar` (in `typeck.ftl`)
193-
// `const-eval-baz` => `baz` (in `const_eval.ftl`)
192+
if name.contains('-') {
193+
Diagnostic::spanned(
194+
ident_span,
195+
Level::Error,
196+
format!("name `{name}` contains a '-' character"),
197+
)
198+
.help("replace any '-'s with '_'s")
199+
.emit();
200+
}
201+
202+
// `typeck_foo_bar` => `foo_bar` (in `typeck.ftl`)
203+
// `const_eval_baz` => `baz` (in `const_eval.ftl`)
204+
// `const-eval-hyphen-having` => `hyphen_having` (in `const_eval.ftl`)
205+
// The last case we error about above, but we want to fall back gracefully
206+
// so that only the error is being emitted and not also one about the macro
207+
// failing.
194208
let snake_name = Ident::new(
195209
// FIXME: should probably trim prefix, not replace all occurrences
196-
&name
197-
.replace(&format!("{}-", res.ident).replace('_', "-"), "")
198-
.replace('-', "_"),
210+
&name.replace('-', "_").replace(&format!("{}_", res.ident), ""),
199211
span,
200212
);
201213
constants.extend(quote! {
@@ -212,6 +224,16 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
212224
continue;
213225
}
214226

227+
if attr_name.contains('-') {
228+
Diagnostic::spanned(
229+
ident_span,
230+
Level::Error,
231+
format!("attribute `{attr_name}` contains a '-' character"),
232+
)
233+
.help("replace any '-'s with '_'s")
234+
.emit();
235+
}
236+
215237
constants.extend(quote! {
216238
pub const #snake_name: crate::SubdiagnosticMessage =
217239
crate::SubdiagnosticMessage::FluentAttr(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
some_slug = hi
2+
.label-has-hyphens = test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this-slug-has-hyphens = hi

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

+18
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ mod duplicate {
5555
}
5656
}
5757

58+
mod slug_with_hyphens {
59+
use super::fluent_messages;
60+
61+
fluent_messages! {
62+
slug_with_hyphens => "./slug-with-hyphens.ftl",
63+
//~^ ERROR name `this-slug-has-hyphens` contains a '-' character
64+
}
65+
}
66+
67+
mod label_with_hyphens {
68+
use super::fluent_messages;
69+
70+
fluent_messages! {
71+
label_with_hyphens => "./label-with-hyphens.ftl",
72+
//~^ ERROR attribute `label-has-hyphens` contains a '-' character
73+
}
74+
}
75+
5876
mod valid {
5977
use super::fluent_messages;
6078

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,21 @@ help: previously defined in this resource
4141
LL | a => "./duplicate-a.ftl",
4242
| ^
4343

44-
error: aborting due to 4 previous errors
44+
error: name `this-slug-has-hyphens` contains a '-' character
45+
--> $DIR/test.rs:62:9
46+
|
47+
LL | slug_with_hyphens => "./slug-with-hyphens.ftl",
48+
| ^^^^^^^^^^^^^^^^^
49+
|
50+
= help: replace any '-'s with '_'s
51+
52+
error: attribute `label-has-hyphens` contains a '-' character
53+
--> $DIR/test.rs:71:9
54+
|
55+
LL | label_with_hyphens => "./label-with-hyphens.ftl",
56+
| ^^^^^^^^^^^^^^^^^^
57+
|
58+
= help: replace any '-'s with '_'s
59+
60+
error: aborting due to 6 previous errors
4561

0 commit comments

Comments
 (0)