From 71e9d871a9a91101f5c81f6c8401c354307089ca Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Mon, 5 Aug 2024 18:15:23 +0200 Subject: [PATCH] Include class name in error message --- godot-core/src/obj/gd.rs | 8 +++++--- godot-core/src/registry/godot_register_wrappers.rs | 13 ++++++++----- godot-core/src/registry/property.rs | 9 ++++++--- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/godot-core/src/obj/gd.rs b/godot-core/src/obj/gd.rs index 1a34cb3d6..103589d3e 100644 --- a/godot-core/src/obj/gd.rs +++ b/godot-core/src/obj/gd.rs @@ -16,7 +16,8 @@ use crate::builtin::{Callable, NodePath, StringName, Variant}; use crate::global::PropertyHint; use crate::meta::error::{ConvertError, FromFfiError}; use crate::meta::{ - ArrayElement, CallContext, FromGodot, GodotConvert, GodotType, PropertyHintInfo, ToGodot, + ArrayElement, CallContext, ClassName, FromGodot, GodotConvert, GodotType, PropertyHintInfo, + ToGodot, }; use crate::obj::{ bounds, cap, Bounds, EngineEnum, GdDerefTarget, GdMut, GdRef, GodotClass, Inherits, InstanceId, @@ -824,8 +825,9 @@ where PropertyHintInfo { hint, hint_string } } - fn is_node_class() -> bool { - T::inherits::() + #[doc(hidden)] + fn as_node_class() -> Option { + T::inherits::().then(|| T::class_name()) } } diff --git a/godot-core/src/registry/godot_register_wrappers.rs b/godot-core/src/registry/godot_register_wrappers.rs index 859d201c5..24a806060 100644 --- a/godot-core/src/registry/godot_register_wrappers.rs +++ b/godot-core/src/registry/godot_register_wrappers.rs @@ -25,11 +25,14 @@ pub fn register_export( ) { // Note: if the user manually specifies `hint`, `hint_string` or `usage` keys, and thus is routed to `register_var()` instead, // they can bypass this validation. - if T::is_node_class() && !C::inherits::() { - panic!( - "Node export is only supported in Node-derived classes, but the current class is {}.", - C::class_name() - ); + if !C::inherits::() { + if let Some(class) = T::as_node_class() { + panic!( + "#[export] for Gd<{t}>: nodes can only be exported in Node-derived classes, but the current class is {c}.", + t = class, + c = C::class_name() + ); + } } register_var::(property_name, getter_name, setter_name, hint_info, usage); diff --git a/godot-core/src/registry/property.rs b/godot-core/src/registry/property.rs index c92b27f80..59ed9daf0 100644 --- a/godot-core/src/registry/property.rs +++ b/godot-core/src/registry/property.rs @@ -9,7 +9,7 @@ use godot_ffi as sys; -use crate::meta::{FromGodot, GodotConvert, GodotType, PropertyHintInfo, ToGodot}; +use crate::meta::{ClassName, FromGodot, GodotConvert, GodotType, PropertyHintInfo, ToGodot}; // ---------------------------------------------------------------------------------------------------------------------------------------------- // Trait definitions @@ -56,9 +56,12 @@ pub trait Export: Var { ::var_hint() } + /// If this is a class inheriting `Node`, returns the `ClassName`; otherwise `None`. + /// /// Only overridden for `Gd`, to detect erroneous exports of `Node` inside a `Resource` class. - fn is_node_class() -> bool { - false + #[allow(clippy::wrong_self_convention)] + fn as_node_class() -> Option { + None } }