Skip to content

Commit b1a9a14

Browse files
committed
Allow #[cfg] to be used with #[constant]
1 parent 165afeb commit b1a9a14

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

godot-macros/src/class/godot_api.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
129129
.map(|func_def| make_method_registration(&class_name, func_def));
130130

131131
let consts = process_godot_constants(&mut decl)?;
132+
let mut integer_constant_cfg_attrs = Vec::new();
132133
let mut integer_constant_names = Vec::new();
133134
let mut integer_constant_values = Vec::new();
134135

@@ -139,6 +140,15 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
139140

140141
let name = &constant.name;
141142

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

152162
#(
163+
#(#integer_constant_cfg_attrs)*
153164
ExportConstant::new(
154165
#class_name_obj,
155166
ConstantKind::Integer(

itest/rust/src/register_tests/constant_test.rs

Lines changed: 19 additions & 0 deletions
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)