Return Either from MacroDefId::ast_id

This commit is contained in:
Jonas Schievink 2021-03-19 19:56:13 +01:00
parent 0392e63c95
commit 93aeb16eb2
4 changed files with 15 additions and 18 deletions

View file

@ -6,7 +6,7 @@ use hir_def::{
src::{HasChildSource, HasSource as _}, src::{HasChildSource, HasSource as _},
Lookup, VariantId, Lookup, VariantId,
}; };
use hir_expand::{InFile, MacroDefKind}; use hir_expand::InFile;
use syntax::ast; use syntax::ast;
use crate::{ use crate::{
@ -113,15 +113,10 @@ impl HasSource for TypeAlias {
impl HasSource for MacroDef { impl HasSource for MacroDef {
type Ast = Either<ast::Macro, ast::Fn>; type Ast = Either<ast::Macro, ast::Fn>;
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> { fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
Some(match &self.id.kind { Some(self.id.ast_id().either(
MacroDefKind::Declarative(id) |id| id.with_value(Either::Left(id.to_node(db.upcast()))),
| MacroDefKind::BuiltIn(_, id) |id| id.with_value(Either::Right(id.to_node(db.upcast()))),
| MacroDefKind::BuiltInDerive(_, id) ))
| MacroDefKind::BuiltInEager(_, id) => {
id.with_value(Either::Left(id.to_node(db.upcast())))
}
MacroDefKind::ProcMacro(_, id) => id.map(|_| Either::Right(id.to_node(db.upcast()))),
})
} }
} }
impl HasSource for Impl { impl HasSource for Impl {

View file

@ -208,9 +208,10 @@ impl Attrs {
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db), AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
}, },
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::MacroDefId(it) => { AttrDefId::MacroDefId(it) => it
it.ast_id().map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) .ast_id()
} .left()
.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)),
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db), AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),

View file

@ -145,7 +145,7 @@ fn make_hygiene_info(
) -> Option<HygieneInfo> { ) -> Option<HygieneInfo> {
let arg_tt = loc.kind.arg(db)?; let arg_tt = loc.kind.arg(db)?;
let def_offset = loc.def.ast_id().and_then(|id| { let def_offset = loc.def.ast_id().left().and_then(|id| {
let def_tt = match id.to_node(db) { let def_tt = match id.to_node(db) {
ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
ast::Macro::MacroDef(_) => return None, ast::Macro::MacroDef(_) => return None,

View file

@ -15,6 +15,7 @@ pub mod proc_macro;
pub mod quote; pub mod quote;
pub mod eager; pub mod eager;
use either::Either;
pub use mbe::{ExpandError, ExpandResult}; pub use mbe::{ExpandError, ExpandResult};
use std::hash::Hash; use std::hash::Hash;
@ -143,7 +144,7 @@ impl HirFileId {
let arg_tt = loc.kind.arg(db)?; let arg_tt = loc.kind.arg(db)?;
let def = loc.def.ast_id().and_then(|id| { let def = loc.def.ast_id().left().and_then(|id| {
let def_tt = match id.to_node(db) { let def_tt = match id.to_node(db) {
ast::Macro::MacroRules(mac) => mac.token_tree()?, ast::Macro::MacroRules(mac) => mac.token_tree()?,
ast::Macro::MacroDef(_) => return None, ast::Macro::MacroDef(_) => return None,
@ -239,15 +240,15 @@ impl MacroDefId {
db.intern_macro(MacroCallLoc { def: self, krate, kind }) db.intern_macro(MacroCallLoc { def: self, krate, kind })
} }
pub fn ast_id(&self) -> Option<AstId<ast::Macro>> { pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
let id = match &self.kind { let id = match &self.kind {
MacroDefKind::Declarative(id) => id, MacroDefKind::Declarative(id) => id,
MacroDefKind::BuiltIn(_, id) => id, MacroDefKind::BuiltIn(_, id) => id,
MacroDefKind::BuiltInDerive(_, id) => id, MacroDefKind::BuiltInDerive(_, id) => id,
MacroDefKind::BuiltInEager(_, id) => id, MacroDefKind::BuiltInEager(_, id) => id,
MacroDefKind::ProcMacro(..) => return None, MacroDefKind::ProcMacro(_, id) => return Either::Right(*id),
}; };
Some(*id) Either::Left(*id)
} }
} }