Skip to content

Commit 119ef6c

Browse files
committed
Fix typo in std::mem::transmute causing UB while using AsArg<DynGd<Base, D>>.
1 parent b9a6cd5 commit 119ef6c

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

godot-core/src/meta/args/as_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ where
178178
{
179179
if T::IS_SAME_CLASS {
180180
// SAFETY: T == Base, so &DynGd<T, D> can be treated as &DynGd<Base, D>.
181-
let gd_ref = unsafe { std::mem::transmute::<&Gd<T>, &DynGd<Base, D>>(self) };
181+
let gd_ref = unsafe { std::mem::transmute::<&DynGd<T, D>, &DynGd<Base, D>>(self) };
182182
CowArg::Borrowed(gd_ref)
183183
} else {
184184
// Different types: clone and upcast. May incur ref-count increment for RefCounted objects, but the common path

itest/rust/src/object_tests/dyn_gd_test.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,18 @@ fn dyn_gd_store_in_godot_array() {
431431
}
432432

433433
#[itest]
434-
fn dyn_gd_as_arg() {
434+
fn dyn_gd_as_arg_inherited_base() {
435435
let refc_health = Gd::from_object(RefcHealth { hp: 42 }).into_dyn();
436436
let node_health = foreign::NodeHealth::new_alloc().into_dyn();
437437
let typed_none = None::<&DynGd<RefcHealth, dyn Health>>;
438438

439+
// Array<DynGd<Base, D>>.
440+
// See: https://github.com/godot-rust/gdext/pull/1345.
441+
let array: Array<DynGd<RefcHealth, dyn Health>> = array![&refc_health];
442+
assert_eq!(array.len(), 1);
443+
let first = array.at(0);
444+
assert_eq!(first.dyn_bind().get_hitpoints(), 42);
445+
439446
// Array<DynGd>.
440447
let array: Array<DynGd<Object, dyn Health>> = array![&refc_health, &node_health];
441448
assert_eq!(array.len(), 2);

0 commit comments

Comments
 (0)