Skip to content

Emit proper compile error while trying to #[export] Gd<T> or DynGd<T, D>. #1243

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

Merged
merged 1 commit into from
Jul 22, 2025

Conversation

Yarwin
Copy link
Contributor

@Yarwin Yarwin commented Jul 21, 2025

Changes compile error from:

error[E0271]: type mismatch resolving `<Node as Bounds>::Memory == MemRefCounted`
  --> src/lib.rs:15:5
   |
15 |     #[export]
   |     ^ expected `MemRefCounted`, found `MemManual`
   |
   = note: required for `godot::prelude::Gd<godot::prelude::Node>` to implement `Default`

error[E0277]: `#[var]` properties require `Var` trait; #[export] ones require `Export` trait
  --> src/lib.rs:16:20
   |
16 |     faulty_export: Gd<Node>,
   |                    ^^^^^^^^ type cannot be used as a property
   |
   = help: the trait `Export` is not implemented for `godot::prelude::Gd<godot::prelude::Node>`
   = note: see also: https://godot-rust.github.io/book/register/properties.html
   = help: the following other types implement trait `Export`:
             Aabb
             Array<DynGd<T, D>>
             Array<T>
             Array<godot::prelude::Gd<T>>
             Basis
             Color
             Dictionary
             GString
           and 42 others

Some errors have detailed explanations: E0271, E0277.
For more information about an error, try `rustc --explain E0271`.
error: could not compile `docstest` (lib) due to 3 previous errors

to:

error: `Gd` and `DynGd` cannot be used directly as properties because they are non-nullable. Use `OnEditor<T>` or `Option<T>` instead.
  --> src/lib.rs:16:20
   |
16 |     faulty_export: Gd<Node>,
   |                    ^^

error: could not compile `docstest` (lib) due to 1 previous error

in case if one tries to export non-nullable property such as:

    #[export]
    faulty_export: Gd<Node>,

I'm not sure if adding extra info/note to diagnostic::on_unimplemented wouldn't be better choice though 🤔. It doesn't seem to break or change anything 🤔 (checked with two medium-sized projects + our tests obviously). Gd<T> couldn't be an #[export] anyway, so 🤔

It provides a bit better IDE experience (notes are omitted by Zed and RustRover):

image image

@Yarwin Yarwin added quality-of-life No new functionality, but improves ergonomics/internals c: register Register classes, functions and other symbols to GDScript labels Jul 21, 2025
@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-1243

@Bromeon
Copy link
Member

Bromeon commented Jul 21, 2025

The result is very nice. But we should generally decide how much we want proc-macros to introspect concrete types, especially in cases where it's "only" informational. For example, initially removing #[base] for Base<T> fields #577 was a bit controversial (also on Discord) due to the "magic", and I added #[hint] attributes to override macro inference.

Here, there's tiny small chance that the macro rejects valid code because other types happen to be named Gd/DynGd as well. This is very unlikely though, so I'm not categorically against it, if the user experience is overall positive.

However, I also want to be careful about setting a precedent to do all sorts of validations on the proc-macro level.


I'm not sure if adding extra info/note to diagnostic::on_unimplemented wouldn't be better choice though 🤔

That lint doesn't work for specific attempts (impls that are tried but are absent), does it? So on the type-system level there isn't a better message than the generic one 🤔

@Bromeon
Copy link
Member

Bromeon commented Jul 21, 2025

Also clarified semantics in #1244.

@Yarwin Yarwin force-pushed the add-gd-export-compile-error branch from 0366b05 to 8d2c975 Compare July 21, 2025 14:30
@Yarwin
Copy link
Contributor Author

Yarwin commented Jul 21, 2025

Here, there's tiny small chance that the macro rejects valid code because other types happen to be named Gd/DynGd as well. This is very unlikely though, so I'm not categorically against it, if the user experience is overall positive.

Yeah, it irks me as well – I can't really say what (if anything) can go wrong and I would rather avoid something which might have weird side effects 🤔. It is also yet another thing which can go wrong or stop working during refactors or after heavier changes.

I decided to add an extra note to Export diagnostic instead (shame we can't control types shown in second help section):

error[E0277]: `#[var]` properties require `Var` trait; #[export] ones require `Export` trait
  --> src/lib.rs:16:20
   |
16 |     faulty_export: Gd<Node>,
   |                    ^^^^^^^^ type cannot be used as a property
   |
   = help: the trait `Export` is not implemented for `godot::prelude::Gd<godot::prelude::Node>`
   = note: see also: https://godot-rust.github.io/book/register/properties.html
   = note: `Gd` and `DynGd` cannot be used directly as properties because they are non-nullable. In such a case use `OnEditor<T>` or `Option<T>` instead.
   = help: the following other types implement trait `Export`:
             Aabb
             Array<DynGd<T, D>>
             Array<T>
             Array<godot::prelude::Gd<T>>
             Basis
             Color
             Dictionary
             GString
           and 42 others

@Bromeon
Copy link
Member

Bromeon commented Jul 21, 2025

The note is too long, it should ideally not need horizontal scrollbar here.

Would it be possible to have

   = note: `Gd` and `DynGd` cannot be exported directly; wrap them in `Option<...>` or `OnEditor<...>`.

…about `#[export]` of `Gd<T>` or `DynGd<T, D>`.
@Yarwin Yarwin force-pushed the add-gd-export-compile-error branch from 8d2c975 to aaa823f Compare July 21, 2025 22:03
@Yarwin
Copy link
Contributor Author

Yarwin commented Jul 21, 2025

The note is too long, it should ideally not need horizontal scrollbar here.

Changed; Aye, we don't have to be so mouthful after docs has been updated.

@Bromeon Bromeon added this to the 0.3.x milestone Jul 22, 2025
@Bromeon Bromeon added this pull request to the merge queue Jul 22, 2025
Merged via the queue into godot-rust:master with commit efd7f23 Jul 22, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: register Register classes, functions and other symbols to GDScript quality-of-life No new functionality, but improves ergonomics/internals
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants