Skip to content

Commit d70ea75

Browse files
bors[bot]Veykril
andauthored
Merge #11663
11663: Internal: Add hir_def::MacroId, add Macro{Id} to ModuleDef{Id} r=Veykril a=Veykril With this we can now handle macros like we handle ModuleDefs making them work more like other definitions and allowing us to remove a bunch of special cases. This also enables us to track the modules these macros are defined in, instead of only recording the crate they come from. Introduces a new class of `MacroId`s (for each of the 3 macro kinds) into `hir_def`. We can't reuse `MacroDefId` as that is defined in `hir_expand` which doesn't know of modules, so now we have two different macro ids, this unfortunately requires some back and forth mapping between the two via database accesses which I hope won't be too expensive. Co-authored-by: Lukas Wirth <[email protected]>
2 parents 4924072 + 2537ad0 commit d70ea75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+975
-663
lines changed

crates/hir/src/attrs.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Attributes & documentation for hir types.
22
3-
use either::Either;
43
use hir_def::{
54
attr::{AttrsWithOwner, Documentation},
65
item_scope::ItemInNs,
@@ -9,13 +8,13 @@ use hir_def::{
98
resolver::HasResolver,
109
AttrDefId, GenericParamId, ModuleDefId,
1110
};
12-
use hir_expand::{hygiene::Hygiene, MacroDefId};
11+
use hir_expand::hygiene::Hygiene;
1312
use hir_ty::db::HirDatabase;
1413
use syntax::{ast, AstNode};
1514

1615
use crate::{
1716
Adt, AssocItem, Const, ConstParam, Enum, Field, Function, GenericParam, Impl, LifetimeParam,
18-
MacroDef, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
17+
Macro, Module, ModuleDef, Static, Struct, Trait, TypeAlias, TypeParam, Union, Variant,
1918
};
2019

2120
pub trait HasAttrs {
@@ -26,7 +25,7 @@ pub trait HasAttrs {
2625
db: &dyn HirDatabase,
2726
link: &str,
2827
ns: Option<Namespace>,
29-
) -> Option<Either<ModuleDef, MacroDef>>;
28+
) -> Option<ModuleDef>;
3029
}
3130

