gdext icon indicating copy to clipboard operation
gdext copied to clipboard

`#[func]` function arguments with invalid types produce weird errors

Open lilizoey opened this issue 9 months ago • 2 comments

A function defined like this:

#[godot_api]
impl Bar {
    #[func]
    fn add_two(&mut self, my_param: &String) {}
}

Produces a "missing lifetime specifier" error

Screenshot_20240503_183427

Which is because the macro does type Sig = ((), &String) which then produces a missing lifetime error. We could instead produce an error in the macro when a reference is used in an argument's type (except for the receiver).


Additionally rust-analyzer produces a "non-exhaustive pattern" error

Screenshot_20240503_183115

This is because when R-A to find an implementation of the *Signature traits for ((), &String) it will assume the Params associated type is ((), &String) instead of (&String,) as intended. This leads to us trying to destruct a ((), &String) using (my_param,) pattern.

One solution could be to create a struct Signature<Ret, Params> { .. } type which we use instead of a pure tuple. Then the macro could explicitly set the return value and the arguments separately.


For a function like this where the invalid argument isn't a reference:

#[godot_api]
impl Bar {
    #[func]
    fn add_two(&mut self, my_param: Vec<String>) {}
}

The error is instead that Vec<String> doesn't implement ToGodot/FromGodot as expected, however the error is displayed on the godot_api attribute, instead of on the Vec<String> argument type as i would expect. Screenshot_20240503_184234

(the my_param error is the R-A error from above)

lilizoey avatar May 03 '24 16:05 lilizoey