diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index ae2959dff62..977782dfaf3 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -1403,12 +1403,15 @@ fn macro_call_as_call_id_with_eager( resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?; let res = match def.kind { - MacroDefKind::BuiltInEager(..) => { - let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db)); - expand_eager_macro_input(db, krate, macro_call, def, call_site, &|path| { - eager_resolver(path).filter(MacroDefId::is_fn_like) - }) - } + MacroDefKind::BuiltInEager(..) => expand_eager_macro_input( + db, + krate, + &call.ast_id.to_node(db), + call.ast_id, + def, + call_site, + &|path| eager_resolver(path).filter(MacroDefId::is_fn_like), + ), _ if def.is_fn_like() => ExpandResult { value: Some(def.make_call( db, diff --git a/crates/hir-expand/src/builtin_attr_macro.rs b/crates/hir-expand/src/builtin_attr_macro.rs index a0102f36aff..64295f64dcd 100644 --- a/crates/hir-expand/src/builtin_attr_macro.rs +++ b/crates/hir-expand/src/builtin_attr_macro.rs @@ -117,7 +117,7 @@ fn derive_expand( } pub fn pseudo_derive_attr_expansion( - tt: &tt::Subtree, + _: &tt::Subtree, args: &tt::Subtree, call_site: Span, ) -> ExpandResult { @@ -141,7 +141,7 @@ pub fn pseudo_derive_attr_expansion( token_trees.push(mk_leaf(']')); } ExpandResult::ok(tt::Subtree { - delimiter: tt.delimiter, + delimiter: args.delimiter, token_trees: token_trees.into_boxed_slice(), }) } diff --git a/crates/hir-expand/src/db.rs b/crates/hir-expand/src/db.rs index 1639ce189e0..40cbb6912db 100644 --- a/crates/hir-expand/src/db.rs +++ b/crates/hir-expand/src/db.rs @@ -146,6 +146,10 @@ pub fn expand_speculative( mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site), SyntaxFixupUndoInfo::NONE, ), + MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => ( + mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site), + SyntaxFixupUndoInfo::NONE, + ), MacroCallKind::Derive { derive_attr_index: index, .. } | MacroCallKind::Attr { invoc_attr_index: index, .. } => { let censor = if let MacroCallKind::Derive { .. } = loc.kind { @@ -406,7 +410,11 @@ fn macro_arg( ); } - let tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site); + let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site); + if loc.def.is_proc_macro() { + // proc macros expect their inputs without parentheses, MBEs expect it with them included + tt.delimiter.kind = tt::DelimiterKind::Invisible; + } let val = (Arc::new(tt), SyntaxFixupUndoInfo::NONE); return if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) { match parse.errors() { diff --git a/crates/hir-expand/src/eager.rs b/crates/hir-expand/src/eager.rs index e8ca4315eaf..4524463e63e 100644 --- a/crates/hir-expand/src/eager.rs +++ b/crates/hir-expand/src/eager.rs @@ -27,21 +27,20 @@ use crate::{ ast::{self, AstNode}, db::ExpandDatabase, mod_path::ModPath, - EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern, + AstId, EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern, MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind, }; pub fn expand_eager_macro_input( db: &dyn ExpandDatabase, krate: CrateId, - macro_call: InFile, + macro_call: &ast::MacroCall, + ast_id: AstId, def: MacroDefId, call_site: Span, resolver: &dyn Fn(ModPath) -> Option, ) -> ExpandResult> { - let ast_map = db.ast_id_map(macro_call.file_id); - let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(¯o_call.value)); - let expand_to = ExpandTo::from_call_site(¯o_call.value); + let expand_to = ExpandTo::from_call_site(macro_call); // Note: // When `lazy_expand` is called, its *parent* file must already exist. @@ -50,7 +49,7 @@ pub fn expand_eager_macro_input( let arg_id = MacroCallLoc { def, krate, - kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr, eager: None }, + kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None }, call_site, } .intern(db); @@ -87,9 +86,8 @@ pub fn expand_eager_macro_input( let loc = MacroCallLoc { def, krate, - kind: MacroCallKind::FnLike { - ast_id: call_id, + ast_id, expand_to, eager: Some(Arc::new(EagerCallInfo { arg: Arc::new(subtree), @@ -106,14 +104,12 @@ pub fn expand_eager_macro_input( fn lazy_expand( db: &dyn ExpandDatabase, def: &MacroDefId, - macro_call: InFile, + macro_call: &ast::MacroCall, + ast_id: AstId, krate: CrateId, call_site: Span, ) -> ExpandResult<(InFile>, Arc)> { - let ast_id = db.ast_id_map(macro_call.file_id).ast_id(¯o_call.value); - - let expand_to = ExpandTo::from_call_site(¯o_call.value); - let ast_id = macro_call.with_value(ast_id); + let expand_to = ExpandTo::from_call_site(macro_call); let id = def.make_call( db, krate, @@ -183,12 +179,14 @@ fn eager_macro_recur( continue; } }; + let ast_id = db.ast_id_map(curr.file_id).ast_id(&call); let ExpandResult { value, err } = match def.kind { MacroDefKind::BuiltInEager(..) => { let ExpandResult { value, err } = expand_eager_macro_input( db, krate, - curr.with_value(call.clone()), + &call, + curr.with_value(ast_id), def, call_site, macro_resolver, @@ -218,7 +216,7 @@ fn eager_macro_recur( | MacroDefKind::BuiltInDerive(..) | MacroDefKind::ProcMacro(..) => { let ExpandResult { value: (parse, tm), err } = - lazy_expand(db, &def, curr.with_value(call.clone()), krate, call_site); + lazy_expand(db, &def, &call, curr.with_value(ast_id), krate, call_site); // replace macro inside let ExpandResult { value, err: error } = eager_macro_recur(