3231
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
@@ -47,9 +46,9 @@ macro_rules! impl_has_attrs {
4746
let def = AttrDefId::$def_id(self.into());
4847
db.attrs(def).docs()
4948
}
50-
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<Either<ModuleDef, MacroDef>> {
49+
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
5150
let def = AttrDefId::$def_id(self.into());
52-
resolve_doc_path(db, def, link, ns).map(|it| it.map_left(ModuleDef::from).map_right(MacroDef::from))
51+
resolve_doc_path(db, def, link, ns).map(ModuleDef::from)
5352
}
5453
}
5554
)*};
@@ -62,7 +61,7 @@ impl_has_attrs![
6261
(Const, ConstId),
6362
(Trait, TraitId),
6463
(TypeAlias, TypeAliasId),
65-
(MacroDef, MacroDefId),
64+
(Macro, MacroId),
6665
(Function, FunctionId),
6766
(Adt, AdtId),
6867
(Module, ModuleId),
@@ -79,7 +78,7 @@ macro_rules! impl_has_attrs_enum {
7978
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
8079
$enum::$variant(self).docs(db)
8180
}
82-
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<Either<ModuleDef, MacroDef>> {
81+
fn resolve_doc_path(self, db: &dyn HirDatabase, link: &str, ns: Option<Namespace>) -> Option<ModuleDef> {
8382
$enum::$variant(self).resolve_doc_path(db, link, ns)
8483
}
8584
}
@@ -111,7 +110,7 @@ impl HasAttrs for AssocItem {
111110
db: &dyn HirDatabase,
112111
link: &str,
113112
ns: Option<Namespace>,
114-
) -> Option<Either<ModuleDef, MacroDef>> {
113+
) -> Option<ModuleDef> {
115114
match self {
116115
AssocItem::Function(it) => it.resolve_doc_path(db, link, ns),
117116
AssocItem::Const(it) => it.resolve_doc_path(db, link, ns),
@@ -125,7 +124,7 @@ fn resolve_doc_path(
125124
def: AttrDefId,
126125
link: &str,
127126
ns: Option<Namespace>,
128-
) -> Option<Either<ModuleDefId, MacroDefId>> {
127+
) -> Option<ModuleDefId> {
129128
let resolver = match def {
130129
AttrDefId::ModuleId(it) => it.resolver(db.upcast()),
131130
AttrDefId::FieldId(it) => it.parent.resolver(db.upcast()),
@@ -138,14 +137,13 @@ fn resolve_doc_path(
138137
AttrDefId::TypeAliasId(it) => it.resolver(db.upcast()),
139138
AttrDefId::ImplId(it) => it.resolver(db.upcast()),
140139
AttrDefId::ExternBlockId(it) => it.resolver(db.upcast()),
140+
AttrDefId::MacroId(it) => it.resolver(db.upcast()),
141141
AttrDefId::GenericParamId(it) => match it {
142142
GenericParamId::TypeParamId(it) => it.parent(),
143143
GenericParamId::ConstParamId(it) => it.parent(),
144144
GenericParamId::LifetimeParamId(it) => it.parent,
145145
}
146146
.resolver(db.upcast()),
147-
// FIXME
148-
AttrDefId::MacroDefId(_) => return None,
149147
};
150148

151149
let modpath = {
@@ -167,13 +165,13 @@ fn resolve_doc_path(
167165
resolved
168166
};
169167
match ns {
170-
Some(Namespace::Types) => resolved.take_types().map(Either::Left),
171-
Some(Namespace::Values) => resolved.take_values().map(Either::Left),
172-
Some(Namespace::Macros) => resolved.take_macros().map(Either::Right),
168+
Some(Namespace::Types) => resolved.take_types(),
169+
Some(Namespace::Values) => resolved.take_values(),
170+
Some(Namespace::Macros) => resolved.take_macros().map(ModuleDefId::MacroId),
173171
None => resolved.iter_items().next().map(|it| match it {
174-
ItemInNs::Types(it) => Either::Left(it),
175-
ItemInNs::Values(it) => Either::Left(it),
176-
ItemInNs::Macros(it) => Either::Right(it),
172+
ItemInNs::Types(it) => it,
173+
ItemInNs::Values(it) => it,
174+
ItemInNs::Macros(it) => ModuleDefId::MacroId(it),
177175
}),
178176
}
179177
}

crates/hir/src/from_id.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ from_id![
4545
(hir_def::TypeParamId, crate::TypeParam),
4646
(hir_def::ConstParamId, crate::ConstParam),
4747
(hir_def::LifetimeParamId, crate::LifetimeParam),
48-
(hir_expand::MacroDefId, crate::MacroDef)
48+
(hir_def::MacroId, crate::Macro)
4949
];
5050

5151
impl From<AdtId> for Adt {
@@ -112,6 +112,7 @@ impl From<ModuleDefId> for ModuleDef {
112112
ModuleDefId::TraitId(it) => ModuleDef::Trait(it.into()),
113113
ModuleDefId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
114114
ModuleDefId::BuiltinType(it) => ModuleDef::BuiltinType(it.into()),
115+
ModuleDefId::MacroId(it) => ModuleDef::Macro(it.into()),
115116
}
116117
}
117118
}
@@ -128,6 +129,7 @@ impl From<ModuleDef> for ModuleDefId {
128129
ModuleDef::Trait(it) => ModuleDefId::TraitId(it.into()),
129130
ModuleDef::TypeAlias(it) => ModuleDefId::TypeAliasId(it.into()),
130131
ModuleDef::BuiltinType(it) => ModuleDefId::BuiltinType(it.into()),
132+
ModuleDef::Macro(it) => ModuleDefId::MacroId(it.into()),
131133
}
132134
}
133135
}

crates/hir/src/has_source.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use either::Either;
44
use hir_def::{
55
nameres::{ModuleOrigin, ModuleSource},
66
src::{HasChildSource, HasSource as _},
7-
Lookup, VariantId,
7+
Lookup, MacroId, VariantId,
88
};
99
use hir_expand::InFile;
1010
use syntax::ast;
1111

1212
use crate::{
13-
db::HirDatabase, Adt, Const, Enum, Field, FieldSource, Function, Impl, LifetimeParam, MacroDef,
13+
db::HirDatabase, Adt, Const, Enum, Field, FieldSource, Function, Impl, LifetimeParam, Macro,
1414
Module, Static, Struct, Trait, TypeAlias, TypeOrConstParam, Union, Variant,
1515
};
1616

@@ -123,13 +123,26 @@ impl HasSource for TypeAlias {
123123
Some(self.id.lookup(db.upcast()).source(db.upcast()))
124124
}
125125
}
126-
impl HasSource for MacroDef {
126+
impl HasSource for Macro {
127127
type Ast = Either<ast::Macro, ast::Fn>;
128128
fn source(self, db: &dyn HirDatabase) -> Option<InFile<Self::Ast>> {
129-
Some(self.id.ast_id().either(
130-
|id| id.with_value(Either::Left(id.to_node(db.upcast()))),
131-
|id| id.with_value(Either::Right(id.to_node(db.upcast()))),
132-
))
129+
match self.id {
130+
MacroId::Macro2Id(it) => Some(
131+
it.lookup(db.upcast())
132+
.source(db.upcast())
133+
.map(ast::Macro::MacroDef)
134+
.map(Either::Left),
135+
),
136+
MacroId::MacroRulesId(it) => Some(
137+
it.lookup(db.upcast())
138+
.source(db.upcast())
139+
.map(ast::Macro::MacroRules)
140+
.map(Either::Left),
141+
),
142+
MacroId::ProcMacroId(it) => {
143+
Some(it.lookup(db.upcast()).source(db.upcast()).map(Either::Right))
144+
}
145+
}
133146
}
134147
}
135148
impl HasSource for Impl {

0 commit comments

Comments
 (0)