Unnamed repository; edit this file 'description' to name the repository.
Simplify
Lukas Wirth 2024-03-14
parent abe3177 · commit 9767156
-rw-r--r--crates/hir-def/src/lib.rs15
-rw-r--r--crates/hir-expand/src/builtin_attr_macro.rs4
-rw-r--r--crates/hir-expand/src/db.rs10
-rw-r--r--crates/hir-expand/src/eager.rs28
4 files changed, 33 insertions, 24 deletions
diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs
index ae2959dff6..977782dfaf 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 a0102f36af..64295f64dc 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<tt::Subtree> {
@@ -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 1639ce189e..40cbb6912d 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 e8ca4315ea..4524463e63 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<ast::MacroCall>,
+ macro_call: &ast::MacroCall,
+ ast_id: AstId<ast::MacroCall>,
def: MacroDefId,
call_site: Span,
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
) -> ExpandResult<Option<MacroCallId>> {
- let ast_map = db.ast_id_map(macro_call.file_id);
- let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
- let expand_to = ExpandTo::from_call_site(&macro_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<ast::MacroCall>,
+ macro_call: &ast::MacroCall,
+ ast_id: AstId<ast::MacroCall>,
krate: CrateId,
call_site: Span,
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
- let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
-
- let expand_to = ExpandTo::from_call_site(&macro_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(