Skip to content

Commit

Permalink
Include class name in error message
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Aug 5, 2024
1 parent aa7cb52 commit 71e9d87
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
8 changes: 5 additions & 3 deletions godot-core/src/obj/gd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -824,8 +825,9 @@ where
PropertyHintInfo { hint, hint_string }
}

fn is_node_class() -> bool {
T::inherits::<classes::Node>()
#[doc(hidden)]
fn as_node_class() -> Option<ClassName> {
T::inherits::<classes::Node>().then(|| T::class_name())
}
}

Expand Down
13 changes: 8 additions & 5 deletions godot-core/src/registry/godot_register_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ pub fn register_export<C: GodotClass, T: 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::<classes::Node>() {
panic!(
"Node export is only supported in Node-derived classes, but the current class is {}.",
C::class_name()
);
if !C::inherits::<classes::Node>() {
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::<C, T>(property_name, getter_name, setter_name, hint_info, usage);
Expand Down
9 changes: 6 additions & 3 deletions godot-core/src/registry/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,9 +56,12 @@ pub trait Export: Var {
<Self as Var>::var_hint()
}

/// If this is a class inheriting `Node`, returns the `ClassName`; otherwise `None`.
///
/// Only overridden for `Gd<T>`, 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<ClassName> {
None
}
}

Expand Down

0 comments on commit 71e9d87

Please sign in to comment.