From 9c521f00ff118c38cbec730ffae1cee5f465f6ac Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 23 Nov 2019 10:33:14 +0800 Subject: Improve fail case in expand_macro --- crates/ra_ide_api/src/expand_macro.rs | 40 +++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/expand_macro.rs b/crates/ra_ide_api/src/expand_macro.rs index 673301b10..0b540b8cd 100644 --- a/crates/ra_ide_api/src/expand_macro.rs +++ b/crates/ra_ide_api/src/expand_macro.rs @@ -47,15 +47,15 @@ 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)?; - - // 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() { - expanded = new_node; - } else { - replaces.insert(child.syntax().clone().into(), new_node.into()); + 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 + if expanded == *child.syntax() { + expanded = new_node; + } else { + replaces.insert(child.syntax().clone().into(), new_node.into()); + } } } @@ -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!()"###); + } } -- cgit v1.2.3