Skip to content

Commit 87991d5

Browse files
committed
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.
2 parents 060e47f + 0c7da94 commit 87991d5

File tree

11 files changed

+82
-31
lines changed

11 files changed

+82
-31
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interfac
1414
privacy_from_private_dep_in_public_interface =
1515
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
1616
17-
private_in_public_lint =
17+
privacy_private_in_public_lint =
1818
{$vis_descr} {$kind} `{$descr}` in public interface (error {$kind ->
1919
[trait] E0445
2020
*[other] E0446

compiler/rustc_macros/src/diagnostics/fluent.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
187187
for entry in resource.entries() {
188188
let span = res.ident.span();
189189
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
190-
let _ = previous_defns.entry(name.to_string()).or_insert(ident_span);
190+
let _ = previous_defns.entry(name.to_string()).or_insert(path_span);
191191

192192
if name.contains('-') {
193193
Diagnostic::spanned(
194-
ident_span,
194+
path_span,
195195
Level::Error,
196196
format!("name `{name}` contains a '-' character"),
197197
)
@@ -205,11 +205,23 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
205205
// The last case we error about above, but we want to fall back gracefully
206206
// so that only the error is being emitted and not also one about the macro
207207
// failing.
208-
let snake_name = Ident::new(
209-
// FIXME: should probably trim prefix, not replace all occurrences
210-
&name.replace('-', "_").replace(&format!("{}_", res.ident), ""),
211-
span,
212-
);
208+
let crate_prefix = format!("{}_", res.ident);
209+
210+
let snake_name = name.replace('-', "_");
211+
let snake_name = match snake_name.strip_prefix(&crate_prefix) {
212+
Some(rest) => Ident::new(rest, span),
213+
None => {
214+
Diagnostic::spanned(
215+
path_span,
216+
Level::Error,
217+
format!("name `{name}` does not start with the crate name"),
218+
)
219+
.help(format!("prepend `{crate_prefix}` to the slug name: `{crate_prefix}{snake_name}`"))
220+
.emit();
221+
Ident::new(&snake_name, span)
222+
}
223+
};
224+
213225
constants.extend(quote! {
214226
pub const #snake_name: crate::DiagnosticMessage =
215227
crate::DiagnosticMessage::FluentIdentifier(
@@ -226,7 +238,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
226238

227239
if attr_name.contains('-') {
228240
Diagnostic::spanned(
229-
ident_span,
241+
path_span,
230242
Level::Error,
231243
format!("attribute `{attr_name}` contains a '-' character"),
232244
)
@@ -249,7 +261,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
249261
match e {
250262
FluentError::Overriding { kind, id } => {
251263
Diagnostic::spanned(
252-
ident_span,
264+
path_span,
253265
Level::Error,
254266
format!("overrides existing {}: `{}`", kind, id),
255267
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a_b_key = Value
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
key = Value
1+
a_b_key = Value

src/test/ui-fulldeps/fluent-messages/duplicate-b.ftl

-1
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
some_slug = hi
1+
label_with_hyphens_some_slug = hi
22
.label-has-hyphens = test
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
with-hyphens = 1234
2+
test-crate_foo = abcd
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
this-slug-has-hyphens = hi
1+
slug_with_hyphens_this-slug-has-hyphens = hi

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

+17-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ mod duplicate {
5050

5151
fluent_messages! {
5252
a => "./duplicate-a.ftl",
53-
b => "./duplicate-b.ftl",
54-
//~^ ERROR overrides existing message: `key`
53+
a_b => "./duplicate-a-b.ftl",
54+
//~^ ERROR overrides existing message: `a_b_key`
5555
}
5656
}
5757

@@ -60,7 +60,7 @@ mod slug_with_hyphens {
6060

6161
fluent_messages! {
6262
slug_with_hyphens => "./slug-with-hyphens.ftl",
63-
//~^ ERROR name `this-slug-has-hyphens` contains a '-' character
63+
//~^ ERROR name `slug_with_hyphens_this-slug-has-hyphens` contains a '-' character
6464
}
6565
}
6666

@@ -80,5 +80,18 @@ mod valid {
8080
valid => "./valid.ftl",
8181
}
8282

83-
use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, valid::valid};
83+
use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, valid::key};
84+
}
85+
86+
mod missing_crate_name {
87+
use super::fluent_messages;
88+
89+
fluent_messages! {
90+
test_crate => "./missing-crate-name.ftl",
91+
//~^ ERROR name `test-crate_foo` contains a '-' character
92+
//~| ERROR name `with-hyphens` contains a '-' character
93+
//~| ERROR name `with-hyphens` does not start with the crate name
94+
}
95+
96+
use self::fluent_generated::{DEFAULT_LOCALE_RESOURCES, test_crate::{foo, with_hyphens}};
8497
}

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

+36-12
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,57 @@ error: expected a message field for "missing_message"
2929
| ^^^^^^^^^^^^^^^^^
3030
|
3131

32-
error: overrides existing message: `key`
33-
--> $DIR/test.rs:53:9
32+
error: overrides existing message: `a_b_key`
33+
--> $DIR/test.rs:53:16
3434
|
35-
LL | b => "./duplicate-b.ftl",
36-
| ^
35+
LL | a_b => "./duplicate-a-b.ftl",
36+
| ^^^^^^^^^^^^^^^^^^^^^
3737
|
3838
help: previously defined in this resource
39-
--> $DIR/test.rs:52:9
39+
--> $DIR/test.rs:52:14
4040
|
4141
LL | a => "./duplicate-a.ftl",
42-
| ^
42+
| ^^^^^^^^^^^^^^^^^^^
4343

44-
error: name `this-slug-has-hyphens` contains a '-' character
45-
--> $DIR/test.rs:62:9
44+
error: name `slug_with_hyphens_this-slug-has-hyphens` contains a '-' character
45+
--> $DIR/test.rs:62:30
4646
|
4747
LL | slug_with_hyphens => "./slug-with-hyphens.ftl",
48-
| ^^^^^^^^^^^^^^^^^
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4949
|
5050
= help: replace any '-'s with '_'s
5151

5252
error: attribute `label-has-hyphens` contains a '-' character
53-
--> $DIR/test.rs:71:9
53+
--> $DIR/test.rs:71:31
5454
|
5555
LL | label_with_hyphens => "./label-with-hyphens.ftl",
56-
| ^^^^^^^^^^^^^^^^^^
56+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5757
|
5858
= help: replace any '-'s with '_'s
5959

60-
error: aborting due to 6 previous errors
60+
error: name `with-hyphens` contains a '-' character
61+
--> $DIR/test.rs:90:23
62+
|
63+
LL | test_crate => "./missing-crate-name.ftl",
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
65+
|
66+
= help: replace any '-'s with '_'s
67+
68+
error: name `with-hyphens` does not start with the crate name
69+
--> $DIR/test.rs:90:23
70+
|
71+
LL | test_crate => "./missing-crate-name.ftl",
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
|
74+
= help: prepend `test_crate_` to the slug name: `test_crate_with_hyphens`
75+
76+
error: name `test-crate_foo` contains a '-' character
77+
--> $DIR/test.rs:90:23
78+
|
79+
LL | test_crate => "./missing-crate-name.ftl",
80+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
81+
|
82+
= help: replace any '-'s with '_'s
83+
84+
error: aborting due to 9 previous errors
6185

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
valid = Valid!
1+
valid_key = Valid!

0 commit comments

Comments
 (0)