Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-expand/src/eager.rs')
| -rw-r--r-- | crates/hir-expand/src/eager.rs | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/crates/hir-expand/src/eager.rs b/crates/hir-expand/src/eager.rs index 0b6124ebf3..a19f58709b 100644 --- a/crates/hir-expand/src/eager.rs +++ b/crates/hir-expand/src/eager.rs @@ -20,9 +20,10 @@ //! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros> use base_db::Crate; use span::SyntaxContext; -use syntax::{AstPtr, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent, ted}; +use syntax::{ + AstPtr, Parse, SyntaxElement, SyntaxNode, TextSize, WalkEvent, syntax_editor::SyntaxEditor, +}; use syntax_bridge::DocCommentDesugarMode; -use triomphe::Arc; use crate::{ AstId, EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, @@ -59,7 +60,7 @@ pub fn expand_eager_macro_input( kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None }, ctxt: call_site, }; - let arg_id = db.intern_macro_call(loc); + let arg_id = MacroCallId::new(db, loc); #[allow(deprecated)] // builtin eager macros are never derives let (_, _, span) = db.macro_arg(arg_id); let ExpandResult { value: (arg_exp, arg_exp_map), err: parse_err } = @@ -70,7 +71,7 @@ pub fn expand_eager_macro_input( let ExpandResult { value: expanded_eager_input, err } = { eager_macro_recur( db, - &arg_exp_map, + arg_exp_map, &mut arg_map, TextSize::new(0), InFile::new(arg_id.into(), arg_exp.syntax_node()), @@ -80,7 +81,7 @@ pub fn expand_eager_macro_input( eager_callback, ) }; - let err = parse_err.or(err); + let err = parse_err.clone().or(err); if cfg!(debug_assertions) { arg_map.finish(); } @@ -92,7 +93,7 @@ pub fn expand_eager_macro_input( let mut subtree = syntax_bridge::syntax_node_to_token_tree( &expanded_eager_input, arg_map, - span, + *span, DocCommentDesugarMode::Mbe, ); @@ -104,28 +105,28 @@ pub fn expand_eager_macro_input( kind: MacroCallKind::FnLike { ast_id, expand_to, - eager: Some(Arc::new(EagerCallInfo { - arg: Arc::new(subtree), + eager: Some(Box::new(EagerCallInfo { + arg: subtree, arg_id, error: err.clone(), - span, + span: *span, })), }, ctxt: call_site, }; - ExpandResult { value: Some(db.intern_macro_call(loc)), err } + ExpandResult { value: Some(MacroCallId::new(db, loc)), err } } -fn lazy_expand( - db: &dyn ExpandDatabase, +fn lazy_expand<'db>( + db: &'db dyn ExpandDatabase, def: &MacroDefId, macro_call: &ast::MacroCall, ast_id: AstId<ast::MacroCall>, krate: Crate, call_site: SyntaxContext, eager_callback: EagerCallBackFn<'_>, -) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> { +) -> ExpandResult<(InFile<Parse<SyntaxNode>>, &'db ExpansionSpanMap)> { let expand_to = ExpandTo::from_call_site(macro_call); let id = def.make_call( db, @@ -135,7 +136,9 @@ fn lazy_expand( ); eager_callback(ast_id.map(|ast_id| (AstPtr::new(macro_call), ast_id)), id); - db.parse_macro_expansion(id).map(|parse| (InFile::new(id.into(), parse.0), parse.1)) + db.parse_macro_expansion(id) + .as_ref() + .map(|parse| (InFile::new(id.into(), parse.0.clone()), &parse.1)) } fn eager_macro_recur( @@ -149,7 +152,8 @@ fn eager_macro_recur( macro_resolver: &dyn Fn(&ModPath) -> Option<MacroDefId>, eager_callback: EagerCallBackFn<'_>, ) -> ExpandResult<Option<(SyntaxNode, TextSize)>> { - let original = curr.value.clone_for_update(); + let (editor, _) = SyntaxEditor::new(curr.value.clone()); + let original = curr.value.clone(); let mut replacements = Vec::new(); @@ -232,7 +236,7 @@ fn eager_macro_recur( syntax_node.clone_for_update(), offset + syntax_node.text_range().len(), )), - err: err.or(err2), + err: err.clone().or_else(|| err2.clone()), } } None => ExpandResult { value: None, err }, @@ -256,7 +260,7 @@ fn eager_macro_recur( // replace macro inside let ExpandResult { value, err: error } = eager_macro_recur( db, - &tm, + tm, expanded_map, offset, // FIXME: We discard parse errors here @@ -288,6 +292,7 @@ fn eager_macro_recur( } } - replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new)); + replacements.into_iter().rev().for_each(|(old, new)| editor.replace(old.syntax(), new)); + let original = editor.finish().new_root().clone(); ExpandResult { value: Some((original, offset)), err: error } } |