@@ -29,7 +29,7 @@ use base_db::CrateId;
2929use mbe:: ExpandResult ;
3030use parser:: FragmentKind ;
3131use std:: sync:: Arc ;
32- use syntax:: { algo :: SyntaxRewriter , SyntaxNode } ;
32+ use syntax:: { ted , SyntaxNode } ;
3333
3434pub struct ErrorEmitted {
3535 _private : ( ) ,
@@ -191,10 +191,10 @@ fn eager_macro_recur(
191191 macro_resolver : & dyn Fn ( ast:: Path ) -> Option < MacroDefId > ,
192192 mut diagnostic_sink : & mut dyn FnMut ( mbe:: ExpandError ) ,
193193) -> Result < SyntaxNode , ErrorEmitted > {
194- let original = curr. value . clone ( ) ;
194+ let original = curr. value . clone ( ) . clone_for_update ( ) ;
195195
196- let children = curr . value . descendants ( ) . filter_map ( ast:: MacroCall :: cast) ;
197- let mut rewriter = SyntaxRewriter :: default ( ) ;
196+ let children = original . descendants ( ) . filter_map ( ast:: MacroCall :: cast) ;
197+ let mut replacements = Vec :: new ( ) ;
198198
199199 // Collect replacement
200200 for child in children {
@@ -213,6 +213,7 @@ fn eager_macro_recur(
213213 . into ( ) ;
214214 db. parse_or_expand ( id. as_file ( ) )
215215 . expect ( "successful macro expansion should be parseable" )
216+ . clone_for_update ( )
216217 }
217218 MacroDefKind :: Declarative ( _)
218219 | MacroDefKind :: BuiltIn ( ..)
@@ -226,15 +227,14 @@ fn eager_macro_recur(
226227 }
227228 } ;
228229
229- // check if the whole original sytnax is replaced
230- // Note that SyntaxRewriter cannot replace the root node itself
230+ // check if the whole original syntax is replaced
231231 if child. syntax ( ) == & original {
232232 return Ok ( insert) ;
233233 }
234234
235- rewriter . replace ( child . syntax ( ) , & insert) ;
235+ replacements . push ( ( child , insert) ) ;
236236 }
237237
238- let res = rewriter . rewrite ( & original ) ;
239- Ok ( res )
238+ replacements . into_iter ( ) . rev ( ) . for_each ( | ( old , new ) | ted :: replace ( old . syntax ( ) , new ) ) ;
239+ Ok ( original )
240240}
0 commit comments