From b84efbaacfc980ba167edc145aa7ca5d738448ff Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 18 Mar 2021 15:37:14 +0100 Subject: Make MacroDefId's `AstId` mandatory when possible --- crates/hir_expand/src/builtin_derive.rs | 6 ++---- crates/hir_expand/src/builtin_macro.rs | 13 +++++-------- crates/hir_expand/src/db.rs | 10 +++++----- crates/hir_expand/src/eager.rs | 10 +++++----- crates/hir_expand/src/hygiene.rs | 10 +++++----- crates/hir_expand/src/lib.rs | 24 +++++++++++++++++------- 6 files changed, 39 insertions(+), 34 deletions(-) (limited to 'crates/hir_expand') diff --git a/crates/hir_expand/src/builtin_derive.rs b/crates/hir_expand/src/builtin_derive.rs index a8d267c30..60fd2ebdd 100644 --- a/crates/hir_expand/src/builtin_derive.rs +++ b/crates/hir_expand/src/builtin_derive.rs @@ -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), diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index fce09a9e7..8529b43b6 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs @@ -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(¯o_rules.name().unwrap().as_name()).unwrap(); + let ast_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_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(¯o_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(¯o_rules))), - kind: MacroDefKind::BuiltInEager(expander), + kind: MacroDefKind::BuiltInEager(expander, ast_id), local_inner: false, }; diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index a3070f1f9..4107d7781 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -130,8 +130,8 @@ fn ast_id_map(db: &dyn AstDatabase, file_id: HirFileId) -> Arc { fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option> { 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 { + 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()))) } diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index dc618a9ee..ddadaffd3 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs @@ -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)?; diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs index 87cad326d..e758b3c0a 100644 --- a/crates/hir_expand/src/hygiene.rs +++ b/crates/hir_expand/src/hygiene.rs @@ -145,7 +145,7 @@ fn make_hygiene_info( ) -> Option { 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), } } diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs index 7532d00b8..83e11019f 100644 --- a/crates/hir_expand/src/lib.rs +++ b/crates/hir_expand/src/lib.rs @@ -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 for MacroCallId { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroDefId { pub krate: CrateId, - pub ast_id: Option>, 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> { + 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), + BuiltIn(BuiltinFnLikeExpander, AstId), // FIXME: maybe just Builtin and rename BuiltinFnLikeExpander to BuiltinExpander - BuiltInDerive(BuiltinDeriveExpander), - BuiltInEager(EagerExpander), + BuiltInDerive(BuiltinDeriveExpander, AstId), + BuiltInEager(EagerExpander, AstId), ProcMacro(ProcMacroExpander), } -- cgit v1.2.3