Skip to content

Commit d10e9a0

Browse files
authored
Merge pull request #995 from jrb0001/cfg-before-api
Fix nightly compiler warnings about #[cfg(before_api = "4.3")] in the generated #[godot_api] impl
2 parents f6e2d0c + ed21957 commit d10e9a0

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

examples/dodge-the-creeps/rust/src/main_scene.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::player;
55
use godot::classes::{Marker2D, PathFollow2D, RigidBody2D, Timer};
66
use godot::prelude::*;
77

8+
use godot::classes::notify::NodeNotification;
89
use rand::Rng as _;
910
use std::f32::consts::PI;
1011

@@ -116,6 +117,8 @@ impl Main {
116117

117118
#[godot_api]
118119
impl INode for Main {
120+
fn on_notification(&mut self, _what: NodeNotification) {}
121+
119122
fn init(base: Base<Node>) -> Self {
120123
Main {
121124
mob_scene: PackedScene::new_gd(),

godot-macros/src/class/data_models/interface_trait_impl.rs

+27-25
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
142142
}
143143

144144
"on_notification" => {
145+
let inactive_class_early_return = make_inactive_class_check(TokenStream::new());
145146
on_notification_impl = quote! {
146147
#on_notification_impl
147148

@@ -150,10 +151,7 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
150151
fn __godot_notification(&mut self, what: i32) {
151152
use ::godot::obj::UserClass as _;
152153

153-
#[cfg(before_api = "4.3")]
154-
if ::godot::private::is_class_inactive(Self::__config().is_tool) {
155-
return;
156-
}
154+
#inactive_class_early_return
157155

158156
<Self as #trait_path>::on_notification(self, what.into())
159157
}
@@ -168,16 +166,14 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
168166
}
169167

170168
"get_property" => {
169+
let inactive_class_early_return = make_inactive_class_check(quote! { None });
171170
get_property_impl = quote! {
172171
#(#cfg_attrs)*
173172
impl ::godot::obj::cap::GodotGet for #class_name {
174173
fn __godot_get_property(&self, property: ::godot::builtin::StringName) -> Option<::godot::builtin::Variant> {
175174
use ::godot::obj::UserClass as _;
176175

177-
#[cfg(before_api = "4.3")]
178-
if ::godot::private::is_class_inactive(Self::__config().is_tool) {
179-
return None;
180-
}
176+
#inactive_class_early_return
181177

182178
<Self as #trait_path>::get_property(self, property)
183179
}
@@ -191,16 +187,14 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
191187
}
192188

193189
"set_property" => {
190+
let inactive_class_early_return = make_inactive_class_check(quote! { false });
194191
set_property_impl = quote! {
195192
#(#cfg_attrs)*
196193
impl ::godot::obj::cap::GodotSet for #class_name {
197194
fn __godot_set_property(&mut self, property: ::godot::builtin::StringName, value: ::godot::builtin::Variant) -> bool {
198195
use ::godot::obj::UserClass as _;
199196

200-
#[cfg(before_api = "4.3")]
201-
if ::godot::private::is_class_inactive(Self::__config().is_tool) {
202-
return false;
203-
}
197+
#inactive_class_early_return
204198

205199
<Self as #trait_path>::set_property(self, property, value)
206200
}
@@ -227,19 +221,15 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
227221

228222
#[cfg(since_api = "4.3")]
229223
"get_property_list" => {
224+
// `get_property_list` is only supported in Godot API >= 4.3. If we add support for `get_property_list` to earlier
225+
// versions of Godot then this code is still needed and should be uncommented.
226+
//
227+
// let inactive_class_early_return = make_inactive_class_check(false);
230228
get_property_list_impl = quote! {
231229
#(#cfg_attrs)*
232230
impl ::godot::obj::cap::GodotGetPropertyList for #class_name {
233231
fn __godot_get_property_list(&mut self) -> Vec<::godot::meta::PropertyInfo> {
234-
// `get_property_list` is only supported in Godot API >= 4.3. If we add support for `get_property_list` to earlier
235-
// versions of Godot then this code is still needed and should be uncommented.
236-
//
237-
// use ::godot::obj::UserClass as _;
238-
//
239-
// #[cfg(before_api = "4.3")]
240-
// if ::godot::private::is_class_inactive(Self::__config().is_tool) {
241-
// return false;
242-
// }
232+
// #inactive_class_early_return
243233

244234
<Self as #trait_path>::get_property_list(self)
245235
}
@@ -257,16 +247,14 @@ pub fn transform_trait_impl(original_impl: venial::Impl) -> ParseResult<TokenStr
257247
}
258248

259249
"property_get_revert" => {
250+
let inactive_class_early_return = make_inactive_class_check(quote! { None });
260251
property_get_revert_impl = quote! {
261252
#(#cfg_attrs)*
262253
impl ::godot::obj::cap::GodotPropertyGetRevert for #class_name {
263254
fn __godot_property_get_revert(&self, property: StringName) -> Option<::godot::builtin::Variant> {
264255
use ::godot::obj::UserClass as _;
265256

266-
#[cfg(before_api = "4.3")]
267-
if ::godot::private::is_class_inactive(Self::__config().is_tool) {
268-
return None;
269-
}
257+
#inactive_class_early_return
270258

271259
<Self as #trait_path>::property_get_revert(self, property)
272260
}
@@ -435,3 +423,17 @@ fn convert_to_match_expression_or_none(tokens: Option<TokenStream>) -> TokenStre
435423
quote! { None }
436424
}
437425
}
426+
427+
#[cfg(before_api = "4.3")]
428+
fn make_inactive_class_check(return_value: TokenStream) -> TokenStream {
429+
quote! {
430+
if ::godot::private::is_class_inactive(Self::__config().is_tool) {
431+
return #return_value;
432+
}
433+
}
434+
}
435+
436+
#[cfg(since_api = "4.3")]
437+
fn make_inactive_class_check(_return_value: TokenStream) -> TokenStream {
438+
TokenStream::new()
439+
}

0 commit comments

Comments
 (0)