Skip to content

Commit 2459569

Browse files
committed
Generate fluent message constant in a flat module for all crates
This will make it easier to grep for fluent message names.
1 parent 6c9c2d8 commit 2459569

File tree

1 file changed

+31
-34
lines changed
  • compiler/rustc_macros/src/diagnostics

1 file changed

+31
-34
lines changed

Diff for: compiler/rustc_macros/src/diagnostics/fluent.rs

+31-34
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ use syn::{
2525
use unic_langid::langid;
2626

2727
struct Resource {
28-
ident: Ident,
28+
krate: Ident,
2929
#[allow(dead_code)]
3030
fat_arrow_token: token::FatArrow,
31-
resource: LitStr,
31+
resource_path: LitStr,
3232
}
3333

3434
impl Parse for Resource {
3535
fn parse(input: ParseStream<'_>) -> Result<Self> {
3636
Ok(Resource {
37-
ident: input.parse()?,
37+
krate: input.parse()?,
3838
fat_arrow_token: input.parse()?,
39-
resource: input.parse()?,
39+
resource_path: input.parse()?,
4040
})
4141
}
4242
}
@@ -94,19 +94,20 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
9494
// diagnostics.
9595
let mut previous_defns = HashMap::new();
9696

97+
// Set of Fluent attribute names already output, to avoid duplicate type errors - any given
98+
// constant created for a given attribute is the same.
99+
let mut previous_attrs = HashSet::new();
100+
97101
let mut includes = TokenStream::new();
98102
let mut generated = TokenStream::new();
99-
for res in resources.0 {
100-
let ident_span = res.ident.span().unwrap();
101-
let path_span = res.resource.span().unwrap();
102103

103-
// Set of Fluent attribute names already output, to avoid duplicate type errors - any given
104-
// constant created for a given attribute is the same.
105-
let mut previous_attrs = HashSet::new();
104+
for res in resources.0 {
105+
let krate_span = res.krate.span().unwrap();
106+
let path_span = res.resource_path.span().unwrap();
106107

107-
let relative_ftl_path = res.resource.value();
108+
let relative_ftl_path = res.resource_path.value();
108109
let absolute_ftl_path =
109-
invocation_relative_path_to_absolute(ident_span, &relative_ftl_path);
110+
invocation_relative_path_to_absolute(krate_span, &relative_ftl_path);
110111
// As this macro also outputs an `include_str!` for this file, the macro will always be
111112
// re-executed when the file changes.
112113
let mut resource_file = match File::open(absolute_ftl_path) {
@@ -185,7 +186,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
185186

186187
let mut constants = TokenStream::new();
187188
for entry in resource.entries() {
188-
let span = res.ident.span();
189+
let span = res.krate.span();
189190
if let Entry::Message(Message { id: Identifier { name }, attributes, .. }) = entry {
190191
let _ = previous_defns.entry(name.to_string()).or_insert(path_span);
191192

@@ -199,29 +200,30 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
199200
.emit();
200201
}
201202

202-
// `typeck_foo_bar` => `foo_bar` (in `typeck.ftl`)
203-
// `const_eval_baz` => `baz` (in `const_eval.ftl`)
203+
// Require that the message name starts with the crate name
204+
// `hir_typeck_foo_bar` (in `hir_typeck.ftl`)
205+
// `const_eval_baz` (in `const_eval.ftl`)
204206
// `const-eval-hyphen-having` => `hyphen_having` (in `const_eval.ftl`)
205207
// The last case we error about above, but we want to fall back gracefully
206208
// so that only the error is being emitted and not also one about the macro
207209
// failing.
208-
let crate_prefix = format!("{}_", res.ident);
210+
let crate_prefix = format!("{}_", res.krate);
209211

210212
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-
}
213+
if !snake_name.starts_with(&crate_prefix) {
214+
Diagnostic::spanned(
215+
path_span,
216+
Level::Error,
217+
format!("name `{name}` does not start with the crate name"),
218+
)
219+
.help(format!(
220+
"prepend `{crate_prefix}` to the slug name: `{crate_prefix}{snake_name}`"
221+
))
222+
.emit();
223223
};
224224

225+
let snake_name = Ident::new(&snake_name, span);
226+
225227
constants.extend(quote! {
226228
pub const #snake_name: crate::DiagnosticMessage =
227229
crate::DiagnosticMessage::FluentIdentifier(
@@ -275,12 +277,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
275277

276278
includes.extend(quote! { include_str!(#relative_ftl_path), });
277279

278-
let ident = res.ident;
279-
generated.extend(quote! {
280-
pub mod #ident {
281-
#constants
282-
}
283-
});
280+
generated.extend(constants);
284281
}
285282

286283
quote! {

0 commit comments

Comments
 (0)