diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-24 08:39:29 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-24 08:39:29 +0000 |
commit | cf47ea2877e1ab17dcd0e66de66fe45bcdcda38a (patch) | |
tree | 1ecb6de96f617c0f3759d9e352744b57a326a12a /crates/ra_ide_api/src/expand_macro.rs | |
parent | 7b6aa7c34e5650506924decfee0a77aa9bb0a480 (diff) | |
parent | 9c521f00ff118c38cbec730ffae1cee5f465f6ac (diff) |
Merge #2365
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 <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/expand_macro.rs')
-rw-r--r-- | crates/ra_ide_api/src/expand_macro.rs | 40 |
1 files changed, 31 insertions, 9 deletions
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( | |||
47 | 47 | ||
48 | for child in children.into_iter() { | 48 | for child in children.into_iter() { |
49 | let node = hir::Source::new(macro_file_id, &child); | 49 | let node = hir::Source::new(macro_file_id, &child); |
50 | let new_node = expand_macro_recur(db, source, node)?; | 50 | if let Some(new_node) = expand_macro_recur(db, source, node) { |
51 | 51 | // Replace the whole node if it is root | |
52 | // Replace the whole node if it is root | 52 | // `replace_descendants` will not replace the parent node |
53 | // `replace_descendants` will not replace the parent node | 53 | // but `SyntaxNode::descendants include itself |
54 | // but `SyntaxNode::descendants include itself | 54 | if expanded == *child.syntax() { |
55 | if expanded == *child.syntax() { | 55 | expanded = new_node; |
56 | expanded = new_node; | 56 | } else { |
57 | } else { | 57 | replaces.insert(child.syntax().clone().into(), new_node.into()); |
58 | replaces.insert(child.syntax().clone().into(), new_node.into()); | 58 | } |
59 | } | 59 | } |
60 | } | 60 | } |
61 | 61 | ||
@@ -247,4 +247,26 @@ fn some_thing() -> u32 { | |||
247 | assert_eq!(res.name, "match_ast"); | 247 | assert_eq!(res.name, "match_ast"); |
248 | assert_snapshot!(res.expansion, @r###"{}"###); | 248 | assert_snapshot!(res.expansion, @r###"{}"###); |
249 | } | 249 | } |
250 | |||
251 | #[test] | ||
252 | fn macro_expand_inner_macro_fail_to_expand() { | ||
253 | let res = check_expand_macro( | ||
254 | r#" | ||
255 | //- /lib.rs | ||
256 | macro_rules! bar { | ||
257 | (BAD) => {}; | ||
258 | } | ||
259 | macro_rules! foo { | ||
260 | () => {bar!()}; | ||
261 | } | ||
262 | |||
263 | fn main() { | ||
264 | let res = fo<|>o!(); | ||
265 | } | ||
266 | "#, | ||
267 | ); | ||
268 | |||
269 | assert_eq!(res.name, "foo"); | ||
270 | assert_snapshot!(res.expansion, @r###"bar!()"###); | ||
271 | } | ||
250 | } | 272 | } |