Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
merged 13 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rust: separate attribute macro and macro call expansions
  • Loading branch information
Paolo Tranquilli committed Apr 29, 2025
commit a7a887c828f1c2f7cd507606e13feaf052ff97a1
2 changes: 1 addition & 1 deletion 2 rust/extractor/src/generated/.generated.list

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions 10 rust/extractor/src/generated/top.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions 11 rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,11 @@ impl<'a> Translator<'a> {
let expand_to = ra_ap_hir_expand::ExpandTo::from_call_site(mcall);
let kind = expanded.kind();
if let Some(value) = self.emit_expanded_as(expand_to, expanded) {
generated::Item::emit_expanded(label.into(), value, &mut self.trap.writer);
generated::MacroCall::emit_macro_call_expansion(
label,
value,
&mut self.trap.writer,
);
} else {
let range = self.text_range_for_node(mcall);
self.emit_parse_error(mcall, &SyntaxError::new(
Expand Down Expand Up @@ -655,8 +659,9 @@ impl<'a> Translator<'a> {
} = semantics.expand_attr_macro(node)?;
// TODO emit err?
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to register any failure reason as a diagnostic, so we can keep track of how many macro expansions were successful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

after looking a bit at the rust analyzer code, I have the feeling the err in the ExpandResult gets recovered again in parse_macro_expansion_error which is called by our emit_macro_expansion_parse_errors function called just below. So I think we should ignore the err here, as otherwise we would be emitting a diagnostic twice about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that makes sense. I guess that leaves the ? after the cast as a potential error that may be worth reporting. I suppose the result is always a MacroItem, but perhaps it could be just an Item if the macro expands to a single item, or an Error if something went wrong (although in that case I'd expect a parse error to be reported).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, maybe reporting an error there makes sense, just to be defensive. I think attribute and derive macros will always expand to MacroItems, as that's the only context in which they appear.

self.emit_macro_expansion_parse_errors(node, &expanded);
let expanded = self.emit_expanded_as(ExpandTo::Items, expanded)?;
generated::Item::emit_expanded(label, expanded, &mut self.trap.writer);
let macro_items = ast::MacroItems::cast(expanded)?;
let expanded = self.emit_macro_items(&macro_items)?;
generated::Item::emit_attribute_macro_expansion(label, expanded, &mut self.trap.writer);
Some(())
})();
}
Expand Down
89 changes: 45 additions & 44 deletions 89 rust/ql/.generated.list

Large diffs are not rendered by default.

35 changes: 18 additions & 17 deletions 35 rust/ql/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 2 rust/ql/lib/codeql/rust/controlflow/CfgNodes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ final class MacroCallCfgNode extends Nodes::MacroCallCfgNode {

/** Gets the CFG node for the expansion of this macro call, if it exists. */
CfgNode getExpandedNode() {
any(ChildMapping mapping).hasCfgChild(node, node.getExpanded(), this, result)
any(ChildMapping mapping).hasCfgChild(node, node.getMacroCallExpansion(), this, result)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class StructPatChildMapping extends ParentAstNode, StructPat {
}

class MacroCallChildMapping extends ParentAstNode, MacroCall {
override predicate relevantChild(AstNode child) { child = this.getExpanded() }
override predicate relevantChild(AstNode child) { child = this.getMacroCallExpansion() }
}

class FormatArgsExprChildMapping extends ParentAstNode, CfgImpl::ExprTrees::FormatArgsExprTree {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private predicate guaranteedMatchPosition(Pat pat) {
parent.(OrPat).getLastPat() = pat
or
// for macro patterns we propagate to the expanded pattern
parent.(MacroPat).getMacroCall().getExpanded() = pat
parent.(MacroPat).getMacroCall().getMacroCallExpansion() = pat
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class LetStmtTree extends PreOrderTree, LetStmt {
}

class MacroCallTree extends StandardPostOrderTree, MacroCall {
override AstNode getChildNode(int i) { i = 0 and result = this.getExpanded() }
override AstNode getChildNode(int i) { i = 0 and result = this.getMacroCallExpansion() }
}

class MacroStmtsTree extends StandardPreOrderTree, MacroStmts {
Expand Down Expand Up @@ -685,7 +685,7 @@ module PatternTrees {
}

class MacroPatTree extends PreOrderPatTree, MacroPat {
override Pat getPat(int i) { i = 0 and result = this.getMacroCall().getExpanded() }
override Pat getPat(int i) { i = 0 and result = this.getMacroCall().getMacroCallExpansion() }
}

class OrPatTree extends PostOrderPatTree instanceof OrPat {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion 2 rust/ql/lib/codeql/rust/elements/Item.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions 1 rust/ql/lib/codeql/rust/elements/MacroCall.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion 10 rust/ql/lib/codeql/rust/elements/MacroItems.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module Impl {

/** Holds if this node is inside a macro expansion. */
predicate isInMacroExpansion() {
this = any(MacroCall mc).getExpanded()
this = any(MacroCall mc).getMacroCallExpansion()
or
this.getParentNode().isInMacroExpansion()
}
Expand Down
10 changes: 9 additions & 1 deletion 10 rust/ql/lib/codeql/rust/elements/internal/MacroItemsImpl.qll

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.