Skip to content
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

Fix #[func]-like attrs being ignored with macros below them #442

Merged
merged 1 commit into from
Oct 6, 2023

Conversation

PgBiel
Copy link
Contributor

@PgBiel PgBiel commented Oct 6, 2023

This fixes the bug mentioned in #379 (comment).

To better explain the problem, consider the following modified code from the Dodge the Creeps example.

#[godot_api]
impl Mob {
    #[func]
    #[cfg(all())]
    fn on_visibility_screen_exited(&mut self) {
        self.base.queue_free();
    }
   /* ... */
}

Note: #[cfg(all())] is a #[cfg] check which should always pass / be a tautology (something like #[cfg(TRUE)], but that doesn't work by itself). This PR does not fix (yet) errors which appear when #[cfg] removes the function.

Before this PR: compile-time error:

    Checking dodge-the-creeps v0.1.0 (/.../gdext/examples/dodge-the-creeps/rust)
error: cannot find attribute `func` in this scope
  --> examples/dodge-the-creeps/rust/src/mob.rs:17:7
   |
17 |     #[func]
   |       ^^^^

error: could not compile `dodge-the-creeps` (lib) due to previous error

After this PR: compiles.

The problem, as mentioned in my original response, is that the extract_attributes function would keep re-assigning the found: Option<BoundAttr> variable even after a #[func]-like attr was already found, thus setting it to None when some unrecognized attribute appeared under #[func]. This fixes the problem by just skipping unrecognized attributes - found is only re-assigned if a #[func]-like attribute is actually found (and this preserves the error when two consecutive #[func]-like attributes are specified).

Added tests under itest.

(Also no idea why clippy is failing here lol, I didn't change that file!)

@GodotRust
Copy link

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

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Can you reference the symbols somewhere in Rust code (assign constant or function result to dummy value)? This would test that the symbols are indeed present.

Comment on lines 35 to 41
#[cfg(all())]
#[constant]
const CONST_SHOULD_STILL_BE_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_ABOVE_CONST_ATTR: bool = true;

#[constant]
#[cfg(all())]
const CONST_SHOULD_STILL_BE_RECOGNIZED_WITH_SIMPLE_PATH_ATTRIBUTE_UNDER_CONST_ATTR: bool = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use "below" instead of "under", and "constant" instead of "const".

Also, can the identifiers be a bit shorter? "should still be recognized" can be shortened to just "recognized".

Same applies to functions 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, and done! 😁

@Bromeon Bromeon added bug c: register Register classes, functions and other symbols to GDScript labels Oct 6, 2023
@PgBiel PgBiel changed the title Fix #[func]-like attrs being ignored with macros under them Fix #[func]-like attrs being ignored with macros below them Oct 6, 2023
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

Regarding failed clippy, this is addressed in #441, so I might merge that one first (given readiness).

@Bromeon Bromeon enabled auto-merge October 6, 2023 20:22
@Bromeon
Copy link
Member

Bromeon commented Oct 6, 2023

Replaced assert!(ALWAYS_TRUE_CONSTANT) with static_assert! due to Clippy warning. These technically don't need to be inside the #[itest], but it's easier to understand like this.

@Bromeon Bromeon added this pull request to the merge queue Oct 6, 2023
Merged via the queue into godot-rust:master with commit a92164b Oct 6, 2023
14 checks passed
@PgBiel PgBiel deleted the fix-attr-macro-recognition branch October 9, 2023 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug c: register Register classes, functions and other symbols to GDScript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants