Skip to content

Commit e06e67b

Browse files
committed
Allow #[cfg] to be used with #[constant]
1 parent 52da90d commit e06e67b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
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

+32-1
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,35 @@ 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: i64 = 5;
46+
47+
#[cfg(any())]
48+
#[constant]
49+
const CFG_REMOVES_CONSTANT: i64 = 10;
50+
51+
#[constant]
52+
#[cfg(any())]
53+
const CFG_REMOVES_CONSTANT: i64 = 10;
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]
4665
fn constants_correct_value() {
47-
const CONSTANTS: [(&str, i64); 4] = [
66+
const CONSTANTS: [(&str, i64); 5] = [
4867
("A", HasConstants::A),
4968
("B", HasConstants::B as i64),
5069
("C", HasConstants::C as i64),
5170
("D", HasConstants::D as i64),
71+
("CFG_REMOVES_CONSTANT", HasConstants::CFG_REMOVES_CONSTANT),
5272
];
5373

5474
let constants = ClassDb::singleton()
@@ -72,6 +92,17 @@ fn constants_correct_value() {
7292
static_assert!(HasConstants::CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR);
7393
}
7494

95+
#[itest]
96+
fn cfg_removes_or_keeps_constants() {
97+
let has_constant = |name: &str| {
98+
ClassDb::singleton()
99+
.class_has_integer_constant(HasConstants::class_name().to_string_name(), name.into())
100+
};
101+
102+
assert!(has_constant("CFG_REMOVES_CONSTANT"));
103+
assert!(!has_constant("CFG_REMOVES_CONSTANT_FFI_GLUE"));
104+
}
105+
75106
#[derive(GodotClass)]
76107
struct HasOtherConstants {}
77108

0 commit comments

Comments
 (0)