Skip to content

Commit 7a378f9

Browse files
committed
Rust: improve handling of macro expansion
1 parent 289fa57 commit 7a378f9

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

rust/extractor/src/translate/base.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use ra_ap_syntax::{
2626
macro_rules! pre_emit {
2727
(Item, $self:ident, $node:ident) => {
2828
if let Some(label) = $self.prepare_item_expansion(&$node.clone().into()) {
29-
return Some(label);
29+
return Some(label.into());
3030
}
3131
};
3232
($($_:tt)*) => {};
@@ -694,10 +694,21 @@ impl<'a> Translator<'a> {
694694
}
695695
}
696696

697+
fn is_attribute_macro_target(&self, node: &ast::Item) -> bool {
698+
// rust-analyzer considers as an `attr_macro_call` also a plain macro call, but we want to
699+
// process that differently (in `extract_macro_call_expanded`)
700+
!matches!(node, ast::Item::MacroCall(_))
701+
&& self.semantics.is_some_and(|semantics| {
702+
let file = semantics.hir_file_for(node.syntax());
703+
let node = InFile::new(file, node);
704+
semantics.is_attr_macro_call(node)
705+
})
706+
}
707+
697708
pub(crate) fn prepare_item_expansion(
698709
&mut self,
699710
node: &ast::Item,
700-
) -> Option<Label<generated::Item>> {
711+
) -> Option<Label<generated::MacroCall>> {
701712
if self.source_kind == SourceKind::Library {
702713
// if the item expands via an attribute macro, we want to only emit the expansion
703714
if let Some(expanded) = self.emit_attribute_macro_expansion(node) {
@@ -714,13 +725,10 @@ impl<'a> Translator<'a> {
714725
expanded.into(),
715726
&mut self.trap.writer,
716727
);
717-
return Some(label.into());
728+
return Some(label);
718729
}
719730
}
720-
let semantics = self.semantics.as_ref()?;
721-
let file = semantics.hir_file_for(node.syntax());
722-
let node = InFile::new(file, node);
723-
if semantics.is_attr_macro_call(node) {
731+
if self.is_attribute_macro_target(node) {
724732
self.macro_context_depth += 1;
725733
}
726734
None
@@ -730,10 +738,7 @@ impl<'a> Translator<'a> {
730738
&mut self,
731739
node: &ast::Item,
732740
) -> Option<Label<generated::MacroItems>> {
733-
let semantics = self.semantics?;
734-
let file = semantics.hir_file_for(node.syntax());
735-
let infile_node = InFile::new(file, node);
736-
if !semantics.is_attr_macro_call(infile_node) {
741+
if !self.is_attribute_macro_target(node) {
737742
return None;
738743
}
739744
self.macro_context_depth -= 1;
@@ -743,7 +748,7 @@ impl<'a> Translator<'a> {
743748
}
744749
let ExpandResult {
745750
value: expanded, ..
746-
} = semantics.expand_attr_macro(node)?;
751+
} = self.semantics.and_then(|s| s.expand_attr_macro(node))?;
747752
self.emit_macro_expansion_parse_errors(node, &expanded);
748753
let macro_items = ast::MacroItems::cast(expanded).or_else(|| {
749754
let message = "attribute macro expansion cannot be cast to MacroItems".to_owned();

rust/ql/test/extractor-tests/macro-expansion/PrintAst.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,17 +579,17 @@ macro_expansion.rs:
579579
# 43| getItem(8): [Impl] impl S { ... }
580580
# 43| getAssocItemList(): [AssocItemList] AssocItemList
581581
# 44| getAssocItem(0): [MacroCall] def_x!...
582-
# 44| getAttributeMacroExpansion(): [MacroItems] MacroItems
582+
# 44| getPath(): [Path] def_x
583+
# 44| getSegment(): [PathSegment] def_x
584+
# 44| getIdentifier(): [NameRef] def_x
585+
# 44| getTokenTree(): [TokenTree] TokenTree
586+
# 44| getMacroCallExpansion(): [MacroItems] MacroItems
583587
# 44| getItem(0): [Function] fn x
584588
# 44| getParamList(): [ParamList] ParamList
585589
# 44| getBody(): [BlockExpr] { ... }
586590
# 44| getStmtList(): [StmtList] StmtList
587591
# 44| getName(): [Name] x
588592
# 44| getVisibility(): [Visibility] Visibility
589-
# 44| getPath(): [Path] def_x
590-
# 44| getSegment(): [PathSegment] def_x
591-
# 44| getIdentifier(): [NameRef] def_x
592-
# 44| getTokenTree(): [TokenTree] TokenTree
593593
# 43| getSelfTy(): [PathTypeRepr] S
594594
# 43| getPath(): [Path] S
595595
# 43| getSegment(): [PathSegment] S

rust/ql/test/extractor-tests/macro-expansion/test.expected

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ attribute_macros
1414
| macro_expansion.rs:31:5:34:5 | fn bzz | 0 | macro_expansion.rs:32:5:33:17 | fn bzz_0 |
1515
| macro_expansion.rs:31:5:34:5 | fn bzz | 1 | macro_expansion.rs:32:5:33:17 | fn bzz_1 |
1616
| macro_expansion.rs:31:5:34:5 | fn bzz | 2 | macro_expansion.rs:32:5:33:17 | fn bzz_2 |
17-
| macro_expansion.rs:44:5:44:13 | def_x!... | 0 | macro_expansion.rs:44:5:44:13 | fn x |
1817
macro_calls
1918
| included/included.rs:2:9:2:39 | concat!... | included/included.rs:2:17:2:38 | "Hello world!" |
2019
| macro_expansion.rs:5:9:5:34 | concat!... | macro_expansion.rs:5:17:5:34 | "Hello world!" |
@@ -28,6 +27,7 @@ macro_calls
2827
| macro_expansion.rs:33:9:33:15 | hello!... | macro_expansion.rs:31:5:31:16 | MacroBlockExpr |
2928
| macro_expansion.rs:33:9:33:15 | hello!... | macro_expansion.rs:31:5:31:16 | MacroBlockExpr |
3029
| macro_expansion.rs:33:9:33:15 | hello!... | macro_expansion.rs:31:5:31:16 | MacroBlockExpr |
30+
| macro_expansion.rs:44:5:44:13 | def_x!... | macro_expansion.rs:44:5:44:13 | MacroItems |
3131
| macro_expansion.rs:53:9:53:25 | concat!... | macro_expansion.rs:53:17:53:24 | "xy" |
3232
| macro_expansion.rs:55:9:58:5 | my_macro!... | macro_expansion.rs:56:9:57:13 | MacroExpr |
3333
| macro_expansion.rs:56:9:56:31 | concat!... | macro_expansion.rs:56:17:56:30 | "<{}>" |
@@ -44,5 +44,4 @@ macro_calls
4444
unexpanded_macro_calls
4545
| macro_expansion.rs:5:9:5:35 | concat!... |
4646
| macro_expansion.rs:33:9:33:16 | hello!... |
47-
| macro_expansion.rs:44:5:44:13 | def_x!... |
4847
warnings

0 commit comments

Comments
 (0)