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

Commit abe3177

Browse files
committed
Shrink MacroCallLoc
1 parent 87e0bbc commit abe3177

File tree

7 files changed

+69
-45
lines changed

7 files changed

+69
-45
lines changed

crates/hir-def/src/data.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ impl<'a> AssocItemCollector<'a> {
745745
self.collect_macro_items(res, &|| hir_expand::MacroCallKind::FnLike {
746746
ast_id: InFile::new(file_id, ast_id),
747747
expand_to: hir_expand::ExpandTo::Items,
748+
eager: None,
748749
});
749750
}
750751
Ok(None) => (),
@@ -754,6 +755,7 @@ impl<'a> AssocItemCollector<'a> {
754755
MacroCallKind::FnLike {
755756
ast_id: InFile::new(file_id, ast_id),
756757
expand_to,
758+
eager: None,
757759
},
758760
Clone::clone(path),
759761
));

crates/hir-def/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,10 +1410,10 @@ fn macro_call_as_call_id_with_eager(
14101410
})
14111411
}
14121412
_ if def.is_fn_like() => ExpandResult {
1413-
value: Some(def.as_lazy_macro(
1413+
value: Some(def.make_call(
14141414
db,
14151415
krate,
1416-
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to },
1416+
MacroCallKind::FnLike { ast_id: call.ast_id, expand_to, eager: None },
14171417
call_site,
14181418
)),
14191419
err: None,

crates/hir-def/src/nameres/attr_resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub(super) fn attr_macro_as_call_id(
116116
_ => None,
117117
};
118118

119-
def.as_lazy_macro(
119+
def.make_call(
120120
db.upcast(),
121121
krate,
122122
MacroCallKind::Attr {
@@ -140,7 +140,7 @@ pub(super) fn derive_macro_as_call_id(
140140
let (macro_id, def_id) = resolver(item_attr.path.clone())
141141
.filter(|(_, def_id)| def_id.is_derive())
142142
.ok_or_else(|| UnresolvedMacro { path: item_attr.path.clone() })?;
143-
let call_id = def_id.as_lazy_macro(
143+
let call_id = def_id.make_call(
144144
db.upcast(),
145145
krate,
146146
MacroCallKind::Derive {

crates/hir-def/src/nameres/collector.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,11 @@ impl DefCollector<'_> {
14511451
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
14521452
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
14531453
directive.module_id,
1454-
MacroCallKind::FnLike { ast_id: ast_id.ast_id, expand_to: *expand_to },
1454+
MacroCallKind::FnLike {
1455+
ast_id: ast_id.ast_id,
1456+
expand_to: *expand_to,
1457+
eager: None,
1458+
},
14551459
path,
14561460
));
14571461
}

crates/hir-expand/src/db.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,16 @@ pub(crate) fn parse_with_map(
332332
fn macro_arg(
333333
db: &dyn ExpandDatabase,
334334
id: MacroCallId,
335-
// FIXME: consider the following by putting fixup info into eager call info args
336-
// ) -> ValueResult<Arc<(tt::Subtree, SyntaxFixupUndoInfo)>, Arc<Box<[SyntaxError]>>> {
337335
) -> ValueResult<(Arc<tt::Subtree>, SyntaxFixupUndoInfo), Arc<Box<[SyntaxError]>>> {
338336
let loc = db.lookup_intern_macro_call(id);
339-
if let Some(EagerCallInfo { arg, .. }) = matches!(loc.def.kind, MacroDefKind::BuiltInEager(..))
340-
.then(|| loc.eager.as_deref())
341-
.flatten()
337+
338+
if let MacroCallLoc {
339+
def: MacroDefId { kind: MacroDefKind::BuiltInEager(..), .. },
340+
kind: MacroCallKind::FnLike { eager: Some(eager), .. },
341+
..
342+
} = &loc
342343
{
343-
return ValueResult::ok((arg.clone(), SyntaxFixupUndoInfo::NONE));
344+
return ValueResult::ok((eager.arg.clone(), SyntaxFixupUndoInfo::NONE));
344345
}
345346

346347
let (parse, map) = parse_with_map(db, loc.kind.file_id());
@@ -518,7 +519,7 @@ fn macro_expand(
518519
) -> ExpandResult<CowArc<tt::Subtree>> {
519520
let _p = tracing::span!(tracing::Level::INFO, "macro_expand").entered();
520521

521-
let ExpandResult { value: tt, mut err } = match loc.def.kind {
522+
let ExpandResult { value: tt, err } = match loc.def.kind {
522523
MacroDefKind::ProcMacro(..) => return db.expand_proc_macro(macro_call_id).map(CowArc::Arc),
523524
_ => {
524525
let ValueResult { value: (macro_arg, undo_info), err } = db.macro_arg(macro_call_id);
@@ -541,23 +542,34 @@ fn macro_expand(
541542
MacroDefKind::BuiltIn(it, _) => {
542543
it.expand(db, macro_call_id, arg).map_err(Into::into)
543544
}
544-
// This might look a bit odd, but we do not expand the inputs to eager macros here.
545-
// Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
546-
// That kind of expansion uses the ast id map of an eager macros input though which goes through
547-
// the HirFileId machinery. As eager macro inputs are assigned a macro file id that query
548-
// will end up going through here again, whereas we want to just want to inspect the raw input.
549-
// As such we just return the input subtree here.
550-
MacroDefKind::BuiltInEager(..) if loc.eager.is_none() => {
551-
return ExpandResult {
552-
value: CowArc::Arc(macro_arg.clone()),
553-
err: err.map(format_parse_err),
554-
};
555-
}
556545
MacroDefKind::BuiltInDerive(it, _) => {
557546
it.expand(db, macro_call_id, arg).map_err(Into::into)
558547
}
559548
MacroDefKind::BuiltInEager(it, _) => {
560-
it.expand(db, macro_call_id, arg).map_err(Into::into)
549+
// This might look a bit odd, but we do not expand the inputs to eager macros here.
550+
// Eager macros inputs are expanded, well, eagerly when we collect the macro calls.
551+
// That kind of expansion uses the ast id map of an eager macros input though which goes through
552+
// the HirFileId machinery. As eager macro inputs are assigned a macro file id that query
553+
// will end up going through here again, whereas we want to just want to inspect the raw input.
554+
// As such we just return the input subtree here.
555+
let eager = match &loc.kind {
556+
MacroCallKind::FnLike { eager: None, .. } => {
557+
return ExpandResult {
558+
value: CowArc::Arc(macro_arg.clone()),
559+
err: err.map(format_parse_err),
560+
};
561+
}
562+
MacroCallKind::FnLike { eager: Some(eager), .. } => Some(&**eager),
563+
_ => None,
564+
};
565+
566+
let mut res = it.expand(db, macro_call_id, arg).map_err(Into::into);
567+
568+
if let Some(EagerCallInfo { error, .. }) = eager {
569+
// FIXME: We should report both errors!
570+
res.err = error.clone().or(res.err);
571+
}
572+
res
561573
}
562574
MacroDefKind::BuiltInAttr(it, _) => {
563575
let mut res = it.expand(db, macro_call_id, arg);
@@ -574,11 +586,6 @@ fn macro_expand(
574586
}
575587
};
576588

577-
if let Some(EagerCallInfo { error, .. }) = loc.eager.as_deref() {
578-
// FIXME: We should report both errors!
579-
err = error.clone().or(err);
580-
}
581-
582589
// Skip checking token tree limit for include! macro call
583590
if !loc.def.is_include() {
584591
// Set a hard limit for the expanded tt

crates/hir-expand/src/eager.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ pub fn expand_eager_macro_input(
4040
resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
4141
) -> ExpandResult<Option<MacroCallId>> {
4242
let ast_map = db.ast_id_map(macro_call.file_id);
43-
// the expansion which the ast id map is built upon has no whitespace, so the offsets are wrong as macro_call is from the token tree that has whitespace!
4443
let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
4544
let expand_to = ExpandTo::from_call_site(&macro_call.value);
4645

@@ -51,8 +50,7 @@ pub fn expand_eager_macro_input(
5150
let arg_id = MacroCallLoc {
5251
def,
5352
krate,
54-
eager: None,
55-
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
53+
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr, eager: None },
5654
call_site,
5755
}
5856
.intern(db);
@@ -89,8 +87,16 @@ pub fn expand_eager_macro_input(
8987
let loc = MacroCallLoc {
9088
def,
9189
krate,
92-
eager: Some(Arc::new(EagerCallInfo { arg: Arc::new(subtree), arg_id, error: err.clone() })),
93-
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
90+
91+
kind: MacroCallKind::FnLike {
92+
ast_id: call_id,
93+
expand_to,
94+
eager: Some(Arc::new(EagerCallInfo {
95+
arg: Arc::new(subtree),
96+
arg_id,
97+
error: err.clone(),
98+
})),
99+
},
94100
call_site,
95101
};
96102

@@ -108,7 +114,12 @@ fn lazy_expand(
108114

109115
let expand_to = ExpandTo::from_call_site(&macro_call.value);
110116
let ast_id = macro_call.with_value(ast_id);
111-
let id = def.as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id, expand_to }, call_site);
117+
let id = def.make_call(
118+
db,
119+
krate,
120+
MacroCallKind::FnLike { ast_id, expand_to, eager: None },
121+
call_site,
122+
);
112123
let macro_file = id.as_macro_file();
113124

114125
db.parse_macro_expansion(macro_file)

crates/hir-expand/src/lib.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ impl fmt::Display for ExpandError {
170170
pub struct MacroCallLoc {
171171
pub def: MacroDefId,
172172
pub krate: CrateId,
173-
/// Some if this is a macro call for an eager macro. Note that this is `None`
174-
/// for the eager input macro file.
175-
// FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
176-
// leakage problems here
177-
eager: Option<Arc<EagerCallInfo>>,
178173
pub kind: MacroCallKind,
179174
// FIXME: Spans while relative to an anchor, are still rather unstable
180175
pub call_site: Span,
@@ -215,6 +210,11 @@ pub enum MacroCallKind {
215210
FnLike {
216211
ast_id: AstId<ast::MacroCall>,
217212
expand_to: ExpandTo,
213+
/// Some if this is a macro call for an eager macro. Note that this is `None`
214+
/// for the eager input macro file.
215+
// FIXME: This is being interned, subtrees can vary quickly differ just slightly causing
216+
// leakage problems here
217+
eager: Option<Arc<EagerCallInfo>>,
218218
},
219219
Derive {
220220
ast_id: AstId<ast::Adt>,
@@ -276,7 +276,7 @@ impl HirFileIdExt for HirFileId {
276276
HirFileIdRepr::MacroFile(file) => {
277277
let loc = db.lookup_intern_macro_call(file.macro_call_id);
278278
if loc.def.is_include() {
279-
if let Some(eager) = &loc.eager {
279+
if let MacroCallKind::FnLike { eager: Some(eager), .. } = &loc.kind {
280280
if let Ok(it) = builtin_fn_macro::include_input_to_file_id(
281281
db,
282282
file.macro_call_id,
@@ -406,14 +406,14 @@ impl MacroFileIdExt for MacroFileId {
406406
}
407407

408408
impl MacroDefId {
409-
pub fn as_lazy_macro(
409+
pub fn make_call(
410410
self,
411411
db: &dyn ExpandDatabase,
412412
krate: CrateId,
413413
kind: MacroCallKind,
414414
call_site: Span,
415415
) -> MacroCallId {
416-
MacroCallLoc { def: self, krate, eager: None, kind, call_site }.intern(db)
416+
MacroCallLoc { def: self, krate, kind, call_site }.intern(db)
417417
}
418418

419419
pub fn definition_range(&self, db: &dyn ExpandDatabase) -> InFile<TextRange> {
@@ -535,7 +535,7 @@ impl MacroCallLoc {
535535
macro_call_id: MacroCallId,
536536
) -> Option<FileId> {
537537
if self.def.is_include() {
538-
if let Some(eager) = &self.eager {
538+
if let MacroCallKind::FnLike { eager: Some(eager), .. } = &self.kind {
539539
if let Ok(it) =
540540
builtin_fn_macro::include_input_to_file_id(db, macro_call_id, &eager.arg)
541541
{

0 commit comments

Comments
 (0)