about summary refs log tree commit diff
path: root/compiler/rustc_macros/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-08-23 22:30:07 +0000
committerbors <bors@rust-lang.org>2022-08-23 22:30:07 +0000
commit87991d5f5d72d6baca490141cb890211ba2f3843 (patch)
tree4d453c889d4f511b3ddbbdac69d123deee641a45 /compiler/rustc_macros/src
parent060e47f74a91b28c3db28d80a6a9d6841ca51026 (diff)
parent0c7da94be01694cb816ac3465aa608da89451f50 (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.
Diffstat (limited to 'compiler/rustc_macros/src')
-rw-r--r--compiler/rustc_macros/src/diagnostics/fluent.rs30
1 files changed, 21 insertions, 9 deletions
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),
                         )