Improve fail case in expand_macro

This commit is contained in:
Edwin Cheng 2019-11-23 10:33:14 +08:00
parent 5b19202e00
commit 9c521f00ff

View file

@ -47,8 +47,7 @@ fn expand_macro_recur(
for child in children.into_iter() {
let node = hir::Source::new(macro_file_id, &child);
let new_node = expand_macro_recur(db, source, node)?;
if let Some(new_node) = expand_macro_recur(db, source, node) {
// Replace the whole node if it is root
// `replace_descendants` will not replace the parent node
// but `SyntaxNode::descendants include itself
@ -58,6 +57,7 @@ fn expand_macro_recur(
replaces.insert(child.syntax().clone().into(), new_node.into());
}
}
}
Some(replace_descendants(&expanded, &replaces))
}
@ -247,4 +247,26 @@ fn some_thing() -> u32 {
assert_eq!(res.name, "match_ast");
assert_snapshot!(res.expansion, @r###"{}"###);
}
#[test]
fn macro_expand_inner_macro_fail_to_expand() {
let res = check_expand_macro(
r#"
//- /lib.rs
macro_rules! bar {
(BAD) => {};
}
macro_rules! foo {
() => {bar!()};
}
fn main() {
let res = fo<|>o!();
}
"#,
);
assert_eq!(res.name, "foo");
assert_snapshot!(res.expansion, @r###"bar!()"###);
}
}