Skip to content

Commit

Permalink
Change fluent_messages macro to expect _ slugs instead of - slugs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
est31 committed Aug 12, 2022
1 parent 75d78b9 commit ca16a8d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
32 changes: 27 additions & 5 deletions compiler/rustc_macros/src/diagnostics/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,25 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
let _ = previous_defns.entry(name.to_string()).or_insert(ident_span);

// `typeck-foo-bar` => `foo_bar` (in `typeck.ftl`)
// `const-eval-baz` => `baz` (in `const_eval.ftl`)
if name.contains('-') {
Diagnostic::spanned(
ident_span,
Level::Error,
format!("name `{name}` contains a '-' character"),
)
.help("replace any '-'s with '_'s")
.emit();
}

// `typeck_foo_bar` => `foo_bar` (in `typeck.ftl`)
// `const_eval_baz` => `baz` (in `const_eval.ftl`)
// `const-eval-hyphen-having` => `hyphen_having` (in `const_eval.ftl`)
// The last case we error about above, but we want to fall back gracefully
// so that only the error is being emitted and not also one about the macro
// failing.
let snake_name = Ident::new(
// FIXME: should probably trim prefix, not replace all occurrences
&name
.replace(&format!("{}-", res.ident).replace('_', "-"), "")
.replace('-', "_"),
&name.replace('-', "_").replace(&format!("{}_", res.ident), ""),
span,
);
constants.extend(quote! {
Expand All @@ -212,6 +224,16 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
continue;
}

if attr_name.contains('-') {
Diagnostic::spanned(
ident_span,
Level::Error,
format!("attribute `{attr_name}` contains a '-' character"),
)
.help("replace any '-'s with '_'s")
.emit();
}

constants.extend(quote! {
pub const #snake_name: crate::SubdiagnosticMessage =
crate::SubdiagnosticMessage::FluentAttr(
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui-fulldeps/fluent-messages/label-with-hyphens.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some_slug = hi
.label-has-hyphens = test
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/fluent-messages/slug-with-hyphens.ftl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
this-slug-has-hyphens = hi
18 changes: 18 additions & 0 deletions src/test/ui-fulldeps/fluent-messages/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ mod duplicate {
}
}

mod slug_with_hyphens {
use super::fluent_messages;

fluent_messages! {
slug_with_hyphens => "./slug-with-hyphens.ftl",
//~^ ERROR name `this-slug-has-hyphens` contains a '-' character
}
}

mod label_with_hyphens {
use super::fluent_messages;

fluent_messages! {
label_with_hyphens => "./label-with-hyphens.ftl",
//~^ ERROR attribute `label-has-hyphens` contains a '-' character
}
}

mod valid {
use super::fluent_messages;

Expand Down
18 changes: 17 additions & 1 deletion src/test/ui-fulldeps/fluent-messages/test.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,21 @@ help: previously defined in this resource
LL | a => "./duplicate-a.ftl",
| ^

error: aborting due to 4 previous errors
error: name `this-slug-has-hyphens` contains a '-' character
--> $DIR/test.rs:62:9
|
LL | slug_with_hyphens => "./slug-with-hyphens.ftl",
| ^^^^^^^^^^^^^^^^^
|
= help: replace any '-'s with '_'s

error: attribute `label-has-hyphens` contains a '-' character
--> $DIR/test.rs:71:9
|
LL | label_with_hyphens => "./label-with-hyphens.ftl",
| ^^^^^^^^^^^^^^^^^^
|
= help: replace any '-'s with '_'s

error: aborting due to 6 previous errors

0 comments on commit ca16a8d

Please sign in to comment.