Remove SyntaxRewriter usage in eager::eager_macro_recur

This commit is contained in:
Lukas Wirth 2021-04-19 19:28:41 +02:00
parent 3f1a220f32
commit 617cd7231c

View File

@ -29,7 +29,7 @@ use base_db::CrateId;
use mbe::ExpandResult; use mbe::ExpandResult;
use parser::FragmentKind; use parser::FragmentKind;
use std::sync::Arc; use std::sync::Arc;
use syntax::{algo::SyntaxRewriter, SyntaxNode}; use syntax::{ted, SyntaxNode};
pub struct ErrorEmitted { pub struct ErrorEmitted {
_private: (), _private: (),
@ -191,10 +191,10 @@ fn eager_macro_recur(
macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>, macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
) -> Result<SyntaxNode, ErrorEmitted> { ) -> Result<SyntaxNode, ErrorEmitted> {
let original = curr.value.clone(); let original = curr.value.clone().clone_for_update();
let children = curr.value.descendants().filter_map(ast::MacroCall::cast); let children = original.descendants().filter_map(ast::MacroCall::cast);
let mut rewriter = SyntaxRewriter::default(); let mut replacements = Vec::new();
// Collect replacement // Collect replacement
for child in children { for child in children {
@ -213,6 +213,7 @@ fn eager_macro_recur(
.into(); .into();
db.parse_or_expand(id.as_file()) db.parse_or_expand(id.as_file())
.expect("successful macro expansion should be parseable") .expect("successful macro expansion should be parseable")
.clone_for_update()
} }
MacroDefKind::Declarative(_) MacroDefKind::Declarative(_)
| MacroDefKind::BuiltIn(..) | MacroDefKind::BuiltIn(..)
@ -226,15 +227,14 @@ fn eager_macro_recur(
} }
}; };
// check if the whole original sytnax is replaced // check if the whole original syntax is replaced
// Note that SyntaxRewriter cannot replace the root node itself
if child.syntax() == &original { if child.syntax() == &original {
return Ok(insert); return Ok(insert);
} }
rewriter.replace(child.syntax(), &insert); replacements.push((child, insert));
} }
let res = rewriter.rewrite(&original); replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
Ok(res) Ok(original)
} }