@@ -25,18 +25,18 @@ use syn::{
25
25
use unic_langid:: langid;
26
26
27
27
struct Resource {
28
- ident : Ident ,
28
+ krate : Ident ,
29
29
#[ allow( dead_code) ]
30
30
fat_arrow_token : token:: FatArrow ,
31
- resource : LitStr ,
31
+ resource_path : LitStr ,
32
32
}
33
33
34
34
impl Parse for Resource {
35
35
fn parse ( input : ParseStream < ' _ > ) -> Result < Self > {
36
36
Ok ( Resource {
37
- ident : input. parse ( ) ?,
37
+ krate : input. parse ( ) ?,
38
38
fat_arrow_token : input. parse ( ) ?,
39
- resource : input. parse ( ) ?,
39
+ resource_path : input. parse ( ) ?,
40
40
} )
41
41
}
42
42
}
@@ -94,19 +94,20 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
94
94
// diagnostics.
95
95
let mut previous_defns = HashMap :: new ( ) ;
96
96
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
+
97
101
let mut includes = TokenStream :: new ( ) ;
98
102
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 ( ) ;
102
103
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 ( ) ;
106
107
107
- let relative_ftl_path = res. resource . value ( ) ;
108
+ let relative_ftl_path = res. resource_path . value ( ) ;
108
109
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) ;
110
111
// As this macro also outputs an `include_str!` for this file, the macro will always be
111
112
// re-executed when the file changes.
112
113
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
185
186
186
187
let mut constants = TokenStream :: new ( ) ;
187
188
for entry in resource. entries ( ) {
188
- let span = res. ident . span ( ) ;
189
+ let span = res. krate . span ( ) ;
189
190
if let Entry :: Message ( Message { id : Identifier { name } , attributes, .. } ) = entry {
190
191
let _ = previous_defns. entry ( name. to_string ( ) ) . or_insert ( path_span) ;
191
192
@@ -199,29 +200,30 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
199
200
. emit ( ) ;
200
201
}
201
202
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`)
204
206
// `const-eval-hyphen-having` => `hyphen_having` (in `const_eval.ftl`)
205
207
// The last case we error about above, but we want to fall back gracefully
206
208
// so that only the error is being emitted and not also one about the macro
207
209
// failing.
208
- let crate_prefix = format ! ( "{}_" , res. ident ) ;
210
+ let crate_prefix = format ! ( "{}_" , res. krate ) ;
209
211
210
212
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 ( ) ;
223
223
} ;
224
224
225
+ let snake_name = Ident :: new ( & snake_name, span) ;
226
+
225
227
constants. extend ( quote ! {
226
228
pub const #snake_name: crate :: DiagnosticMessage =
227
229
crate :: DiagnosticMessage :: FluentIdentifier (
@@ -275,12 +277,7 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
275
277
276
278
includes. extend ( quote ! { include_str!( #relative_ftl_path) , } ) ;
277
279
278
- let ident = res. ident ;
279
- generated. extend ( quote ! {
280
- pub mod #ident {
281
- #constants
282
- }
283
- } ) ;
280
+ generated. extend ( constants) ;
284
281
}
285
282
286
283
quote ! {
0 commit comments