Skip to content

Crash when assigning a Rust instance to a variable with a type #905

Open
@kylewlacy

Description

@kylewlacy

Rust version: rustc 1.62.0 (a8314ef7d 2022-06-27)
Godot version: Godot Engine v3.4.4.stable.official.419e713a2
godot-rust version: 0.10.0
Platform: Windows 11 (x86_64-pc-windows-msvc)

I ran into what looks like a segfault when interacting with a GDNative class created in godot-rust from a GDScript class. I did my best to narrow down the problem as much as possible, but it seems like it's still a pretty niche set of conditions. Here's the Rust source:

use gdnative::prelude::*;

godot_init!(init);

fn init(handle: InitHandle) {
    handle.add_class::<HelloWorld>();
}

#[derive(NativeClass)]
#[inherit(Node)]
pub struct HelloWorld;

impl HelloWorld {
    fn new(_owner: &Node) -> Self {
        godot_print!("[HelloWorld] new");
        HelloWorld
    }
}

#[methods]
impl HelloWorld {
    #[export]
    fn make_another(&self, _owner: &Node) -> Instance<Self, Unique> {
        let result = HelloWorld.emplace();
        godot_print!("[HelloWorld] make_another");
        result
    }
}

Here's the GDScript source:

extends Node

const HelloWorld = preload("res://HelloWorld.gdns")

var hello: HelloWorld

func _ready():
	print("[Example] _ready: Start")
	var new_hello_world: HelloWorld = HelloWorld.new()
	print("[Example] _ready: Created new_hello_world")
	var hello: HelloWorld = new_hello_world.make_another() # <-- Crash here
	print("[Example] _ready: Called make_another")

Here's a full example project with this setup: GodotRustEmplacePoc.zip

When I attach the above GDScript file to a node, the project crashes when assigning to the hello member variable. No error is included in the output, the process just exits with a bad exit code. I'm able to reproduce this consistently when trying to assign the result of a godot-rust method that returns Instance<_, _> to a GDScript variable with a type annotation that matches that instance type. Either using a supertype like Node or removing the type annotation from the hello variable seems to fix the crash.

Is this a bug, or is this violating some constraint I'm not aware of with Instance<_, _> or .emplace()? If this is a bug, is it a bug in godot-rust, or is it an upstream bug in Godot?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions