aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-19 18:56:38 +0000
committerGitHub <[email protected]>2021-03-19 18:56:38 +0000
commitfc21640a65b5caef8dbbc9e85e9616b843847fb4 (patch)
tree6f4808ef6a79fa816d6b03d18234f27a0ed59c42 /crates
parent0392e63c95736e988bda0fc62261ef48e89b3a31 (diff)
parent93aeb16eb21709de38ba8484fa82ed4e05ae5665 (diff)
Merge #8111
8111: Return `Either` from `MacroDefId::ast_id` r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/hir/src/has_source.rs15
-rw-r--r--crates/hir_def/src/attr.rs7
-rw-r--r--crates/hir_expand/src/hygiene.rs2
-rw-r--r--crates/hir_expand/src/lib.rs9
4 files changed, 15 insertions, 18 deletions
diff --git a/crates/hir/src/has_source.rs b/crates/hir/src/has_source.rs
index d57fad9ed..dc10a4d0f 100644
--- a/crates/hir/src/has_source.rs
+++ b/crates/hir/src/has_source.rs
@@ -6,7 +6,7 @@ use hir_def::{
6 src::{HasChildSource, HasSource as _}, 6 src::{HasChildSource, HasSource as _},
7 Lookup, VariantId, 7 Lookup, VariantId,
8}; 8};
9use hir_expand::{InFile, MacroDefKind}; 9use hir_expand::InFile;
10use syntax::ast; 10use syntax::ast;
11 11
12use crate::{ 12use crate::{
@@ -113,15 +113,10 @@ impl HasSource for TypeAlias {
113impl HasSource for MacroDef { 113impl HasSource for MacroDef {
114 type Ast = Either<ast::Macro, ast::Fn>; 114 type Ast = Either<ast::Macro, ast::Fn>;
115 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> { 115 fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
116 Some(match &self.id.kind { 116 Some(self.id.ast_id().either(
117 MacroDefKind::Declarative(id) 117 |id| id.with_value(Either::Left(id.to_node(db.upcast()))),
118 | MacroDefKind::BuiltIn(_, id) 118 |id| id.with_value(Either::Right(id.to_node(db.upcast()))),
119 | MacroDefKind::BuiltInDerive(_, id) 119 ))
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 })
125 } 120 }
126} 121}
127impl HasSource for Impl { 122impl HasSource for Impl {
diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs
index 0360fb627..beeaaf117 100644
--- a/crates/hir_def/src/attr.rs
+++ b/crates/hir_def/src/attr.rs
@@ -208,9 +208,10 @@ impl Attrs {
208 AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db), 208 AdtId::UnionId(it) => attrs_from_item_tree(it.lookup(db).id, db),
209 }, 209 },
210 AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db), 210 AttrDefId::TraitId(it) => attrs_from_item_tree(it.lookup(db).id, db),
211 AttrDefId::MacroDefId(it) => { 211 AttrDefId::MacroDefId(it) => it
212 it.ast_id().map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)) 212 .ast_id()
213 } 213 .left()
214 .map_or_else(Default::default, |ast_id| attrs_from_ast(ast_id, db)),
214 AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db), 215 AttrDefId::ImplId(it) => attrs_from_item_tree(it.lookup(db).id, db),
215 AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db), 216 AttrDefId::ConstId(it) => attrs_from_item_tree(it.lookup(db).id, db),
216 AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db), 217 AttrDefId::StaticId(it) => attrs_from_item_tree(it.lookup(db).id, db),
diff --git a/crates/hir_expand/src/hygiene.rs b/crates/hir_expand/src/hygiene.rs
index 20cda1683..0e0f7214a 100644
--- a/crates/hir_expand/src/hygiene.rs
+++ b/crates/hir_expand/src/hygiene.rs
@@ -145,7 +145,7 @@ fn make_hygiene_info(
145) -> Option<HygieneInfo> { 145) -> Option<HygieneInfo> {
146 let arg_tt = loc.kind.arg(db)?; 146 let arg_tt = loc.kind.arg(db)?;
147 147
148 let def_offset = loc.def.ast_id().and_then(|id| { 148 let def_offset = loc.def.ast_id().left().and_then(|id| {
149 let def_tt = match id.to_node(db) { 149 let def_tt = match id.to_node(db) {
150 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(), 150 ast::Macro::MacroRules(mac) => mac.token_tree()?.syntax().text_range().start(),
151 ast::Macro::MacroDef(_) => return None, 151 ast::Macro::MacroDef(_) => return None,
diff --git a/crates/hir_expand/src/lib.rs b/crates/hir_expand/src/lib.rs
index c958b0865..f49fd4fda 100644
--- a/crates/hir_expand/src/lib.rs
+++ b/crates/hir_expand/src/lib.rs
@@ -15,6 +15,7 @@ pub mod proc_macro;
15pub mod quote; 15pub mod quote;
16pub mod eager; 16pub mod eager;
17 17
18use either::Either;
18pub use mbe::{ExpandError, ExpandResult}; 19pub use mbe::{ExpandError, ExpandResult};
19 20
20use std::hash::Hash; 21use std::hash::Hash;
@@ -143,7 +144,7 @@ impl HirFileId {
143 144
144 let arg_tt = loc.kind.arg(db)?; 145 let arg_tt = loc.kind.arg(db)?;
145 146
146 let def = loc.def.ast_id().and_then(|id| { 147 let def = loc.def.ast_id().left().and_then(|id| {
147 let def_tt = match id.to_node(db) { 148 let def_tt = match id.to_node(db) {
148 ast::Macro::MacroRules(mac) => mac.token_tree()?, 149 ast::Macro::MacroRules(mac) => mac.token_tree()?,
149 ast::Macro::MacroDef(_) => return None, 150 ast::Macro::MacroDef(_) => return None,
@@ -239,15 +240,15 @@ impl MacroDefId {
239 db.intern_macro(MacroCallLoc { def: self, krate, kind }) 240 db.intern_macro(MacroCallLoc { def: self, krate, kind })
240 } 241 }
241 242
242 pub fn ast_id(&self) -> Option<AstId<ast::Macro>> { 243 pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
243 let id = match &self.kind { 244 let id = match &self.kind {
244 MacroDefKind::Declarative(id) => id, 245 MacroDefKind::Declarative(id) => id,
245 MacroDefKind::BuiltIn(_, id) => id, 246 MacroDefKind::BuiltIn(_, id) => id,
246 MacroDefKind::BuiltInDerive(_, id) => id, 247 MacroDefKind::BuiltInDerive(_, id) => id,
247 MacroDefKind::BuiltInEager(_, id) => id, 248 MacroDefKind::BuiltInEager(_, id) => id,
248 MacroDefKind::ProcMacro(..) => return None, 249 MacroDefKind::ProcMacro(_, id) => return Either::Right(*id),
249 }; 250 };
250 Some(*id) 251 Either::Left(*id)
251 } 252 }
252} 253}
253 254