Shrink ProcMacroExpander size

This commit is contained in:
Lukas Wirth 2023-03-13 16:49:32 +01:00
parent 9fb9ee3b6a
commit aaf08bdcc5
3 changed files with 16 additions and 20 deletions

View file

@ -87,10 +87,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, mut def_map: DefMap, tree_id: T
// FIXME: a hacky way to create a Name from string.
let name =
tt::Ident { text: it.name.clone(), span: tt::TokenId::unspecified() };
(
name.as_name(),
ProcMacroExpander::new(def_map.krate, base_db::ProcMacroId(idx as u32)),
)
(name.as_name(), ProcMacroExpander::new(base_db::ProcMacroId(idx as u32)))
})
.collect()
}
@ -581,7 +578,7 @@ impl DefCollector<'_> {
let kind = def.kind.to_basedb_kind();
let (expander, kind) = match self.proc_macros.iter().find(|(n, _)| n == &def.name) {
Some(&(_, expander)) => (expander, kind),
None => (ProcMacroExpander::dummy(self.def_map.krate), kind),
None => (ProcMacroExpander::dummy(), kind),
};
let proc_macro_id =

View file

@ -210,7 +210,7 @@ pub fn expand_speculative(
let mut speculative_expansion = match loc.def.kind {
MacroDefKind::ProcMacro(expander, ..) => {
tt.delimiter = tt::Delimiter::unspecified();
expander.expand(db, loc.krate, &tt, attr_arg.as_ref())
expander.expand(db, loc.def.krate, loc.krate, &tt, attr_arg.as_ref())
}
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?)
@ -256,9 +256,9 @@ fn parse_macro_expansion(
macro_file: MacroFile,
) -> ExpandResult<Option<(Parse<SyntaxNode>, Arc<mbe::TokenMap>)>> {
let _p = profile::span("parse_macro_expansion");
let result = db.macro_expand(macro_file.macro_call_id);
let mbe::ValueResult { value, err } = db.macro_expand(macro_file.macro_call_id);
if let Some(err) = &result.err {
if let Some(err) = &err {
// Note:
// The final goal we would like to make all parse_macro success,
// such that the following log will not call anyway.
@ -279,9 +279,9 @@ fn parse_macro_expansion(
parents
);
}
let tt = match result.value {
let tt = match value {
Some(tt) => tt,
None => return ExpandResult { value: None, err: result.err },
None => return ExpandResult { value: None, err },
};
let expand_to = macro_expand_to(db, macro_file.macro_call_id);
@ -291,7 +291,7 @@ fn parse_macro_expansion(
let (parse, rev_token_map) = token_tree_to_syntax_node(&tt, expand_to);
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err: result.err }
ExpandResult { value: Some((parse, Arc::new(rev_token_map))), err }
}
fn macro_arg(
@ -504,7 +504,7 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<t
_ => None,
};
expander.expand(db, loc.krate, &macro_arg.0, attr_arg.as_ref())
expander.expand(db, loc.def.krate, loc.krate, &macro_arg.0, attr_arg.as_ref())
}
fn hygiene_frame(db: &dyn ExpandDatabase, file_id: HirFileId) -> Arc<HygieneFrame> {

View file

@ -7,18 +7,16 @@ use crate::{db::ExpandDatabase, tt, ExpandError, ExpandResult};
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ProcMacroExpander {
krate: CrateId,
proc_macro_id: Option<ProcMacroId>,
}
impl ProcMacroExpander {
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
Self { krate, proc_macro_id: Some(proc_macro_id) }
pub fn new(proc_macro_id: ProcMacroId) -> Self {
Self { proc_macro_id: Some(proc_macro_id) }
}
pub fn dummy(krate: CrateId) -> Self {
// FIXME: Should store the name for better errors
Self { krate, proc_macro_id: None }
pub fn dummy() -> Self {
Self { proc_macro_id: None }
}
pub fn is_dummy(&self) -> bool {
@ -28,6 +26,7 @@ impl ProcMacroExpander {
pub fn expand(
self,
db: &dyn ExpandDatabase,
def_crate: CrateId,
calling_crate: CrateId,
tt: &tt::Subtree,
attr_arg: Option<&tt::Subtree>,
@ -35,7 +34,7 @@ impl ProcMacroExpander {
match self.proc_macro_id {
Some(id) => {
let krate_graph = db.crate_graph();
let proc_macros = match &krate_graph[self.krate].proc_macro {
let proc_macros = match &krate_graph[def_crate].proc_macro {
Ok(proc_macros) => proc_macros,
Err(_) => {
never!("Non-dummy expander even though there are no proc macros");
@ -84,7 +83,7 @@ impl ProcMacroExpander {
}
None => ExpandResult::with_err(
tt::Subtree::empty(),
ExpandError::UnresolvedProcMacro(self.krate),
ExpandError::UnresolvedProcMacro(def_crate),
),
}
}