mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
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 <edwin0cheng@gmail.com>
This commit is contained in:
commit
cf47ea2877
1 changed files with 31 additions and 9 deletions
|
@ -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!()"###);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue