Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9767156

Browse files
committed
Simplify
1 parent abe3177 commit 9767156

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

crates/hir-def/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,12 +1403,15 @@ fn macro_call_as_call_id_with_eager(
14031403
resolver(call.path.clone()).ok_or_else(|| UnresolvedMacro { path: call.path.clone() })?;
14041404

14051405
let res = match def.kind {
1406-
MacroDefKind::BuiltInEager(..) => {
1407-
let macro_call = InFile::new(call.ast_id.file_id, call.ast_id.to_node(db));
1408-
expand_eager_macro_input(db, krate, macro_call, def, call_site, &|path| {
1409-
eager_resolver(path).filter(MacroDefId::is_fn_like)
1410-
})
1411-
}
1406+
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(
1407+
db,
1408+
krate,
1409+
&call.ast_id.to_node(db),
1410+
call.ast_id,
1411+
def,
1412+
call_site,
1413+
&|path| eager_resolver(path).filter(MacroDefId::is_fn_like),
1414+
),
14121415
_ if def.is_fn_like() => ExpandResult {
14131416
value: Some(def.make_call(
14141417
db,

crates/hir-expand/src/builtin_attr_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ fn derive_expand(
117117
}
118118

119119
pub fn pseudo_derive_attr_expansion(
120-
tt: &tt::Subtree,
120+
_: &tt::Subtree,
121121
args: &tt::Subtree,
122122
call_site: Span,
123123
) -> ExpandResult<tt::Subtree> {
@@ -141,7 +141,7 @@ pub fn pseudo_derive_attr_expansion(
141141
token_trees.push(mk_leaf(']'));
142142
}
143143
ExpandResult::ok(tt::Subtree {
144-
delimiter: tt.delimiter,
144+
delimiter: args.delimiter,
145145
token_trees: token_trees.into_boxed_slice(),
146146
})
147147
}

crates/hir-expand/src/db.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ pub fn expand_speculative(
146146
mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
147147
SyntaxFixupUndoInfo::NONE,
148148
),
149+
MacroCallKind::Attr { .. } if loc.def.is_attribute_derive() => (
150+
mbe::syntax_node_to_token_tree(speculative_args, span_map, loc.call_site),
151+
SyntaxFixupUndoInfo::NONE,
152+
),
149153
MacroCallKind::Derive { derive_attr_index: index, .. }
150154
| MacroCallKind::Attr { invoc_attr_index: index, .. } => {
151155
let censor = if let MacroCallKind::Derive { .. } = loc.kind {
@@ -406,7 +410,11 @@ fn macro_arg(
406410
);
407411
}
408412

409-
let tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site);
413+
let mut tt = mbe::syntax_node_to_token_tree(tt.syntax(), map.as_ref(), loc.call_site);
414+
if loc.def.is_proc_macro() {
415+
// proc macros expect their inputs without parentheses, MBEs expect it with them included
416+
tt.delimiter.kind = tt::DelimiterKind::Invisible;
417+
}
410418
let val = (Arc::new(tt), SyntaxFixupUndoInfo::NONE);
411419
return if matches!(loc.def.kind, MacroDefKind::BuiltInEager(..)) {
412420
match parse.errors() {

crates/hir-expand/src/eager.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ use crate::{
2727
ast::{self, AstNode},
2828
db::ExpandDatabase,
2929
mod_path::ModPath,
30-
EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern,
30+
AstId, EagerCallInfo, ExpandError, ExpandResult, ExpandTo, ExpansionSpanMap, InFile, Intern,
3131
MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
3232
};
3333

3434
pub fn expand_eager_macro_input(
3535
db: &dyn ExpandDatabase,
3636
krate: CrateId,
37-
macro_call: InFile<ast::MacroCall>,
37+
macro_call: &ast::MacroCall,
38+
ast_id: AstId<ast::MacroCall>,
3839
def: MacroDefId,
3940
call_site: Span,
4041
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
4142
) -> ExpandResult<Option<MacroCallId>> {
42-
let ast_map = db.ast_id_map(macro_call.file_id);
43-
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
44-
let expand_to = ExpandTo::from_call_site(&macro_call.value);
43+
let expand_to = ExpandTo::from_call_site(macro_call);
4544

4645
// Note:
4746
// When `lazy_expand` is called, its *parent* file must already exist.
@@ -50,7 +49,7 @@ pub fn expand_eager_macro_input(
5049
let arg_id = MacroCallLoc {
5150
def,
5251
krate,
53-
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr, eager: None },
52+
kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None },
5453
call_site,
5554
}
5655
.intern(db);
@@ -87,9 +86,8 @@ pub fn expand_eager_macro_input(
8786
let loc = MacroCallLoc {
8887
def,
8988
krate,
90-
9189
kind: MacroCallKind::FnLike {
92-
ast_id: call_id,
90+
ast_id,
9391
expand_to,
9492
eager: Some(Arc::new(EagerCallInfo {
9593
arg: Arc::new(subtree),
@@ -106,14 +104,12 @@ pub fn expand_eager_macro_input(
106104
fn lazy_expand(
107105
db: &dyn ExpandDatabase,
108106
def: &MacroDefId,
109-
macro_call: InFile<ast::MacroCall>,
107+
macro_call: &ast::MacroCall,
108+
ast_id: AstId<ast::MacroCall>,
110109
krate: CrateId,
111110
call_site: Span,
112111
) -> ExpandResult<(InFile<Parse<SyntaxNode>>, Arc<ExpansionSpanMap>)> {
113-
let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
114-
115-
let expand_to = ExpandTo::from_call_site(&macro_call.value);
116-
let ast_id = macro_call.with_value(ast_id);
112+
let expand_to = ExpandTo::from_call_site(macro_call);
117113
let id = def.make_call(
118114
db,
119115
krate,
@@ -183,12 +179,14 @@ fn eager_macro_recur(
183179
continue;
184180
}
185181
};
182+
let ast_id = db.ast_id_map(curr.file_id).ast_id(&call);
186183
let ExpandResult { value, err } = match def.kind {
187184
MacroDefKind::BuiltInEager(..) => {
188185
let ExpandResult { value, err } = expand_eager_macro_input(
189186
db,
190187
krate,
191-
curr.with_value(call.clone()),
188+
&call,
189+
curr.with_value(ast_id),
192190
def,
193191
call_site,
194192
macro_resolver,
@@ -218,7 +216,7 @@ fn eager_macro_recur(
218216
| MacroDefKind::BuiltInDerive(..)
219217
| MacroDefKind::ProcMacro(..) => {
220218
let ExpandResult { value: (parse, tm), err } =
221-
lazy_expand(db, &def, curr.with_value(call.clone()), krate, call_site);
219+
lazy_expand(db, &def, &call, curr.with_value(ast_id), krate, call_site);
222220

223221
// replace macro inside
224222
let ExpandResult { value, err: error } = eager_macro_recur(

0 commit comments

Comments
 (0)