Skip to content

Commit a92164b

Browse files
authored
Merge pull request #442 from PgBiel/fix-attr-macro-recognition
Fix #[func]-like attrs being ignored with macros below them
2 parents de73f39 + 5835012 commit a92164b

File tree

3 files changed

+60
-12
lines changed

3 files changed

+60
-12
lines changed

godot-macros/src/class/godot_api.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,42 +337,43 @@ where
337337
let rename = parser.handle_expr("rename")?.map(|ts| ts.to_string());
338338
let has_gd_self = parser.handle_alone("gd_self")?;
339339

340-
Some(BoundAttr {
340+
BoundAttr {
341341
attr_name: attr_name.clone(),
342342
index,
343343
ty: BoundAttrType::Func {
344344
rename,
345345
has_gd_self,
346346
},
347-
})
347+
}
348348
}
349349
name if name == "signal" => {
350350
// TODO once parameters are supported, this should probably be moved to the struct definition
351351
// E.g. a zero-sized type Signal<(i32, String)> with a provided emit(i32, String) method
352352
// This could even be made public (callable on the struct obj itself)
353-
Some(BoundAttr {
353+
BoundAttr {
354354
attr_name: attr_name.clone(),
355355
index,
356356
ty: BoundAttrType::Signal(attr.value.clone()),
357-
})
357+
}
358358
}
359-
name if name == "constant" => Some(BoundAttr {
359+
name if name == "constant" => BoundAttr {
360360
attr_name: attr_name.clone(),
361361
index,
362362
ty: BoundAttrType::Const(attr.value.clone()),
363-
}),
364-
_ => None,
363+
},
364+
// Ignore unknown attributes
365+
_ => continue,
365366
};
366367

367368
// Validate at most 1 attribute
368-
if found.is_some() && new_found.is_some() {
369+
if found.is_some() {
369370
bail!(
370371
&error_scope,
371372
"at most one #[func], #[signal], or #[constant] attribute per declaration allowed",
372373
)?;
373374
}
374375

375-
found = new_found;
376+
found = Some(new_found);
376377
}
377378

378379
Ok(found)

itest/rust/src/register_tests/constant_test.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7+
// Needed for Clippy to accept #[cfg(all())]
8+
#![allow(clippy::non_minimal_cfg)]
9+
710
use crate::framework::itest;
811
use godot::engine::ClassDb;
912
use godot::prelude::*;
13+
use godot::sys::static_assert;
1014

1115
#[derive(GodotClass)]
1216
struct HasConstants {}
@@ -28,6 +32,14 @@ impl HasConstants {
2832
#[constant]
2933
#[rustfmt::skip]
3034
const DONT_PANIC_WITH_SEGMENTED_PATH_ATTRIBUTE: bool = true;
35+
36+
#[cfg(all())]
37+
#[constant]
38+
const CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_ABOVE_CONST_ATTR: bool = true;
39+
40+
#[constant]
41+
#[cfg(all())]
42+
const CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR: bool = true;
3143
}
3244

3345
#[itest]
@@ -54,6 +66,10 @@ fn constants_correct_value() {
5466
constant_value
5567
);
5668
}
69+
70+
// Ensure the constants are still present and are equal to 'true'
71+
static_assert!(HasConstants::CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_ABOVE_CONST_ATTR);
72+
static_assert!(HasConstants::CONSTANT_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_BELOW_CONST_ATTR);
5773
}
5874

5975
#[derive(GodotClass)]
@@ -73,7 +89,7 @@ impl HasOtherConstants {
7389

7490
// TODO: replace with proc-macro api when constant enums and bitfields can be exported through the
7591
// proc-macro.
76-
impl ::godot::obj::cap::ImplementsGodotApi for HasOtherConstants {
92+
impl godot::obj::cap::ImplementsGodotApi for HasOtherConstants {
7793
fn __register_methods() {}
7894
fn __register_constants() {
7995
use ::godot::builtin::meta::registration::constant::*;
@@ -107,8 +123,8 @@ impl ::godot::obj::cap::ImplementsGodotApi for HasOtherConstants {
107123
}
108124
}
109125

110-
::godot::sys::plugin_add!(
111-
__GODOT_PLUGIN_REGISTRY in::godot::private;
126+
godot::sys::plugin_add!(
127+
__GODOT_PLUGIN_REGISTRY in ::godot::private;
112128
::godot::private::ClassPlugin {
113129
class_name: HasOtherConstants::class_name(),
114130
component: ::godot::private::PluginComponent::UserMethodBinds {

itest/rust/src/register_tests/func_test.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7+
// Needed for Clippy to accept #[cfg(all())]
8+
#![allow(clippy::non_minimal_cfg)]
9+
710
use godot::prelude::*;
811

912
#[derive(GodotClass)]
@@ -66,10 +69,38 @@ impl GdSelfReference {
6669
true
6770
}
6871

72+
#[cfg(all())]
73+
#[func]
74+
fn func_recognized_with_simple_path_attribute_above_func_attr() -> bool {
75+
true
76+
}
77+
78+
#[func]
79+
#[cfg(all())]
80+
fn func_recognized_with_simple_path_attribute_below_func_attr() -> bool {
81+
true
82+
}
83+
84+
#[func]
85+
fn funcs_above_are_kept() -> bool {
86+
let f2 = Self::func_recognized_with_simple_path_attribute_above_func_attr();
87+
let f1 = Self::func_recognized_with_simple_path_attribute_below_func_attr();
88+
89+
f1 && f2
90+
}
91+
6992
#[signal]
7093
#[rustfmt::skip]
7194
fn signal_shouldnt_panic_with_segmented_path_attribute();
7295

96+
#[cfg(all())]
97+
#[signal]
98+
fn signal_recognized_with_simple_path_attribute_above_signal_attr();
99+
100+
#[signal]
101+
#[cfg(all())]
102+
fn signal_recognized_with_simple_path_attribute_below_signal_attr();
103+
73104
#[func]
74105
fn fail_to_update_internal_value_due_to_conflicting_borrow(
75106
&mut self,

0 commit comments

Comments
 (0)