Skip to content

Commit

Permalink
Improve #[rpc] error message if Base<T> field is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Nov 10, 2024
1 parent 1d14ac2 commit 343eb47
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion godot-core/src/obj/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub trait IndexEnum: EngineEnum {
#[diagnostic::on_unimplemented(
message = "Class `{Self}` requires a `Base<T>` field",
label = "missing field `_base: Base<...>`",
note = "A base field is required to access the base from within `self`, or when using script virtual functions",
note = "A base field is required to access the base from within `self`, for script-virtual functions or #[rpc] methods",
note = "see also: https://godot-rust.github.io/book/register/classes.html#the-base-field"
)]
pub trait WithBaseField: GodotClass + Bounds<Declarer = bounds::DeclUser> {
Expand Down
13 changes: 8 additions & 5 deletions godot-macros/src/class/data_models/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ pub fn make_rpc_registrations_fn(class_name: &Ident, funcs: &[FuncDefinition]) -
}

quote! {
#[allow(clippy::needless_update)] // clippy complains about using `..RpcConfig::default()` if all fields are overridden
// Clippy complains about using `..RpcConfig::default()` if all fields are overridden.
#[allow(clippy::needless_update)]
fn __register_rpcs(object: &mut dyn ::std::any::Any) {
use ::std::any::Any;
use ::godot::register::RpcConfig;
Expand All @@ -83,10 +84,12 @@ pub fn make_rpc_registrations_fn(class_name: &Ident, funcs: &[FuncDefinition]) -
use ::godot::classes::Node;
use ::godot::obj::{WithBaseField, Gd};

let mut gd = object
.downcast_mut::<#class_name>()
.expect("bad type erasure when registering RPCs")
.to_gd();
let this = object
.downcast_ref::<#class_name>()
.expect("bad type erasure when registering RPCs");

// Use fully-qualified syntax, so that error message isn't just "no method named `to_gd` found".
let mut gd = ::godot::obj::WithBaseField::to_gd(this);

let node = gd.upcast_mut::<Node>();
#( #rpc_registrations )*
Expand Down

0 comments on commit 343eb47

Please sign in to comment.