Abstract over ItemTreeLoc

This commit is contained in:
Lukas Wirth 2024-02-10 11:37:59 +01:00
parent 2ebf0c87c2
commit 00303c3b67
11 changed files with 160 additions and 229 deletions

View file

@ -24,12 +24,11 @@ use triomphe::Arc;
use crate::{ use crate::{
db::DefDatabase, db::DefDatabase,
item_tree::{AttrOwner, Fields, ItemTreeId, ItemTreeModItemNode}, item_tree::{AttrOwner, Fields, ItemTreeNode},
lang_item::LangItem, lang_item::LangItem,
nameres::{ModuleOrigin, ModuleSource}, nameres::{ModuleOrigin, ModuleSource},
src::{HasChildSource, HasSource}, src::{HasChildSource, HasSource},
AdtId, AssocItemLoc, AttrDefId, GenericParamId, ItemLoc, LocalFieldId, Lookup, MacroId, AdtId, AttrDefId, GenericParamId, ItemTreeLoc, LocalFieldId, Lookup, MacroId, VariantId,
VariantId,
}; };
/// Desugared attributes of an item post `cfg_attr` expansion. /// Desugared attributes of an item post `cfg_attr` expansion.
@ -356,11 +355,7 @@ impl AttrsWithOwner {
AttrDefId::FieldId(it) => { AttrDefId::FieldId(it) => {
return db.fields_attrs(it.parent)[it.local_id].clone(); return db.fields_attrs(it.parent)[it.local_id].clone();
} }
AttrDefId::EnumVariantId(it) => { AttrDefId::EnumVariantId(it) => attrs_from_item_tree_loc(db, it),
let id = it.lookup(db).id;
let tree = id.item_tree(db);
tree.raw_attrs(id.value.into()).clone()
}
AttrDefId::AdtId(it) => match it { AttrDefId::AdtId(it) => match it {
AdtId::StructId(it) => attrs_from_item_tree_loc(db, it), AdtId::StructId(it) => attrs_from_item_tree_loc(db, it),
AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it), AdtId::EnumId(it) => attrs_from_item_tree_loc(db, it),
@ -369,15 +364,15 @@ impl AttrsWithOwner {
AttrDefId::TraitId(it) => attrs_from_item_tree_loc(db, it), AttrDefId::TraitId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::TraitAliasId(it) => attrs_from_item_tree_loc(db, it), AttrDefId::TraitAliasId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::MacroId(it) => match it { AttrDefId::MacroId(it) => match it {
MacroId::Macro2Id(it) => attrs_from_item_tree(db, it.lookup(db).id), MacroId::Macro2Id(it) => attrs_from_item_tree_loc(db, it),
MacroId::MacroRulesId(it) => attrs_from_item_tree(db, it.lookup(db).id), MacroId::MacroRulesId(it) => attrs_from_item_tree_loc(db, it),
MacroId::ProcMacroId(it) => attrs_from_item_tree(db, it.lookup(db).id), MacroId::ProcMacroId(it) => attrs_from_item_tree_loc(db, it),
}, },
AttrDefId::ImplId(it) => attrs_from_item_tree_loc(db, it), AttrDefId::ImplId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::ConstId(it) => attrs_from_item_tree_assoc(db, it), AttrDefId::ConstId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::StaticId(it) => attrs_from_item_tree_assoc(db, it), AttrDefId::StaticId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::FunctionId(it) => attrs_from_item_tree_assoc(db, it), AttrDefId::FunctionId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_assoc(db, it), AttrDefId::TypeAliasId(it) => attrs_from_item_tree_loc(db, it),
AttrDefId::GenericParamId(it) => match it { AttrDefId::GenericParamId(it) => match it {
GenericParamId::ConstParamId(it) => { GenericParamId::ConstParamId(it) => {
let src = it.parent().child_source(db); let src = it.parent().child_source(db);
@ -602,29 +597,14 @@ fn any_has_attrs<'db>(
id.lookup(db).source(db).map(ast::AnyHasAttrs::new) id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
} }
fn attrs_from_item_tree<N: ItemTreeModItemNode>( fn attrs_from_item_tree_loc<'db, N: ItemTreeNode>(
db: &dyn DefDatabase, db: &(dyn DefDatabase + 'db),
id: ItemTreeId<N>, lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = impl ItemTreeLoc<Id = N>>,
) -> RawAttrs { ) -> RawAttrs {
let id = lookup.lookup(db).item_tree_id();
let tree = id.item_tree(db); let tree = id.item_tree(db);
let mod_item = N::id_to_mod_item(id.value); let attr_owner = N::attr_owner(id.value);
tree.raw_attrs(mod_item.into()).clone() tree.raw_attrs(attr_owner).clone()
}
fn attrs_from_item_tree_loc<'db, N: ItemTreeModItemNode>(
db: &(dyn DefDatabase + 'db),
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>>,
) -> RawAttrs {
let id = lookup.lookup(db).id;
attrs_from_item_tree(db, id)
}
fn attrs_from_item_tree_assoc<'db, N: ItemTreeModItemNode>(
db: &(dyn DefDatabase + 'db),
lookup: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>>,
) -> RawAttrs {
let id = lookup.lookup(db).id;
attrs_from_item_tree(db, id)
} }
pub(crate) fn fields_attrs_source_map( pub(crate) fn fields_attrs_source_map(

View file

@ -14,11 +14,11 @@ use crate::{
DynMap, DynMap,
}, },
item_scope::ItemScope, item_scope::ItemScope,
item_tree::ItemTreeModItemNode, item_tree::ItemTreeNode,
nameres::DefMap, nameres::DefMap,
src::{HasChildSource, HasSource}, src::{HasChildSource, HasSource},
AdtId, AssocItemId, AssocItemLoc, DefWithBodyId, EnumId, FieldId, ImplId, ItemLoc, Lookup, AdtId, AssocItemId, DefWithBodyId, EnumId, FieldId, ImplId, ItemTreeLoc, Lookup, MacroId,
MacroId, ModuleDefId, ModuleId, TraitId, VariantId, ModuleDefId, ModuleId, TraitId, VariantId,
}; };
pub trait ChildBySource { pub trait ChildBySource {
@ -61,13 +61,9 @@ impl ChildBySource for ImplId {
fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) { fn add_assoc_item(db: &dyn DefDatabase, res: &mut DynMap, file_id: HirFileId, item: AssocItemId) {
match item { match item {
AssocItemId::FunctionId(func) => { AssocItemId::FunctionId(func) => insert_item_loc(db, res, file_id, func, keys::FUNCTION),
insert_assoc_item_loc(db, res, file_id, func, keys::FUNCTION) AssocItemId::ConstId(konst) => insert_item_loc(db, res, file_id, konst, keys::CONST),
} AssocItemId::TypeAliasId(ty) => insert_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS),
AssocItemId::ConstId(konst) => insert_assoc_item_loc(db, res, file_id, konst, keys::CONST),
AssocItemId::TypeAliasId(ty) => {
insert_assoc_item_loc(db, res, file_id, ty, keys::TYPE_ALIAS)
}
} }
} }
@ -87,7 +83,7 @@ impl ChildBySource for ItemScope {
.for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::EXTERN_CRATE)); .for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::EXTERN_CRATE));
self.use_decls().for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::USE)); self.use_decls().for_each(|ext| insert_item_loc(db, res, file_id, ext, keys::USE));
self.unnamed_consts(db) self.unnamed_consts(db)
.for_each(|konst| insert_assoc_item_loc(db, res, file_id, konst, keys::CONST)); .for_each(|konst| insert_item_loc(db, res, file_id, konst, keys::CONST));
self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each( self.attr_macro_invocs().filter(|(id, _)| id.file_id == file_id).for_each(
|(ast_id, call_id)| { |(ast_id, call_id)| {
res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id); res[keys::ATTR_MACRO_CALL].insert(ast_id.to_node(db.upcast()), call_id);
@ -132,17 +128,13 @@ impl ChildBySource for ItemScope {
} }
match item { match item {
ModuleDefId::FunctionId(id) => { ModuleDefId::FunctionId(id) => {
insert_assoc_item_loc(db, map, file_id, id, keys::FUNCTION) insert_item_loc(db, map, file_id, id, keys::FUNCTION)
}
ModuleDefId::ConstId(id) => {
insert_assoc_item_loc(db, map, file_id, id, keys::CONST)
} }
ModuleDefId::ConstId(id) => insert_item_loc(db, map, file_id, id, keys::CONST),
ModuleDefId::TypeAliasId(id) => { ModuleDefId::TypeAliasId(id) => {
insert_assoc_item_loc(db, map, file_id, id, keys::TYPE_ALIAS) insert_item_loc(db, map, file_id, id, keys::TYPE_ALIAS)
}
ModuleDefId::StaticId(id) => {
insert_assoc_item_loc(db, map, file_id, id, keys::STATIC)
} }
ModuleDefId::StaticId(id) => insert_item_loc(db, map, file_id, id, keys::STATIC),
ModuleDefId::TraitId(id) => insert_item_loc(db, map, file_id, id, keys::TRAIT), ModuleDefId::TraitId(id) => insert_item_loc(db, map, file_id, id, keys::TRAIT),
ModuleDefId::TraitAliasId(id) => { ModuleDefId::TraitAliasId(id) => {
insert_item_loc(db, map, file_id, id, keys::TRAIT_ALIAS) insert_item_loc(db, map, file_id, id, keys::TRAIT_ALIAS)
@ -215,36 +207,20 @@ impl ChildBySource for DefWithBodyId {
} }
} }
fn insert_item_loc<ID, N>( fn insert_item_loc<ID, N, Data>(
db: &dyn DefDatabase, db: &dyn DefDatabase,
res: &mut DynMap, res: &mut DynMap,
file_id: HirFileId, file_id: HirFileId,
id: ID, id: ID,
key: Key<N::Source, ID>, key: Key<N::Source, ID>,
) where ) where
ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>> + 'static, ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = Data> + 'static,
N: ItemTreeModItemNode, Data: ItemTreeLoc<Id = N>,
N: ItemTreeNode,
N::Source: 'static, N::Source: 'static,
{ {
let loc = id.lookup(db); let loc = id.lookup(db);
if loc.id.file_id() == file_id { if loc.item_tree_id().file_id() == file_id {
res[key].insert(loc.source(db).value, id)
}
}
fn insert_assoc_item_loc<ID, N>(
db: &dyn DefDatabase,
res: &mut DynMap,
file_id: HirFileId,
id: ID,
key: Key<N::Source, ID>,
) where
ID: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<N>> + 'static,
N: ItemTreeModItemNode,
N::Source: 'static,
{
let loc = id.lookup(db);
if loc.id.file_id() == file_id {
res[key].insert(loc.source(db).value, id) res[key].insert(loc.source(db).value, id)
} }
} }

View file

@ -21,13 +21,13 @@ use crate::{
db::DefDatabase, db::DefDatabase,
dyn_map::{keys, DynMap}, dyn_map::{keys, DynMap},
expander::Expander, expander::Expander,
item_tree::{ItemTree, ItemTreeModItemNode}, item_tree::ItemTree,
lower::LowerCtx, lower::LowerCtx,
nameres::{DefMap, MacroSubNs}, nameres::{DefMap, MacroSubNs},
src::{HasChildSource, HasSource}, src::{HasChildSource, HasSource},
type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef}, type_ref::{ConstRef, LifetimeRef, TypeBound, TypeRef},
AdtId, AssocItemLoc, ConstParamId, GenericDefId, HasModule, ItemLoc, LifetimeParamId, AdtId, ConstParamId, GenericDefId, HasModule, LifetimeParamId, LocalLifetimeParamId,
LocalLifetimeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId, LocalTypeOrConstParamId, Lookup, TypeOrConstParamId, TypeParamId,
}; };
/// Data about a generic type parameter (to a function, struct, impl, ...). /// Data about a generic type parameter (to a function, struct, impl, ...).

View file

@ -337,20 +337,15 @@ from_attrs!(
LifetimeParamData(Idx<LifetimeParamData>), LifetimeParamData(Idx<LifetimeParamData>),
); );
/// Trait implemented by all item nodes in the item tree. /// Trait implemented by all nodes in the item tree.
pub trait ItemTreeModItemNode: Clone { pub trait ItemTreeNode: Clone {
type Source: AstIdNode + Into<ast::Item>; type Source: AstIdNode;
fn ast_id(&self) -> FileAstId<Self::Source>; fn ast_id(&self) -> FileAstId<Self::Source>;
/// Looks up an instance of `Self` in an item tree. /// Looks up an instance of `Self` in an item tree.
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self; fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self;
fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner;
/// Downcasts a `ModItem` to a `FileItemTreeId` specific to this type.
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>>;
/// Upcasts a `FileItemTreeId` to a generic `ModItem`.
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem;
} }
pub struct FileItemTreeId<N>(Idx<N>); pub struct FileItemTreeId<N>(Idx<N>);
@ -495,7 +490,7 @@ macro_rules! mod_items {
)+ )+
$( $(
impl ItemTreeModItemNode for $typ { impl ItemTreeNode for $typ {
type Source = $ast; type Source = $ast;
fn ast_id(&self) -> FileAstId<Self::Source> { fn ast_id(&self) -> FileAstId<Self::Source> {
@ -506,15 +501,8 @@ macro_rules! mod_items {
&tree.data().$fld[index] &tree.data().$fld[index]
} }
fn id_from_mod_item(mod_item: ModItem) -> Option<FileItemTreeId<Self>> { fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
match mod_item { AttrOwner::ModItem(ModItem::$typ(id))
ModItem::$typ(id) => Some(id),
_ => None,
}
}
fn id_to_mod_item(id: FileItemTreeId<Self>) -> ModItem {
ModItem::$typ(id)
} }
} }
@ -578,17 +566,26 @@ impl Index<RawVisibilityId> for ItemTree {
} }
} }
impl<N: ItemTreeModItemNode> Index<FileItemTreeId<N>> for ItemTree { impl<N: ItemTreeNode> Index<FileItemTreeId<N>> for ItemTree {
type Output = N; type Output = N;
fn index(&self, id: FileItemTreeId<N>) -> &N { fn index(&self, id: FileItemTreeId<N>) -> &N {
N::lookup(self, id.index()) N::lookup(self, id.index())
} }
} }
impl Index<FileItemTreeId<Variant>> for ItemTree { impl ItemTreeNode for Variant {
type Output = Variant; type Source = ast::Variant;
fn index(&self, id: FileItemTreeId<Variant>) -> &Variant {
&self[id.index()] fn ast_id(&self) -> FileAstId<Self::Source> {
self.ast_id
}
fn lookup(tree: &ItemTree, index: Idx<Self>) -> &Self {
&tree.data().variants[index]
}
fn attr_owner(id: FileItemTreeId<Self>) -> AttrOwner {
AttrOwner::Variant(id)
} }
} }
@ -1027,7 +1024,7 @@ impl AssocItem {
} }
} }
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variant { pub struct Variant {
pub name: Name, pub name: Name,
pub fields: Fields, pub fields: Fields,

View file

@ -13,7 +13,7 @@ use crate::{
use super::*; use super::*;
fn id<N: ItemTreeModItemNode>(index: Idx<N>) -> FileItemTreeId<N> { fn id<N: ItemTreeNode>(index: Idx<N>) -> FileItemTreeId<N> {
FileItemTreeId(index) FileItemTreeId(index)
} }
@ -267,7 +267,7 @@ impl<'a> Ctx<'a> {
if let Some(data) = self.lower_variant(&variant) { if let Some(data) = self.lower_variant(&variant) {
let idx = self.data().variants.alloc(data); let idx = self.data().variants.alloc(data);
self.add_attrs( self.add_attrs(
FileItemTreeId(idx).into(), id(idx).into(),
RawAttrs::new(self.db.upcast(), &variant, self.span_map()), RawAttrs::new(self.db.upcast(), &variant, self.span_map()),
); );
} }

View file

@ -98,8 +98,8 @@ use crate::{
data::adt::VariantData, data::adt::VariantData,
db::DefDatabase, db::DefDatabase,
item_tree::{ item_tree::{
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeModItemNode, Macro2, Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
}, },
}; };
@ -212,28 +212,28 @@ impl ModuleId {
pub type LocalModuleId = Idx<nameres::ModuleData>; pub type LocalModuleId = Idx<nameres::ModuleData>;
#[derive(Debug)] #[derive(Debug)]
pub struct ItemLoc<N: ItemTreeModItemNode> { pub struct ItemLoc<N: ItemTreeNode> {
pub container: ModuleId, pub container: ModuleId,
pub id: ItemTreeId<N>, pub id: ItemTreeId<N>,
} }
impl<N: ItemTreeModItemNode> Clone for ItemLoc<N> { impl<N: ItemTreeNode> Clone for ItemLoc<N> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
} }
impl<N: ItemTreeModItemNode> Copy for ItemLoc<N> {} impl<N: ItemTreeNode> Copy for ItemLoc<N> {}
impl<N: ItemTreeModItemNode> PartialEq for ItemLoc<N> { impl<N: ItemTreeNode> PartialEq for ItemLoc<N> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id self.container == other.container && self.id == other.id
} }
} }
impl<N: ItemTreeModItemNode> Eq for ItemLoc<N> {} impl<N: ItemTreeNode> Eq for ItemLoc<N> {}
impl<N: ItemTreeModItemNode> Hash for ItemLoc<N> { impl<N: ItemTreeNode> Hash for ItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state); self.container.hash(state);
self.id.hash(state); self.id.hash(state);
@ -241,34 +241,41 @@ impl<N: ItemTreeModItemNode> Hash for ItemLoc<N> {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct AssocItemLoc<N: ItemTreeModItemNode> { pub struct AssocItemLoc<N: ItemTreeNode> {
pub container: ItemContainerId, pub container: ItemContainerId,
pub id: ItemTreeId<N>, pub id: ItemTreeId<N>,
} }
impl<N: ItemTreeModItemNode> Clone for AssocItemLoc<N> { impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
*self *self
} }
} }
impl<N: ItemTreeModItemNode> Copy for AssocItemLoc<N> {} impl<N: ItemTreeNode> Copy for AssocItemLoc<N> {}
impl<N: ItemTreeModItemNode> PartialEq for AssocItemLoc<N> { impl<N: ItemTreeNode> PartialEq for AssocItemLoc<N> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.container == other.container && self.id == other.id self.container == other.container && self.id == other.id
} }
} }
impl<N: ItemTreeModItemNode> Eq for AssocItemLoc<N> {} impl<N: ItemTreeNode> Eq for AssocItemLoc<N> {}
impl<N: ItemTreeModItemNode> Hash for AssocItemLoc<N> { impl<N: ItemTreeNode> Hash for AssocItemLoc<N> {
fn hash<H: Hasher>(&self, state: &mut H) { fn hash<H: Hasher>(&self, state: &mut H) {
self.container.hash(state); self.container.hash(state);
self.id.hash(state); self.id.hash(state);
} }
} }
pub trait ItemTreeLoc {
type Container;
type Id;
fn item_tree_id(&self) -> ItemTreeId<Self::Id>;
fn container(&self) -> Self::Container;
}
macro_rules! impl_intern { macro_rules! impl_intern {
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => { ($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
impl_intern_key!($id); impl_intern_key!($id);
@ -276,25 +283,44 @@ macro_rules! impl_intern {
}; };
} }
macro_rules! impl_loc {
($loc:ident, $id:ident: $id_ty:ident, $container:ident: $container_type:ident) => {
impl ItemTreeLoc for $loc {
type Container = $container_type;
type Id = $id_ty;
fn item_tree_id(&self) -> ItemTreeId<Self::Id> {
self.$id
}
fn container(&self) -> Self::Container {
self.$container
}
}
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FunctionId(salsa::InternId); pub struct FunctionId(salsa::InternId);
type FunctionLoc = AssocItemLoc<Function>; type FunctionLoc = AssocItemLoc<Function>;
impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function); impl_intern!(FunctionId, FunctionLoc, intern_function, lookup_intern_function);
impl_loc!(FunctionLoc, id: Function, container: ItemContainerId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct StructId(salsa::InternId); pub struct StructId(salsa::InternId);
type StructLoc = ItemLoc<Struct>; type StructLoc = ItemLoc<Struct>;
impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct); impl_intern!(StructId, StructLoc, intern_struct, lookup_intern_struct);
impl_loc!(StructLoc, id: Struct, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct UnionId(salsa::InternId); pub struct UnionId(salsa::InternId);
pub type UnionLoc = ItemLoc<Union>; pub type UnionLoc = ItemLoc<Union>;
impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union); impl_intern!(UnionId, UnionLoc, intern_union, lookup_intern_union);
impl_loc!(UnionLoc, id: Union, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct EnumId(salsa::InternId); pub struct EnumId(salsa::InternId);
pub type EnumLoc = ItemLoc<Enum>; pub type EnumLoc = ItemLoc<Enum>;
impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum); impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
impl_loc!(EnumLoc, id: Enum, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EnumVariantId(salsa::InternId); pub struct EnumVariantId(salsa::InternId);
@ -306,6 +332,7 @@ pub struct EnumVariantLoc {
pub index: u32, pub index: u32,
} }
impl_intern!(EnumVariantId, EnumVariantLoc, intern_enum_variant, lookup_intern_enum_variant); impl_intern!(EnumVariantId, EnumVariantLoc, intern_enum_variant, lookup_intern_enum_variant);
impl_loc!(EnumVariantLoc, id: Variant, parent: EnumId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct FieldId { pub struct FieldId {
@ -328,46 +355,55 @@ pub struct TupleFieldId {
pub struct ConstId(salsa::InternId); pub struct ConstId(salsa::InternId);
type ConstLoc = AssocItemLoc<Const>; type ConstLoc = AssocItemLoc<Const>;
impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const); impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
impl_loc!(ConstLoc, id: Const, container: ItemContainerId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct StaticId(salsa::InternId); pub struct StaticId(salsa::InternId);
pub type StaticLoc = AssocItemLoc<Static>; pub type StaticLoc = AssocItemLoc<Static>;
impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static); impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
impl_loc!(StaticLoc, id: Static, container: ItemContainerId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TraitId(salsa::InternId); pub struct TraitId(salsa::InternId);
pub type TraitLoc = ItemLoc<Trait>; pub type TraitLoc = ItemLoc<Trait>;
impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait); impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
impl_loc!(TraitLoc, id: Trait, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TraitAliasId(salsa::InternId); pub struct TraitAliasId(salsa::InternId);
pub type TraitAliasLoc = ItemLoc<TraitAlias>; pub type TraitAliasLoc = ItemLoc<TraitAlias>;
impl_intern!(TraitAliasId, TraitAliasLoc, intern_trait_alias, lookup_intern_trait_alias); impl_intern!(TraitAliasId, TraitAliasLoc, intern_trait_alias, lookup_intern_trait_alias);
impl_loc!(TraitAliasLoc, id: TraitAlias, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TypeAliasId(salsa::InternId); pub struct TypeAliasId(salsa::InternId);
type TypeAliasLoc = AssocItemLoc<TypeAlias>; type TypeAliasLoc = AssocItemLoc<TypeAlias>;
impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias); impl_intern!(TypeAliasId, TypeAliasLoc, intern_type_alias, lookup_intern_type_alias);
impl_loc!(TypeAliasLoc, id: TypeAlias, container: ItemContainerId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ImplId(salsa::InternId); pub struct ImplId(salsa::InternId);
type ImplLoc = ItemLoc<Impl>; type ImplLoc = ItemLoc<Impl>;
impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl); impl_intern!(ImplId, ImplLoc, intern_impl, lookup_intern_impl);
impl_loc!(ImplLoc, id: Impl, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct UseId(salsa::InternId); pub struct UseId(salsa::InternId);
type UseLoc = ItemLoc<Use>; type UseLoc = ItemLoc<Use>;
impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use); impl_intern!(UseId, UseLoc, intern_use, lookup_intern_use);
impl_loc!(UseLoc, id: Use, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ExternCrateId(salsa::InternId); pub struct ExternCrateId(salsa::InternId);
type ExternCrateLoc = ItemLoc<ExternCrate>; type ExternCrateLoc = ItemLoc<ExternCrate>;
impl_intern!(ExternCrateId, ExternCrateLoc, intern_extern_crate, lookup_intern_extern_crate); impl_intern!(ExternCrateId, ExternCrateLoc, intern_extern_crate, lookup_intern_extern_crate);
impl_loc!(ExternCrateLoc, id: ExternCrate, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct ExternBlockId(salsa::InternId); pub struct ExternBlockId(salsa::InternId);
type ExternBlockLoc = ItemLoc<ExternBlock>; type ExternBlockLoc = ItemLoc<ExternBlock>;
impl_intern!(ExternBlockId, ExternBlockLoc, intern_extern_block, lookup_intern_extern_block); impl_intern!(ExternBlockId, ExternBlockLoc, intern_extern_block, lookup_intern_extern_block);
impl_loc!(ExternBlockLoc, id: ExternBlock, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MacroExpander { pub enum MacroExpander {
@ -389,6 +425,7 @@ pub struct Macro2Loc {
pub edition: Edition, pub edition: Edition,
} }
impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2); impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2);
impl_loc!(Macro2Loc, id: Macro2, container: ModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct MacroRulesId(salsa::InternId); pub struct MacroRulesId(salsa::InternId);
@ -401,6 +438,7 @@ pub struct MacroRulesLoc {
pub edition: Edition, pub edition: Edition,
} }
impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules); impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules);
impl_loc!(MacroRulesLoc, id: MacroRules, container: ModuleId);
bitflags::bitflags! { bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -421,6 +459,7 @@ pub struct ProcMacroLoc {
pub edition: Edition, pub edition: Edition,
} }
impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro); impl_intern!(ProcMacroId, ProcMacroLoc, intern_proc_macro, lookup_intern_proc_macro);
impl_loc!(ProcMacroLoc, id: Function, container: CrateRootModuleId);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct BlockId(salsa::InternId); pub struct BlockId(salsa::InternId);
@ -996,16 +1035,23 @@ pub trait HasModule {
} }
} }
impl<N: ItemTreeModItemNode> HasModule for AssocItemLoc<N> { // In theory this impl should work out for us, but rustc thinks it collides with all the other
#[inline] // manual impls that do not have a ModuleId container...
fn module(&self, db: &dyn DefDatabase) -> ModuleId { // impl<N, ItemId, Data> HasModule for ItemId
self.container.module(db) // where
} // N: ItemTreeNode,
} // ItemId: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = Data> + Copy,
// Data: ItemTreeLoc<Id = N, Container = ModuleId>,
// {
// #[inline]
// fn module(&self, db: &dyn DefDatabase) -> ModuleId {
// self.lookup(db).container()
// }
// }
impl<N, ItemId> HasModule for ItemId impl<N, ItemId> HasModule for ItemId
where where
N: ItemTreeModItemNode, N: ItemTreeNode,
ItemId: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>> + Copy, ItemId: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>> + Copy,
{ {
#[inline] #[inline]
@ -1031,10 +1077,7 @@ where
#[inline] #[inline]
fn module_for_assoc_item_loc<'db>( fn module_for_assoc_item_loc<'db>(
db: &(dyn 'db + DefDatabase), db: &(dyn 'db + DefDatabase),
id: impl Lookup< id: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<impl ItemTreeNode>>,
Database<'db> = dyn DefDatabase + 'db,
Data = AssocItemLoc<impl ItemTreeModItemNode>,
>,
) -> ModuleId { ) -> ModuleId {
id.lookup(db).container.module(db) id.lookup(db).container.module(db)
} }

View file

@ -33,8 +33,8 @@ use crate::{
db::DefDatabase, db::DefDatabase,
item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports}, item_scope::{ImportId, ImportOrExternCrate, ImportType, PerNsGlobImports},
item_tree::{ item_tree::{
self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId, self, ExternCrate, Fields, FileItemTreeId, ImportKind, ItemTree, ItemTreeId, ItemTreeNode,
ItemTreeModItemNode, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModKind, TreeId,
}, },
macro_call_as_call_id, macro_call_as_call_id_with_eager, macro_call_as_call_id, macro_call_as_call_id_with_eager,
nameres::{ nameres::{

View file

@ -4,91 +4,29 @@ use hir_expand::InFile;
use la_arena::ArenaMap; use la_arena::ArenaMap;
use syntax::ast; use syntax::ast;
use crate::{ use crate::{db::DefDatabase, item_tree::ItemTreeNode, ItemTreeLoc, Lookup, UseId};
db::DefDatabase, item_tree::ItemTreeModItemNode, AssocItemLoc, EnumVariantLoc, ItemLoc, Lookup,
Macro2Loc, MacroRulesLoc, ProcMacroLoc, UseId,
};
pub trait HasSource { pub trait HasSource {
type Value; type Value;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>; fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value>;
} }
impl<N: ItemTreeModItemNode> HasSource for AssocItemLoc<N> { impl<T> HasSource for T
type Value = N::Source; where
T: ItemTreeLoc,
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> { T::Id: ItemTreeNode,
let tree = self.id.item_tree(db); {
let ast_id_map = db.ast_id_map(self.id.file_id()); type Value = <T::Id as ItemTreeNode>::Source;
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl<N: ItemTreeModItemNode> HasSource for ItemLoc<N> {
type Value = N::Source;
fn source(&self, db: &dyn DefDatabase) -> InFile<N::Source> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl HasSource for EnumVariantLoc {
type Value = ast::Variant;
fn source(&self, db: &dyn DefDatabase) -> InFile<ast::Variant> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id).to_node(&root))
}
}
impl HasSource for Macro2Loc {
type Value = ast::MacroDef;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> { fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db); let id = self.item_tree_id();
let ast_id_map = db.ast_id_map(self.id.file_id()); let file_id = id.file_id();
let root = db.parse_or_expand(self.id.file_id()); let tree = id.item_tree(db);
let node = &tree[self.id.value]; let ast_id_map = db.ast_id_map(file_id);
let root = db.parse_or_expand(file_id);
let node = &tree[id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root)) InFile::new(file_id, ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl HasSource for MacroRulesLoc {
type Value = ast::MacroRules;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
}
}
impl HasSource for ProcMacroLoc {
type Value = ast::Fn;
fn source(&self, db: &dyn DefDatabase) -> InFile<Self::Value> {
let tree = self.id.item_tree(db);
let ast_id_map = db.ast_id_map(self.id.file_id());
let root = db.parse_or_expand(self.id.file_id());
let node = &tree[self.id.value];
InFile::new(self.id.file_id(), ast_id_map.get(node.ast_id()).to_node(&root))
} }
} }

View file

@ -814,9 +814,8 @@ impl HirDisplay for Ty {
// Don't count Sized but count when it absent // Don't count Sized but count when it absent
// (i.e. when explicit ?Sized bound is set). // (i.e. when explicit ?Sized bound is set).
let default_sized = SizedByDefault::Sized { let default_sized =
anchor: func.lookup(db.upcast()).module(db.upcast()).krate(), SizedByDefault::Sized { anchor: func.krate(db.upcast()) };
};
let sized_bounds = bounds let sized_bounds = bounds
.skip_binders() .skip_binders()
.iter() .iter()
@ -1025,7 +1024,7 @@ impl HirDisplay for Ty {
let data = let data =
(*datas).as_ref().map(|rpit| rpit.impl_traits[idx].bounds.clone()); (*datas).as_ref().map(|rpit| rpit.impl_traits[idx].bounds.clone());
let bounds = data.substitute(Interner, &parameters); let bounds = data.substitute(Interner, &parameters);
let krate = func.lookup(db.upcast()).module(db.upcast()).krate(); let krate = func.krate(db.upcast());
write_bounds_like_dyn_trait_with_prefix( write_bounds_like_dyn_trait_with_prefix(
f, f,
"impl", "impl",
@ -1191,7 +1190,7 @@ impl HirDisplay for Ty {
let data = let data =
(*datas).as_ref().map(|rpit| rpit.impl_traits[idx].bounds.clone()); (*datas).as_ref().map(|rpit| rpit.impl_traits[idx].bounds.clone());
let bounds = data.substitute(Interner, &opaque_ty.substitution); let bounds = data.substitute(Interner, &opaque_ty.substitution);
let krate = func.lookup(db.upcast()).module(db.upcast()).krate(); let krate = func.krate(db.upcast());
write_bounds_like_dyn_trait_with_prefix( write_bounds_like_dyn_trait_with_prefix(
f, f,
"impl", "impl",

View file

@ -1225,7 +1225,7 @@ impl<'a> TyLoweringContext<'a> {
.collect(); .collect();
if !ctx.unsized_types.borrow().contains(&self_ty) { if !ctx.unsized_types.borrow().contains(&self_ty) {
let krate = func.lookup(ctx.db.upcast()).module(ctx.db.upcast()).krate(); let krate = func.krate(ctx.db.upcast());
let sized_trait = ctx let sized_trait = ctx
.db .db
.lang_item(krate, LangItem::Sized) .lang_item(krate, LangItem::Sized)
@ -1824,11 +1824,10 @@ impl CallableDefId {
pub fn krate(self, db: &dyn HirDatabase) -> CrateId { pub fn krate(self, db: &dyn HirDatabase) -> CrateId {
let db = db.upcast(); let db = db.upcast();
match self { match self {
CallableDefId::FunctionId(f) => f.lookup(db).module(db), CallableDefId::FunctionId(f) => f.krate(db),
CallableDefId::StructId(s) => s.lookup(db).container, CallableDefId::StructId(s) => s.krate(db),
CallableDefId::EnumVariantId(e) => e.module(db), CallableDefId::EnumVariantId(e) => e.krate(db),
} }
.krate()
} }
} }

View file

@ -44,7 +44,7 @@ use hir_def::{
data::adt::VariantData, data::adt::VariantData,
generics::{LifetimeParamData, TypeOrConstParamData, TypeParamProvenance}, generics::{LifetimeParamData, TypeOrConstParamData, TypeParamProvenance},
hir::{BindingAnnotation, BindingId, ExprOrPatId, LabelId, Pat}, hir::{BindingAnnotation, BindingId, ExprOrPatId, LabelId, Pat},
item_tree::ItemTreeModItemNode, item_tree::ItemTreeNode,
lang_item::LangItemTarget, lang_item::LangItemTarget,
layout::{self, ReprOptions, TargetDataLayout}, layout::{self, ReprOptions, TargetDataLayout},
nameres::{self, diagnostics::DefDiagnostic}, nameres::{self, diagnostics::DefDiagnostic},
@ -1768,7 +1768,7 @@ pub struct Function {
impl Function { impl Function {
pub fn module(self, db: &dyn HirDatabase) -> Module { pub fn module(self, db: &dyn HirDatabase) -> Module {
self.id.lookup(db.upcast()).module(db.upcast()).into() self.id.module(db.upcast()).into()
} }
pub fn name(self, db: &dyn HirDatabase) -> Name { pub fn name(self, db: &dyn HirDatabase) -> Name {
@ -1909,8 +1909,7 @@ impl Function {
{ {
return None; return None;
} }
let loc = self.id.lookup(db.upcast()); let def_map = db.crate_def_map(HasModule::krate(&self.id, db.upcast()));
let def_map = db.crate_def_map(HasModule::krate(&loc, db.upcast()));
def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() }) def_map.fn_as_proc_macro(self.id).map(|id| Macro { id: id.into() })
} }
@ -2119,7 +2118,7 @@ pub struct Const {
impl Const { impl Const {
pub fn module(self, db: &dyn HirDatabase) -> Module { pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } Module { id: self.id.module(db.upcast()) }
} }
pub fn name(self, db: &dyn HirDatabase) -> Option<Name> { pub fn name(self, db: &dyn HirDatabase) -> Option<Name> {
@ -2174,7 +2173,7 @@ pub struct Static {
impl Static { impl Static {
pub fn module(self, db: &dyn HirDatabase) -> Module { pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } Module { id: self.id.module(db.upcast()) }
} }
pub fn name(self, db: &dyn HirDatabase) -> Name { pub fn name(self, db: &dyn HirDatabase) -> Name {
@ -2293,7 +2292,7 @@ impl TypeAlias {
} }
pub fn module(self, db: &dyn HirDatabase) -> Module { pub fn module(self, db: &dyn HirDatabase) -> Module {
Module { id: self.id.lookup(db.upcast()).module(db.upcast()) } Module { id: self.id.module(db.upcast()) }
} }
pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> { pub fn type_ref(self, db: &dyn HirDatabase) -> Option<TypeRef> {
@ -2566,15 +2565,15 @@ impl AsAssocItem for DefWithBody {
} }
} }
fn as_assoc_item<'db, ID, DEF, AST>( fn as_assoc_item<'db, ID, DEF, LOC>(
db: &(dyn HirDatabase + 'db), db: &(dyn HirDatabase + 'db),
ctor: impl FnOnce(DEF) -> AssocItem, ctor: impl FnOnce(DEF) -> AssocItem,
id: ID, id: ID,
) -> Option<AssocItem> ) -> Option<AssocItem>
where where
ID: Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<AST>>, ID: Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<LOC>>,
DEF: From<ID>, DEF: From<ID>,
AST: ItemTreeModItemNode, LOC: ItemTreeNode,
{ {
match id.lookup(db.upcast()).container { match id.lookup(db.upcast()).container {
ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) => Some(ctor(DEF::from(id))), ItemContainerId::TraitId(_) | ItemContainerId::ImplId(_) => Some(ctor(DEF::from(id))),