-
-
Notifications
You must be signed in to change notification settings - Fork 273
Add type-checking to #[var(get, set)]
#1469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
0f48648 to
7fc5593
Compare
|
API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1469 |
godot-core/src/obj/on_editor.rs
Outdated
| /// pub fn set_my_node(&mut self, value: Gd<Node>) { | ||
| /// *self.my_node = value; | ||
| /// } | ||
| /// | ||
| /// #[func] | ||
| /// pub fn get_my_value(&self) -> i32 { | ||
| /// self.my_value.get_property() | ||
| /// *self.my_value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both won't work in an editor – dereferencing OnEditor will result in panic if value is not set yet (and it can't be set because we can't dereference it...).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, great remark! So it would also be Var::var_get() here...
I guess we shoukd document this 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, and set should mirror it as well
godot-core/src/obj/on_editor.rs
Outdated
| fn var_get(field: &Self) -> Self::Via { | ||
| field | ||
| .get_property_inner() | ||
| .expect("OnEditor is not nullable") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change expect message (OnEditor field of given type is not nullable and mustn't be None?)
i.e. leave the information why this expect in var getter is fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me;
PR in general looks intimidating but it is smaller than it seems – bulk of the changes are due to switching from &mut self to field: &mut Self in Var signature (+ blanket impl SimpleVar).
I don't get why docs for OnEditor about setters/getters has been changed though.
|
Thanks a lot for the great review! 👍
Yep, as written this is based on #1466 -- maybe I should have linked only the 3 relevant commits (out of 6):
I wasn't thinking of the editor case, so I accidentally "simplified" the doctests (they kept passing in unit-tests). Will fix that + add documentation why going through Regarding "/// Use the default |
I think Maybe just "use Var instead of the Deref" or so? should be OKay |
1d61b2a to
da38a98
Compare
It was possible for user-defined getters and setters to return/accept types that are completely unrelated to the field type. This introduces descriptive compile-time errors, with tweaked spans.
da38a98 to
6392fb6
Compare
It was possible for user-defined getters and setters to return/accept types that are completely unrelated to the field type.
This PR introduces descriptive compile-time errors, with tweaked spans for beautiful error messages.
Based on #1466.
Example
causes these errors:
(Yes, it's not quite symmetric, if someone wants to tweak this further, I'm happy for PRs 🙂)
Breaking change
Previously, having wrong getter/setter types worked as long as they were compatible in Godot subtying terms, e.g.
Gd<Derived>->Gd<Base>, ori32->Variant, etc. The new typechecks are now stricter.There are a few instances where such weak typing was used in itests, possibly deliberately, or possibly due to limitations in early godot-rust.
gdext/itest/rust/src/object_tests/property_test.rs
Lines 25 to 70 in 5a7b65c