Make MacroDefId's AstId mandatory when possible

This commit is contained in:
Jonas Schievink 2021-03-18 15:37:14 +01:00
parent 816bc73895
commit b84efbaacf
13 changed files with 58 additions and 55 deletions

View file

@ -61,8 +61,7 @@ pub fn find_builtin_derive(
let expander = BuiltinDeriveExpander::find_by_name(ident)?;
Some(MacroDefId {
krate,
ast_id: Some(ast_id),
kind: MacroDefKind::BuiltInDerive(expander),
kind: MacroDefKind::BuiltInDerive(expander, ast_id),
local_inner: false,
})
}
@ -314,8 +313,7 @@ $0
let loc = MacroCallLoc {
def: MacroDefId {
krate: CrateId(0),
ast_id: Some(macro_ast_id),
kind: MacroDefKind::BuiltInDerive(expander),
kind: MacroDefKind::BuiltInDerive(expander, macro_ast_id),
local_inner: false,
},
krate: CrateId(0),

View file

@ -71,14 +71,12 @@ pub fn find_builtin_macro(
match kind {
Either::Left(kind) => Some(MacroDefId {
krate,
ast_id: Some(ast_id),
kind: MacroDefKind::BuiltIn(kind),
kind: MacroDefKind::BuiltIn(kind, ast_id),
local_inner: false,
}),
Either::Right(kind) => Some(MacroDefId {
krate,
ast_id: Some(ast_id),
kind: MacroDefKind::BuiltInEager(kind),
kind: MacroDefKind::BuiltInEager(kind, ast_id),
local_inner: false,
}),
}
@ -512,6 +510,7 @@ mod tests {
let macro_call = macro_calls.pop().unwrap();
let expander = find_by_name(&macro_rules.name().unwrap().as_name()).unwrap();
let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules));
let krate = CrateId(0);
let file_id = match expander {
@ -519,8 +518,7 @@ mod tests {
// the first one should be a macro_rules
let def = MacroDefId {
krate: CrateId(0),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules))),
kind: MacroDefKind::BuiltIn(expander),
kind: MacroDefKind::BuiltIn(expander, ast_id),
local_inner: false,
};
@ -540,8 +538,7 @@ mod tests {
// the first one should be a macro_rules
let def = MacroDefId {
krate,
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_rules))),
kind: MacroDefKind::BuiltInEager(expander),
kind: MacroDefKind::BuiltInEager(expander, ast_id),
local_inner: false,
};

View file

