Skip to content

Commit

Permalink
Allow #[cfg] to be used with #[constant]
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Oct 12, 2023
1 parent daa6bfa commit 3dc7d32
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
11 changes: 11 additions & 0 deletions godot-macros/src/class/godot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
.map(|func_def| make_method_registration(&class_name, func_def));

let consts = process_godot_constants(&mut decl)?;
let mut integer_constant_cfg_attrs = Vec::new();
let mut integer_constant_names = Vec::new();
let mut integer_constant_values = Vec::new();

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

let name = &constant.name;

// Unlike with #[func] and #[signal], we don't remove the attributes from Constant
// signatures within 'process_godot_constants'.
let cfg_attrs = util::extract_cfg_attrs(&constant.attributes)
.into_iter()
.collect::<Vec<_>>();

// Transport #[cfg] attrs to the FFI glue to ensure constants which were conditionally
// removed from compilation don't cause errors.
integer_constant_cfg_attrs.push(cfg_attrs);
integer_constant_names.push(constant.name.to_string());
integer_constant_values.push(quote! { #class_name::#name });
}
Expand All @@ -153,6 +163,7 @@ fn transform_inherent_impl(mut decl: Impl) -> Result<TokenStream, Error> {
use ::godot::builtin::StringName;

#(
#(#integer_constant_cfg_attrs)*
ExportConstant::new(
#class_name_obj,
ConstantKind::Integer(
Expand Down
19 changes: 19 additions & 0 deletions itest/rust/src/register_tests/constant_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ impl HasConstants {
#[constant]
#[cfg(all())]
const CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR: bool = true;

#[constant]
const CFG_REMOVES_CONSTANT: bool = true;

#[cfg(any())]
#[constant]
const CFG_REMOVES_CONSTANT: bool = false;

#[constant]
#[cfg(any())]
const CFG_REMOVES_CONSTANT: bool = false;

#[cfg(any())]
#[constant]
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;

#[constant]
#[cfg(any())]
const CFG_REMOVES_CONSTANT_FFI_GLUE: bool = true;
}

#[itest]
Expand Down

0 comments on commit 3dc7d32

Please sign in to comment.