Skip to content

Commit

Permalink
Fix #[func]-like attrs being ignored with macros under them
Browse files Browse the repository at this point in the history
  • Loading branch information
PgBiel committed Oct 6, 2023
1 parent 88a7934 commit bc87b5e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
19 changes: 10 additions & 9 deletions godot-macros/src/class/godot_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,42 +337,43 @@ where
let rename = parser.handle_expr("rename")?.map(|ts| ts.to_string());
let has_gd_self = parser.handle_alone("gd_self")?;

Some(BoundAttr {
BoundAttr {
attr_name: attr_name.clone(),
index,
ty: BoundAttrType::Func {
rename,
has_gd_self,
},
})
}
}
name if name == "signal" => {
// TODO once parameters are supported, this should probably be moved to the struct definition
// E.g. a zero-sized type Signal<(i32, String)> with a provided emit(i32, String) method
// This could even be made public (callable on the struct obj itself)
Some(BoundAttr {
BoundAttr {
attr_name: attr_name.clone(),
index,
ty: BoundAttrType::Signal(attr.value.clone()),
})
}
}
name if name == "constant" => Some(BoundAttr {
name if name == "constant" => BoundAttr {
attr_name: attr_name.clone(),
index,
ty: BoundAttrType::Const(attr.value.clone()),
}),
_ => None,
},
// Ignore unknown attributes
_ => continue,
};

// Validate at most 1 attribute
if found.is_some() && new_found.is_some() {
if found.is_some() {
bail!(
&error_scope,
"at most one #[func], #[signal], or #[constant] attribute per declaration allowed",
)?;
}

found = new_found;
found = Some(new_found);
}

Ok(found)
Expand Down
11 changes: 11 additions & 0 deletions itest/rust/src/register_tests/constant_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

// Needed for Clippy to accept #[cfg(all())]
#![allow(clippy::non_minimal_cfg)]

use crate::framework::itest;
use godot::engine::ClassDb;
use godot::prelude::*;
Expand All @@ -28,6 +31,14 @@ impl HasConstants {
#[constant]
#[rustfmt::skip]
const DONT_PANIC_WITH_SEGMENTED_PATH_ATTRIBUTE: bool = true;

#[cfg(all())]
#[constant]
const CONST_SHOULD_STILL_BE_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_ABOVE_CONST_ATTR: bool = true;

#[constant]
#[cfg(all())]
const CONST_SHOULD_STILL_BE_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_UNDER_CONST_ATTR: bool = true;
}

#[itest]
Expand Down
23 changes: 23 additions & 0 deletions itest/rust/src/register_tests/func_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

// Needed for Clippy to accept #[cfg(all())]
#![allow(clippy::non_minimal_cfg)]

use godot::prelude::*;

#[derive(GodotClass)]
Expand Down Expand Up @@ -66,10 +69,30 @@ impl GdSelfReference {
true
}

#[cfg(all())]
#[func]
fn func_should_still_be_recognized_with_simple_path_attribute_above_func_attr() -> bool {
true
}

#[func]
#[cfg(all())]
fn func_should_still_be_recognized_with_simple_path_attribute_under_func_attr() -> bool {
true
}

#[signal]
#[rustfmt::skip]
fn signal_shouldnt_panic_with_segmented_path_attribute();

#[cfg(all())]
#[signal]
fn signal_should_still_be_recognized_with_simple_path_attribute_above_signal_attr();

#[signal]
#[cfg(all())]
fn signal_should_still_be_recognized_with_simple_path_attribute_under_signal_attr();

#[func]
fn fail_to_update_internal_value_due_to_conflicting_borrow(
&mut self,
Expand Down

0 comments on commit bc87b5e

Please sign in to comment.