@ -130,8 +130,8 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc<AstIdMap> {
fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>> {
match id.kind {
MacroDefKind::Declarative => {
let macro_rules = match id.ast_id?.to_node(db) {
MacroDefKind::Declarative(ast_id) => {
let macro_rules = match ast_id.to_node(db) {
syntax::ast::Macro::MacroRules(mac) => mac,
syntax::ast::Macro::MacroDef(_) => return None,
};
@ -150,13 +150,13 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander,
};
Some(Arc::new((TokenExpander::MacroRules(rules), tmap)))
}
MacroDefKind::BuiltIn(expander) => {
MacroDefKind::BuiltIn(expander, _) => {
Some(Arc::new((TokenExpander::Builtin(expander), mbe::TokenMap::default())))
}
MacroDefKind::BuiltInDerive(expander) => {
MacroDefKind::BuiltInDerive(expander, _) => {
Some(Arc::new((TokenExpander::BuiltinDerive(expander), mbe::TokenMap::default())))
}
MacroDefKind::BuiltInEager(_) => None,
MacroDefKind::BuiltInEager(..) => None,
MacroDefKind::ProcMacro(expander) => {
Some(Arc::new((TokenExpander::ProcMacro(expander), mbe::TokenMap::default())))
}

View file

@ -140,7 +140,7 @@ pub fn expand_eager_macro(
let subtree =
diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?;
if let MacroDefKind::BuiltInEager(eager) = def.kind {
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
let res = eager.expand(db, arg_id, &subtree);
let (subtree, fragment) = diagnostic_sink.expand_result_option(res)?;
@ -193,7 +193,7 @@ fn eager_macro_recur(
let def = diagnostic_sink
.option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?;
let insert = match def.kind {
MacroDefKind::BuiltInEager(_) => {
MacroDefKind::BuiltInEager(..) => {
let id: MacroCallId = expand_eager_macro(
db,
krate,
@ -206,9 +206,9 @@ fn eager_macro_recur(
db.parse_or_expand(id.as_file())
.expect("successful macro expansion should be parseable")
}
MacroDefKind::Declarative
| MacroDefKind::BuiltIn(_)
| MacroDefKind::BuiltInDerive(_)
MacroDefKind::Declarative(_)
| MacroDefKind::BuiltIn(..)
| MacroDefKind::BuiltInDerive(..)
| MacroDefKind::ProcMacro(_) => {
let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
let val = diagnostic_sink.expand_result_option(res)?;

View file

@ -145,7 +145,7 @@ fn make_hygiene_info(
) -> Option<HygieneInfo> {
let arg_tt = loc.kind.arg(db)?;
let def_offset = loc.def.ast_id.and_then(|id| {
let def_offset = loc.def.ast_id().and_then(|id| {
let def_tt = match id.to_node(db) {
ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
ast::Macro::MacroDef(_) => return None,
@ -176,12 +176,12 @@ impl HygieneFrame {
let loc = db.lookup_intern_macro(id);
let info = make_hygiene_info(db, macro_file, &loc);
match loc.def.kind {
MacroDefKind::Declarative => {
MacroDefKind::Declarative(_) => {
(info, Some(loc.def.krate), loc.def.local_inner)
}
MacroDefKind::BuiltIn(_) => (info, Some(loc.def.krate), false),
MacroDefKind::BuiltInDerive(_) => (info, None, false),
MacroDefKind::BuiltInEager(_) => (info, None, false),
MacroDefKind::BuiltIn(..) => (info, Some(loc.def.krate), false),
MacroDefKind::BuiltInDerive(..) => (info, None, false),
MacroDefKind::BuiltInEager(..) => (info, None, false),
MacroDefKind::ProcMacro(_) => (info, None, false),
}
}

View file

@ -143,7 +143,7 @@ impl HirFileId {
let arg_tt = loc.kind.arg(db)?;
let def = loc.def.ast_id.and_then(|id| {
let def = loc.def.ast_id().and_then(|id| {
let def_tt = match id.to_node(db) {
ast::Macro::MacroRules(mac) => mac.token_tree()?,
ast::Macro::MacroDef(_) => return None,
@ -180,7 +180,7 @@ impl HirFileId {
};
let loc: MacroCallLoc = db.lookup_intern_macro(lazy_id);
let item = match loc.def.kind {
MacroDefKind::BuiltInDerive(_) => loc.kind.node(db),
MacroDefKind::BuiltInDerive(..) => loc.kind.node(db),
_ => return None,
};
Some(item.with_value(ast::Item::cast(item.value.clone())?))
@ -224,7 +224,6 @@ impl From<EagerMacroId> for MacroCallId {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MacroDefId {
pub krate: CrateId,
pub ast_id: Option<AstId<ast::Macro>>,
pub kind: MacroDefKind,
pub local_inner: bool,
@ -239,15 +238,26 @@ impl MacroDefId {
) -> LazyMacroId {
db.intern_macro(MacroCallLoc { def: self, krate, kind })
}
pub fn ast_id(&self) -> Option<AstId<ast::Macro>> {
let id = match &self.kind {
MacroDefKind::Declarative(id) => id,
MacroDefKind::BuiltIn(_, id) => id,
MacroDefKind::BuiltInDerive(_, id) => id,
MacroDefKind::BuiltInEager(_, id) => id,
MacroDefKind::ProcMacro(_) => return None,
};
Some(*id)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroDefKind {
Declarative,
BuiltIn(BuiltinFnLikeExpander),
Declarative(AstId<ast::Macro>),
BuiltIn(BuiltinFnLikeExpander, AstId<ast::Macro>),
// FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander
BuiltInDerive(BuiltinDeriveExpander),
BuiltInEager(EagerExpander),
BuiltInDerive(BuiltinDeriveExpander, AstId<ast::Macro>),
BuiltInEager(EagerExpander, AstId<ast::Macro>),
ProcMacro(ProcMacroExpander),
}