Skip to content

Commit aaf08bd

Browse files
committed
Shrink ProcMacroExpander size
1 parent 9fb9ee3 commit aaf08bd

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
8787
// FIXME: a hacky way to create a Name from string.
8888
let name =
8989
tt::Ident { text: it.name.clone(), span: tt::TokenId::unspecified() };
90-
(
91-
name.as_name(),
92-
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
93-
)
90+
(name.as_name(), ProcMacroExpander::new(base_db::ProcMacroId(idx as u32)))
9491
})
9592
.collect()
9693
}
@@ -581,7 +578,7 @@ impl DefCollector<'_> {
581578
let kind = def.kind.to_basedb_kind();
582579
let (expander, kind) = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
583580
Some(&(_, expander)) => (expander, kind),
584-
None => (ProcMacroExpander::dummy(self.def_map.krate), kind),
581+
None => (ProcMacroExpander::dummy(), kind),
585582
};
586583

587584
let proc_macro_id =

crates/hir-expand/src/db.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub fn expand_speculative(
210210
let mut speculative_expansion = match loc.def.kind {
211211
MacroDefKind::ProcMacro(expander, ..) => {
212212
tt.delimiter = tt::Delimiter::unspecified();
213-
expander.expand(db, loc.krate, &tt, attr_arg.as_ref())
213+
expander.expand(db, loc.def.krate, loc.krate, &tt, attr_arg.as_ref())
214214
}
215215
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
216216
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?)
@@ -256,9 +256,9 @@ fn parse_macro_expansion(
256256
macro_file: MacroFile,
257257
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
258258
let _p = profile::span("parse_macro_expansion");
259-
let result = db.macro_expand(macro_file.macro_call_id);
259+
let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id);
260260

261-
if let Some(err) = &result.err {
261+
if let Some(err) = &err {
262262
// Note:
263263
// The final goal we would like to make all parse_macro success,
264264
// such that the following log will not call anyway.
@@ -279,9 +279,9 @@ fn parse_macro_expansion(
279279
parents
280280
);
281281
}
282-
let tt = match result.value {
282+
let tt = match value {
283283
Some(tt) => tt,
284-
None => return ExpandResult { value: None, err: result.err },
284+
None => return ExpandResult { value: None, err },
285285
};
286286

287287
let expand_to = macro_expand_to(db, macro_file.macro_call_id);
@@ -291,7 +291,7 @@ fn parse_macro_expansion(
291291

292292
let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to);
293293

294-
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: result.err }
294+
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err }
295295
}
296296

297297
fn macro_arg(
@@ -504,7 +504,7 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<t
504504
_ => None,
505505
};
506506

507-
expander.expand(db, loc.krate, &macro_arg.0, attr_arg.as_ref())
507+
expander.expand(db, loc.def.krate, loc.krate, &macro_arg.0, attr_arg.as_ref())
508508
}
509509

510510
fn hygiene_frame(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<HygieneFrame> {

crates/hir-expand/src/proc_macro.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@ use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
77

88
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
99
pub struct ProcMacroExpander {
10-
krate: CrateId,
1110
proc_macro_id: Option<ProcMacroId>,
1211
}
1312

1413
impl ProcMacroExpander {
15-
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
16-
Self { krate, proc_macro_id: Some(proc_macro_id) }
14+
pub fn new(proc_macro_id: ProcMacroId) -> Self {
15+
Self { proc_macro_id: Some(proc_macro_id) }
1716
}
1817

19-
pub fn dummy(krate: CrateId) -> Self {
20-
// FIXME: Should store the name for better errors
21-
Self { krate, proc_macro_id: None }
18+
pub fn dummy() -> Self {
19+
Self { proc_macro_id: None }
2220
}
2321

2422
pub fn is_dummy(&self) -> bool {
@@ -28,14 +26,15 @@ impl ProcMacroExpander {
2826
pub fn expand(
2927
self,
3028
db: &dyn ExpandDatabase,
29+
def_crate: CrateId,
3130
calling_crate: CrateId,
3231
tt: &tt::Subtree,
3332
attr_arg: Option<&tt::Subtree>,
3433
) -> ExpandResult<tt::Subtree> {
3534
match self.proc_macro_id {
3635
Some(id) => {
3736
let krate_graph = db.crate_graph();
38-
let proc_macros = match &krate_graph[self.krate].proc_macro {
37+
let proc_macros = match &krate_graph[def_crate].proc_macro {
3938
Ok(proc_macros) => proc_macros,
4039
Err(_) => {
4140
never!("Non-dummy expander even though there are no proc macros");
@@ -84,7 +83,7 @@ impl ProcMacroExpander {
8483
}
8584
None => ExpandResult::with_err(
8685
tt::Subtree::empty(),
87-
ExpandError::UnresolvedProcMacro(self.krate),
86+
ExpandError::UnresolvedProcMacro(def_crate),
8887
),
8988
}
9089
}

0 commit comments

Comments
 (0)