Skip to content

Commit a88d02f

Browse files
committed
Allow #[cfg] to be used with #[constant]
1 parent 7116516 commit a88d02f

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

godot-macros/src/class/godot_api.rs

+11
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
136136
.map(|func_def| make_method_registration(&class_name, func_def));
137137

138138
let consts = process_godot_constants(&mut decl)?;
139+
let mut integer_constant_cfg_attrs = Vec::new();
139140
let mut integer_constant_names = Vec::new();
140141
let mut integer_constant_values = Vec::new();
141142

@@ -146,6 +147,15 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
146147

147148
let name = &constant.name;
148149

150+
// Unlike with #[func] and #[signal], we don't remove the attributes from Constant
151+
// signatures within 'process_godot_constants'.
152+
let cfg_attrs = util::extract_cfg_attrs(&constant.attributes)
153+
.into_iter()
154+
.collect::<Vec<_>>();
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);
149159
integer_constant_names.push(constant.name.to_string());
150160
integer_constant_values.push(quote! { #class_name::#name });
151161
}
@@ -157,6 +167,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
157167
use ::godot::builtin::StringName;
158168

159169
#(
170+
#(#integer_constant_cfg_attrs)*
160171
ExportConstant::new(
161172
#class_name_obj,
162173
ConstantKind::Integer(

itest/rust/src/register_tests/constant_test.rs

+19
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ impl HasConstants {
4040
#[constant]
4141
#[cfg(all())]
4242
const CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR: bool = true;
43+
44+
#[constant]
45+
const CFG_REMOVES_CONSTANT: bool = true;
46+
47+
#[cfg(any())]
48+
#[constant]
49+
const CFG_REMOVES_CONSTANT: bool = false;
50+
51+
#[constant]
52+
#[cfg(any())]
53+
const CFG_REMOVES_CONSTANT: bool = false;
54+
55+
#[cfg(any())]
56+
#[constant]
57+
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;
58+
59+
#[constant]
60+
#[cfg(any())]
61+
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;
4362
}
4463

4564
#[itest]

0 commit comments

Comments
 (0)