2365: Make expand-macro more flexible r=matklad a=edwin0cheng

Due to lack of implementation or other types of errors, some macros do not expand correctly in the current situation. The PR attempts to make `expand-macro` more flexible in error situations by ignoring internal failed macro expansion.

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot] 2019-11-24 08:39:29 +00:00 committed by GitHub
commit cf47ea2877
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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!()"###);
}
}