Skip to content

Rust: expand attribute macros #19334

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

redsun82
Copy link
Contributor

No description provided.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Apr 17, 2025
@@ -5,6 +5,7 @@

private import internal.ItemImpl
import codeql.rust.elements.Addressable
import codeql.rust.elements.AstNode

Check warning

Code scanning / CodeQL

Redundant import Warning

Redundant import, the module is already imported inside
codeql.rust.elements.Addressable
.
Redundant import, the module is already imported inside
codeql.rust.elements.Stmt
.
@redsun82 redsun82 force-pushed the redsun82/rust-expand-attr-macros branch from 7c8fc79 to 49cf173 Compare April 25, 2025 14:17
@redsun82 redsun82 changed the title Rust: extract getExpended on Items Rust: expand attribute macros Apr 25, 2025
@redsun82 redsun82 marked this pull request as ready for review April 25, 2025 15:43
@redsun82 redsun82 requested a review from a team as a code owner April 25, 2025 15:43
Copy link
Contributor

@hvitved hvitved left a comment

Choose a reason for hiding this comment

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

Initial comments.

@@ -0,0 +1,2 @@
| attr_macro_expansion.rs:1:1:2:11 | fn foo | attr_macro_expansion.rs:2:4:2:6 | Static |
| attr_macro_expansion.rs:1:1:2:11 | fn foo | attr_macro_expansion.rs:2:4:2:10 | fn foo |
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is it not the attribute that is expanded? It seems weird that the function can expand to two things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that's what the attribute macro does: it takes in an AST (in this case, the definition of a function) and replaces it with possibly multiple items like in this case. So it's not really the attribute that is expanded, it's really the AST node it is attached to that gets entirely replaced.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, thanks for clarifying.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should the expanded items then be ordered, i.e. getExpanded(int i) instead of just getExpanded()?

@@ -1945,4 +1944,4 @@ class FormatArgument(Locatable):

@annotate(Item, add_bases=(Addressable,))
class _:
pass
expanded: optional[AstNode] | child | rust.detach | doc("expanded attribute or procedural macro call of this item")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to have a new base class, say Expandable, for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

considering that MacroCall is also an Item, and that the only remaining case to cover (derive macros) also applies to Items, I don't see the need for another class in the hierarchy. What do you think would be the advantage of such an additional class?

Copy link
Contributor

Choose a reason for hiding this comment

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

In light of your reply to my question above, this question is no longer relevant.

@hvitved
Copy link
Contributor

hvitved commented Apr 28, 2025

I have started a DCA run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants