Skip to content

Commit f9b3cea

Browse files
committed
Modify function body in process/physics_process to auto-cast f64 -> f32
1 parent 5fed5cc commit f9b3cea

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::class::{into_signature_info, make_virtual_callback, BeforeKind, Signa
99
use crate::util::ident;
1010
use crate::{util, ParseResult};
1111

12-
use proc_macro2::{Group, Ident, TokenStream};
12+
use proc_macro2::{Delimiter, Group, Ident, TokenStream};
1313
use quote::{quote, ToTokens};
1414

1515
/// Codegen for `#[godot_api] impl ISomething for MyType`.
@@ -479,21 +479,30 @@ fn handle_regular_virtual_fn<'a>(
479479

480480
// If there was a signature change (e.g. f32 -> f64 in process/physics_process), apply to new function tokens.
481481
if !signature_info.modified_param_types.is_empty() {
482+
let mut param_name = None;
483+
482484
let mut new_params = original_method.params.clone();
483485
for (index, new_ty) in signature_info.modified_param_types.iter() {
484486
let venial::FnParam::Typed(typed) = &mut new_params.inner[*index].0 else {
485-
panic!("Unexpected parameter type: {new_params:?}");
487+
panic!("unexpected parameter type: {new_params:?}");
486488
};
487489

488490
typed.ty = new_ty.clone();
491+
param_name = Some(typed.name.clone());
489492
}
490493

491-
let body = original_method
492-
.body
493-
.clone()
494-
.expect("function must have a body");
494+
let original_body = &original_method.body;
495+
let param_name = param_name.expect("parameter had no name");
496+
497+
// Currently hardcoded to f32/f64 exchange; can be generalized if needed.
498+
let body_code = quote! {
499+
let #param_name = #param_name as f32;
500+
#original_body
501+
};
502+
503+
let wrapping_body = Group::new(Delimiter::Brace, body_code);
495504

496-
updated_function = Some((new_params, body));
505+
updated_function = Some((new_params, wrapping_body));
497506
}
498507

499508
// Overridden ready() methods additionally have an additional `__before_ready()` call (for OnReady inits).

itest/rust/src/engine_tests/codegen_test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ impl IHttpRequest for CodegenTest {
9797
fn process(&mut self, _: f64) {}
9898

9999
// Test auto-cast to f32 parameter in virtual function
100-
fn physics_process(&mut self, _delta: f32) {}
100+
fn physics_process(&mut self, delta: f32) {
101+
// Test it's actually f32 in the body.
102+
let _use_param: f32 = delta;
103+
}
101104
}
102105

103106
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)