Skip to content

Commit 3dc7d32

Browse files
committed
Allow #[cfg] to be used with #[constant]
1 parent daa6bfa commit 3dc7d32

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
@@ -132,6 +132,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
132132
.map(|func_def| make_method_registration(&class_name, func_def));
133133

134134
let consts = process_godot_constants(&mut decl)?;
135+
let mut integer_constant_cfg_attrs = Vec::new();
135136
let mut integer_constant_names = Vec::new();
136137
let mut integer_constant_values = Vec::new();
137138

@@ -142,6 +143,15 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
142143

143144
let name = &constant.name;
144145

146+
// Unlike with #[func] and #[signal], we don't remove the attributes from Constant
147+
// signatures within 'process_godot_constants'.
148+
let cfg_attrs = util::extract_cfg_attrs(&constant.attributes)
149+
.into_iter()
150+
.collect::<Vec<_>>();
151+
152+
// Transport #[cfg] attrs to the FFI glue to ensure constants which were conditionally
153+
// removed from compilation don't cause errors.
154+
integer_constant_cfg_attrs.push(cfg_attrs);
145155
integer_constant_names.push(constant.name.to_string());
146156
integer_constant_values.push(quote! { #class_name::#name });
147157
}
@@ -153,6 +163,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
153163
use ::godot::builtin::StringName;
154164

155165
#(
166+
#(#integer_constant_cfg_attrs)*
156167
ExportConstant::new(
157168
#class_name_obj,
158169
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)