Skip to content

Commit 93aeb16

Browse files
Return Either from MacroDefId::ast_id
1 parent 0392e63 commit 93aeb16

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

crates/hir/src/has_source.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use hir_def::{
66
src::{HasChildSource, HasSource as _},
77
Lookup, VariantId,
88
};
9-
use hir_expand::{InFile, MacroDefKind};
9+
use hir_expand::InFile;
1010
use syntax::ast;
1111

1212
use crate::{
@@ -113,15 +113,10 @@ impl HasSource for TypeAlias {
113113
impl HasSource for MacroDef {
114114
type Ast = Either<ast::Macro, ast::Fn>;
115115
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
116-
Some(match &self.id.kind {
117-
MacroDefKind::Declarative(id)
118-
| MacroDefKind::BuiltIn(_, id)
119-
| MacroDefKind::BuiltInDerive(_, id)
120-
| MacroDefKind::BuiltInEager(_, id) => {
121-
id.with_value(Either::Left(id.to_node(db.upcast())))
122-
}
123-
MacroDefKind::ProcMacro(_, id) => id.map(|_| Either::Right(id.to_node(db.upcast()))),
124-
})
116+
Some(self.id.ast_id().either(
117+
|id| id.with_value(Either::Left(id.to_node(db.upcast()))),
118+
|id| id.with_value(Either::Right(id.to_node(db.upcast()))),
119+
))
125120
}
126121
}
127122
impl HasSource for Impl {

crates/hir_def/src/attr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,10 @@ impl Attrs {
208208
AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
209209
},
210210
AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
211-
AttrDefId::MacroDefId(it) => {
212-
it.ast_id().map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db))
213-
}
211+
AttrDefId::MacroDefId(it) => it
212+
.ast_id()
213+
.left()
214+
.map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)),
214215
AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
215216
AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
216217
AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),

crates/hir_expand/src/hygiene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn make_hygiene_info(
145145
) -> Option<HygieneInfo> {
146146
let arg_tt = loc.kind.arg(db)?;
147147

148-
let def_offset = loc.def.ast_id().and_then(|id| {
148+
let def_offset = loc.def.ast_id().left().and_then(|id| {
149149
let def_tt = match id.to_node(db) {
150150
ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
151151
ast::Macro::MacroDef(_) => return None,

crates/hir_expand/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod proc_macro;
1515
pub mod quote;
1616
pub mod eager;
1717

18+
use either::Either;
1819
pub use mbe::{ExpandError, ExpandResult};
1920

2021
use std::hash::Hash;
@@ -143,7 +144,7 @@ impl HirFileId {
143144

144145
let arg_tt = loc.kind.arg(db)?;
145146

146-
let def = loc.def.ast_id().and_then(|id| {
147+
let def = loc.def.ast_id().left().and_then(|id| {
147148
let def_tt = match id.to_node(db) {
148149
ast::Macro::MacroRules(mac) => mac.token_tree()?,
149150
ast::Macro::MacroDef(_) => return None,
@@ -239,15 +240,15 @@ impl MacroDefId {
239240
db.intern_macro(MacroCallLoc { def: self, krate, kind })
240241
}
241242

242-
pub fn ast_id(&self) -> Option<AstId<ast::Macro>> {
243+
pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
243244
let id = match &self.kind {
244245
MacroDefKind::Declarative(id) => id,
245246
MacroDefKind::BuiltIn(_, id) => id,
246247
MacroDefKind::BuiltInDerive(_, id) => id,
247248
MacroDefKind::BuiltInEager(_, id) => id,
248-
MacroDefKind::ProcMacro(..) => return None,
249+
MacroDefKind::ProcMacro(_, id) => return Either::Right(*id),
249250
};
250-
Some(*id)
251+
Either::Left(*id)
251252
}
252253
}
253254

0 commit comments

Comments
 (0)