Skip to content
Merged
Changes from all commits
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
34 changes: 32 additions & 2 deletions crates/ra_ide_api/src/expand_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn expand_macro_recur(
let analyzer = hir::SourceAnalyzer::new(db, source, None);
let expansion = analyzer.expand(db, macro_call)?;
let macro_file_id = expansion.file_id();
let expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?;
let mut expanded: SyntaxNode = db.parse_or_expand(macro_file_id)?;

let children = expanded.descendants().filter_map(ast::MacroCall::cast);
let mut replaces = FxHashMap::default();
Expand All @@ -49,7 +49,14 @@ fn expand_macro_recur(
let node = hir::Source::new(macro_file_id, &child);
let new_node = expand_macro_recur(db, source, node)?;

replaces.insert(child.syntax().clone().into(), new_node.into());
// Replace the whole node if it is root
// `replace_descendants` will not replace the parent node
// but `SyntaxNode::descendants include itself
if expanded == *child.syntax() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or should we fix replace_descendants instead ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I think this is a better fix

expanded = new_node;
} else {
replaces.insert(child.syntax().clone().into(), new_node.into());
}
}

Some(replace_descendants(&expanded, &replaces))
Expand Down Expand Up @@ -217,4 +224,27 @@ fn some_thing() -> u32 {
}
"###);
}

#[test]
fn macro_expand_match_ast_inside_let_statement() {
let res = check_expand_macro(
r#"
//- /lib.rs
macro_rules! match_ast {
(match $node:ident { $($tt:tt)* }) => { match_ast!(match ($node) { $($tt)* }) };
(match ($node:expr) {}) => {{}};
}

fn main() {
let p = f(|it| {
let res = mat<|>ch_ast! { match c {}};
Some(res)
})?;
}
"#,
);

assert_eq!(res.name, "match_ast");
assert_snapshot!(res.expansion, @r###"{}"###);
}
}