diff options
| author | bors <bors@rust-lang.org> | 2022-08-23 22:30:07 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-08-23 22:30:07 +0000 |
| commit | 87991d5f5d72d6baca490141cb890211ba2f3843 (patch) | |
| tree | 4d453c889d4f511b3ddbbdac69d123deee641a45 | |
| parent | 060e47f74a91b28c3db28d80a6a9d6841ca51026 (diff) | |
| parent | 0c7da94be01694cb816ac3465aa608da89451f50 (diff) | |
Auto merge of #100675 - Xiretza:fluent-mandate-crate-prefix, r=davidtwco
fluent: mandate slug names to be prefixed by crate name This is currently only convention, but not actively checked for. Additionally, improve error messages to highlight the path of the offending fluent file rather than the identifier preceding it. This will conflict with #100671, so I'll leave it as draft until that's merged.
11 files changed, 82 insertions, 31 deletions
diff --git a/compiler/rustc_error_messages/locales/en-US/privacy.ftl b/compiler/rustc_error_messages/locales/en-US/privacy.ftl index 97050635f45..223092a74bd 100644 --- a/compiler/rustc_error_messages/locales/en-US/privacy.ftl +++ b/compiler/rustc_error_messages/locales/en-US/privacy.ftl @@ -14,7 +14,7 @@ privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interfac privacy_from_private_dep_in_public_interface = {$kind} `{$descr}` from private dependency '{$krate}' in public interface -private_in_public_lint = +privacy_private_in_public_lint = {$vis_descr} {$kind} `{$descr}` in public interface (error {$kind -> [trait] E0445 *[other] E0446 diff --git a/compiler/rustc_macros/src/diagnostics/fluent.rs b/compiler/rustc_macros/src/diagnostics/fluent.rs index 2e7652ad333..f7d8b494ee2 100644 --- a/compiler/rustc_macros/src/diagnostics/fluent.rs +++ b/compiler/rustc_macros/src/diagnostics/fluent.rs @@ -187,11 +187,11 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok for entry in resource.entries() { let span = res.ident.span(); if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry { - let _ = previous_defns.entry(name.to_string()).or_insert(ident_span); + let _ = previous_defns.entry(name.to_string()).or_insert(path_span); if name.contains('-') { Diagnostic::spanned( - ident_span, + path_span, Level::Error, format!("name `{name}` contains a '-' character"), ) @@ -205,11 +205,23 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok // 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('-', "_").replace(&format!("{}_", res.ident), ""), - span, - ); + let crate_prefix = format!("{}_", res.ident); + + let snake_name = name.replace('-', "_"); + let snake_name = match snake_name.strip_prefix(&crate_prefix) { + Some(rest) => Ident::new(rest, span), + None => { + Diagnostic::spanned( + path_span, + Level::Error, + format!("name `{name}` does not start with the crate name"), + ) + .help(format!("prepend `{crate_prefix}` to the slug name: `{crate_prefix}{snake_name}`")) + .emit(); + Ident::new(&snake_name, span) + } + }; + constants.extend(quote! { pub const #snake_name: crate::DiagnosticMessage = crate::DiagnosticMessage::FluentIdentifier( @@ -226,7 +238,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok if attr_name.contains('-') { Diagnostic::spanned( - ident_span, + path_span, Level::Error, format!("attribute `{attr_name}` contains a '-' character"), ) @@ -249,7 +261,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok match e { FluentError::Overriding { kind, id } => { Diagnostic::spanned( - ident_span, + path_span, Level::Error, format!("overrides existing {}: `{}`", kind, id), ) diff --git a/src/test/ui-fulldeps/fluent-messages/duplicate-a-b.ftl b/src/test/ui-fulldeps/fluent-messages/duplicate-a-b.ftl new file mode 100644 index 00000000000..9407c517045 --- /dev/null +++ b/src/test/ui-fulldeps/fluent-messages/duplicate-a-b.ftl @@ -0,0 +1 @@ +a_b_key = Value diff --git a/src/test/ui-fulldeps/fluent-messages/duplicate-a.ftl b/src/test/ui-fulldeps/fluent-messages/duplicate-a.ftl index fd9976b5a41..9407c517045 100644 --- a/src/test/ui-fulldeps/fluent-messages/duplicate-a.ftl +++ b/src/test/ui-fulldeps/fluent-messages/duplicate-a.ftl @@ -1 +1 @@ -key = Value +a_b_key = Value diff --git a/src/test/ui-fulldeps/fluent-messages/duplicate-b.ftl b/src/test/ui-fulldeps/fluent-messages/duplicate-b.ftl deleted file mode 100644 index fd9976b5a41..00000000000 --- a/src/test/ui-fulldeps/fluent-messages/duplicate-b.ftl +++ /dev/null @@ -1 +0,0 @@ -key = Value diff --git a/src/test/ui-fulldeps/fluent-messages/label-with-hyphens.ftl b/src/test/ui-fulldeps/fluent-messages/label-with-hyphens.ftl index 84b6c3e6c00..016cbeef662 100644 --- a/src/test/ui-fulldeps/fluent-messages/label-with-hyphens.ftl +++ b/src/test/ui-fulldeps/fluent-messages/label-with-hyphens.ftl @@ -1,2 +1,2 @@ -some_slug = hi +label_with_hyphens_some_slug = hi .label-has-hyphens = test diff --git a/src/test/ui-fulldeps/fluent-messages/missing-crate-name.ftl b/src/test/ui-fulldeps/fluent-messages/missing-crate-name.ftl new file mode 100644 index 00000000000..9bd035c1bba --- /dev/null +++ b/src/test/ui-fulldeps/fluent-messages/missing-crate-name.ftl @@ -0,0 +1,2 @@ +with-hyphens = 1234 +test-crate_foo = abcd diff --git a/src/test/ui-fulldeps/fluent-messages/slug-with-hyphens.ftl b/src/test/ui-fulldeps/fluent-messages/slug-with-hyphens.ftl index 07517c9a243..86ba9a268f3 100644 --- a/src/test/ui-fulldeps/fluent-messages/slug-with-hyphens.ftl +++ b/src/test/ui-fulldeps/fluent-messages/slug-with-hyphens.ftl @@ -1 +1 @@ -this-slug-has-hyphens = hi +slug_with_hyphens_this-slug-has-hyphens = hi diff --git a/src/test/ui-fulldeps/fluent-messages/test.rs b/src/test/ui-fulldeps/fluent-messages/test.rs index f0f35780666..256857e52a7 100644 --- a/src/test/ui-fulldeps/fluent-messages/test.rs +++ b/src/test/ui-fulldeps/fluent-messages/test.rs @@ -50,8 +50,8 @@ mod duplicate { fluent_messages! { a => "./duplicate-a.ftl", - b => "./duplicate-b.ftl", -//~^ ERROR overrides existing message: `key` + a_b => "./duplicate-a-b.ftl", +//~^ ERROR overrides existing message: `a_b_key` } } @@ -60,7 +60,7 @@ mod slug_with_hyphens { fluent_messages! { slug_with_hyphens => "./slug-with-hyphens.ftl", -//~^ ERROR name `this-slug-has-hyphens` contains a '-' character +//~^ ERROR name `slug_with_hyphens_this-slug-has-hyphens` contains a '-' character } } @@ -80,5 +80,18 @@ mod valid { valid => "./valid.ftl", } - use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, valid::valid}; + use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, valid::key}; +} + +mod missing_crate_name { + use super::fluent_messages; + + fluent_messages! { + test_crate => "./missing-crate-name.ftl", +//~^ ERROR name `test-crate_foo` contains a '-' character +//~| ERROR name `with-hyphens` contains a '-' character +//~| ERROR name `with-hyphens` does not start with the crate name + } + + use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, test_crate::{foo, with_hyphens}}; } diff --git a/src/test/ui-fulldeps/fluent-messages/test.stderr b/src/test/ui-fulldeps/fluent-messages/test.stderr index 856642c4818..26d87430a8f 100644 --- a/src/test/ui-fulldeps/fluent-messages/test.stderr +++ b/src/test/ui-fulldeps/fluent-messages/test.stderr @@ -29,33 +29,57 @@ error: expected a message field for "missing_message" | ^^^^^^^^^^^^^^^^^ | -error: overrides existing message: `key` - --> $DIR/test.rs:53:9 +error: overrides existing message: `a_b_key` + --> $DIR/test.rs:53:16 | -LL | b => "./duplicate-b.ftl", - | ^ +LL | a_b => "./duplicate-a-b.ftl", + | ^^^^^^^^^^^^^^^^^^^^^ | help: previously defined in this resource - --> $DIR/test.rs:52:9 + --> $DIR/test.rs:52:14 | LL | a => "./duplicate-a.ftl", - | ^ + | ^^^^^^^^^^^^^^^^^^^ -error: name `this-slug-has-hyphens` contains a '-' character - --> $DIR/test.rs:62:9 +error: name `slug_with_hyphens_this-slug-has-hyphens` contains a '-' character + --> $DIR/test.rs:62:30 | 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 + --> $DIR/test.rs:71:31 | LL | label_with_hyphens => "./label-with-hyphens.ftl", - | ^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: replace any '-'s with '_'s -error: aborting due to 6 previous errors +error: name `with-hyphens` contains a '-' character + --> $DIR/test.rs:90:23 + | +LL | test_crate => "./missing-crate-name.ftl", + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: replace any '-'s with '_'s + +error: name `with-hyphens` does not start with the crate name + --> $DIR/test.rs:90:23 + | +LL | test_crate => "./missing-crate-name.ftl", + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: prepend `test_crate_` to the slug name: `test_crate_with_hyphens` + +error: name `test-crate_foo` contains a '-' character + --> $DIR/test.rs:90:23 + | +LL | test_crate => "./missing-crate-name.ftl", + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: replace any '-'s with '_'s + +error: aborting due to 9 previous errors diff --git a/src/test/ui-fulldeps/fluent-messages/valid.ftl b/src/test/ui-fulldeps/fluent-messages/valid.ftl index 0eee4a02b96..54927430600 100644 --- a/src/test/ui-fulldeps/fluent-messages/valid.ftl +++ b/src/test/ui-fulldeps/fluent-messages/valid.ftl @@ -1 +1 @@ -valid = Valid! +valid_key = Valid! |
