@@ -73,6 +73,12 @@ struct SignalDefinition {
73
73
attributes : Vec < Attribute > ,
74
74
}
75
75
76
+ /// Holds information known from a constant's definition
77
+ struct ConstantDefinition {
78
+ constant : Constant ,
79
+ attributes : Vec < Attribute > ,
80
+ }
81
+
76
82
/// Codegen for `#[godot_api] impl MyType`
77
83
fn transform_inherent_impl ( mut decl : Impl ) -> Result < TokenStream , Error > {
78
84
let class_name = util:: validate_impl ( & decl, None , "godot_api" ) ?;
@@ -129,16 +135,27 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
129
135
. map ( |func_def| make_method_registration ( & class_name, func_def) ) ;
130
136
131
137
let consts = process_godot_constants ( & mut decl) ?;
138
+ let mut integer_constant_cfg_attrs = Vec :: new ( ) ;
132
139
let mut integer_constant_names = Vec :: new ( ) ;
133
140
let mut integer_constant_values = Vec :: new ( ) ;
134
141
135
- for constant in consts. iter ( ) {
142
+ for constant_def in consts. iter ( ) {
143
+ let ConstantDefinition {
144
+ constant,
145
+ attributes,
146
+ } = constant_def;
136
147
if constant. initializer . is_none ( ) {
137
148
return bail ! ( constant, "exported const should have initializer" ) ;
138
149
} ;
139
150
140
151
let name = & constant. name ;
152
+ let cfg_attrs = util:: extract_cfg_attrs ( attributes)
153
+ . into_iter ( )
154
+ . collect :: < Vec < _ > > ( ) ;
141
155
156
+ // Transport #[cfg] attrs to the FFI glue to ensure constants which were conditionally
157
+ // removed from compilation don't cause errors.
158
+ integer_constant_cfg_attrs. push ( cfg_attrs) ;
142
159
integer_constant_names. push ( constant. name . to_string ( ) ) ;
143
160
integer_constant_values. push ( quote ! { #class_name:: #name } ) ;
144
161
}
@@ -150,6 +167,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
150
167
use :: godot:: builtin:: StringName ;
151
168
152
169
#(
170
+ #( #integer_constant_cfg_attrs) *
153
171
ExportConstant :: new(
154
172
#class_name_obj,
155
173
ConstantKind :: Integer (
@@ -307,15 +325,16 @@ fn process_godot_fns(
307
325
Ok ( ( func_definitions, signal_definitions) )
308
326
}
309
327
310
- fn process_godot_constants ( decl : & mut Impl ) -> Result < Vec < Constant > , Error > {
311
- let mut constant_signatures = vec ! [ ] ;
328
+ fn process_godot_constants ( decl : & mut Impl ) -> Result < Vec < ConstantDefinition > , Error > {
329
+ let mut constant_definitions = vec ! [ ] ;
312
330
313
331
for item in decl. body_items . iter_mut ( ) {
314
332
let ImplMember :: Constant ( constant) = item else {
315
333
continue ;
316
334
} ;
317
335
318
336
if let Some ( attr) = extract_attributes ( & constant, & constant. attributes ) ? {
337
+ let attributes = constant. attributes . clone ( ) ;
319
338
// Remaining code no longer has attribute -- rest stays
320
339
constant. attributes . remove ( attr. index ) ;
321
340
@@ -330,13 +349,16 @@ fn process_godot_constants(decl: &mut Impl) -> Result<Vec<Constant>, Error> {
330
349
if constant. initializer . is_none ( ) {
331
350
return bail ! ( constant, "exported constant must have initializer" ) ;
332
351
}
333
- constant_signatures. push ( constant. clone ( ) ) ;
352
+ constant_definitions. push ( ConstantDefinition {
353
+ constant : constant. clone ( ) ,
354
+ attributes,
355
+ } ) ;
334
356
}
335
357
}
336
358
}
337
359
}
338
360
339
- Ok ( constant_signatures )
361
+ Ok ( constant_definitions )
340
362
}
341
363
342
364
fn extract_attributes < T > (
0 commit comments