mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Abstract over ItemTreeLoc
This commit is contained in:
parent
2ebf0c87c2
commit
00303c3b67
11 changed files with 160 additions and 229 deletions
|
@ -98,8 +98,8 @@ use crate::{
|
|||
data::adt::VariantData,
|
||||
db::DefDatabase,
|
||||
item_tree::{
|
||||
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeModItemNode, Macro2,
|
||||
MacroRules, Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
|
||||
Const, Enum, ExternCrate, Function, Impl, ItemTreeId, ItemTreeNode, Macro2, MacroRules,
|
||||
Static, Struct, Trait, TraitAlias, TypeAlias, Union, Use, Variant,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -212,28 +212,28 @@ impl ModuleId {
|
|||
pub type LocalModuleId = Idx<nameres::ModuleData>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ItemLoc<N: ItemTreeModItemNode> {
|
||||
pub struct ItemLoc<N: ItemTreeNode> {
|
||||
pub container: ModuleId,
|
||||
pub id: ItemTreeId<N>,
|
||||
}
|
||||
|
||||
impl<N: ItemTreeModItemNode> Clone for ItemLoc<N> {
|
||||
impl<N: ItemTreeNode> Clone for ItemLoc<N> {
|
||||
fn clone(&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 {
|
||||
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) {
|
||||
self.container.hash(state);
|
||||
self.id.hash(state);
|
||||
|
@ -241,34 +241,41 @@ impl<N: ItemTreeModItemNode> Hash for ItemLoc<N> {
|
|||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct AssocItemLoc<N: ItemTreeModItemNode> {
|
||||
pub struct AssocItemLoc<N: ItemTreeNode> {
|
||||
pub container: ItemContainerId,
|
||||
pub id: ItemTreeId<N>,
|
||||
}
|
||||
|
||||
impl<N: ItemTreeModItemNode> Clone for AssocItemLoc<N> {
|
||||
impl<N: ItemTreeNode> Clone for AssocItemLoc<N> {
|
||||
fn clone(&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 {
|
||||
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) {
|
||||
self.container.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 {
|
||||
($id:ident, $loc:ident, $intern:ident, $lookup:ident) => {
|
||||
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)]
|
||||
pub struct FunctionId(salsa::InternId);
|
||||
type FunctionLoc = AssocItemLoc<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)]
|
||||
pub struct StructId(salsa::InternId);
|
||||
type StructLoc = ItemLoc<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)]
|
||||
pub struct UnionId(salsa::InternId);
|
||||
pub type UnionLoc = ItemLoc<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)]
|
||||
pub struct EnumId(salsa::InternId);
|
||||
pub type EnumLoc = ItemLoc<Enum>;
|
||||
impl_intern!(EnumId, EnumLoc, intern_enum, lookup_intern_enum);
|
||||
impl_loc!(EnumLoc, id: Enum, container: ModuleId);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct EnumVariantId(salsa::InternId);
|
||||
|
@ -306,6 +332,7 @@ pub struct EnumVariantLoc {
|
|||
pub index: u32,
|
||||
}
|
||||
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)]
|
||||
pub struct FieldId {
|
||||
|
@ -328,46 +355,55 @@ pub struct TupleFieldId {
|
|||
pub struct ConstId(salsa::InternId);
|
||||
type ConstLoc = AssocItemLoc<Const>;
|
||||
impl_intern!(ConstId, ConstLoc, intern_const, lookup_intern_const);
|
||||
impl_loc!(ConstLoc, id: Const, container: ItemContainerId);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct StaticId(salsa::InternId);
|
||||
pub type StaticLoc = AssocItemLoc<Static>;
|
||||
impl_intern!(StaticId, StaticLoc, intern_static, lookup_intern_static);
|
||||
impl_loc!(StaticLoc, id: Static, container: ItemContainerId);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TraitId(salsa::InternId);
|
||||
pub type TraitLoc = ItemLoc<Trait>;
|
||||
impl_intern!(TraitId, TraitLoc, intern_trait, lookup_intern_trait);
|
||||
impl_loc!(TraitLoc, id: Trait, container: ModuleId);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct TraitAliasId(salsa::InternId);
|
||||
pub type TraitAliasLoc = ItemLoc<TraitAlias>;
|
||||
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)]
|
||||
pub struct TypeAliasId(salsa::InternId);
|
||||
type TypeAliasLoc = AssocItemLoc<TypeAlias>;
|
||||
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)]
|
||||
pub struct ImplId(salsa::InternId);
|
||||
type ImplLoc = ItemLoc<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)]
|
||||
pub struct UseId(salsa::InternId);
|
||||
type UseLoc = ItemLoc<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)]
|
||||
pub struct ExternCrateId(salsa::InternId);
|
||||
type ExternCrateLoc = ItemLoc<ExternCrate>;
|
||||
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)]
|
||||
pub struct ExternBlockId(salsa::InternId);
|
||||
type ExternBlockLoc = ItemLoc<ExternBlock>;
|
||||
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)]
|
||||
pub enum MacroExpander {
|
||||
|
@ -389,6 +425,7 @@ pub struct Macro2Loc {
|
|||
pub edition: Edition,
|
||||
}
|
||||
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)]
|
||||
pub struct MacroRulesId(salsa::InternId);
|
||||
|
@ -401,6 +438,7 @@ pub struct MacroRulesLoc {
|
|||
pub edition: Edition,
|
||||
}
|
||||
impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules);
|
||||
impl_loc!(MacroRulesLoc, id: MacroRules, container: ModuleId);
|
||||
|
||||
bitflags::bitflags! {
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
|
@ -421,6 +459,7 @@ pub struct ProcMacroLoc {
|
|||
pub edition: Edition,
|
||||
}
|
||||
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)]
|
||||
pub struct BlockId(salsa::InternId);
|
||||
|
@ -996,16 +1035,23 @@ pub trait HasModule {
|
|||
}
|
||||
}
|
||||
|
||||
impl<N: ItemTreeModItemNode> HasModule for AssocItemLoc<N> {
|
||||
#[inline]
|
||||
fn module(&self, db: &dyn DefDatabase) -> ModuleId {
|
||||
self.container.module(db)
|
||||
}
|
||||
}
|
||||
// In theory this impl should work out for us, but rustc thinks it collides with all the other
|
||||
// manual impls that do not have a ModuleId container...
|
||||
// impl<N, ItemId, Data> HasModule for ItemId
|
||||
// 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
|
||||
where
|
||||
N: ItemTreeModItemNode,
|
||||
N: ItemTreeNode,
|
||||
ItemId: for<'db> Lookup<Database<'db> = dyn DefDatabase + 'db, Data = ItemLoc<N>> + Copy,
|
||||
{
|
||||
#[inline]
|
||||
|
@ -1031,10 +1077,7 @@ where
|
|||
#[inline]
|
||||
fn module_for_assoc_item_loc<'db>(
|
||||
db: &(dyn 'db + DefDatabase),
|
||||
id: impl Lookup<
|
||||
Database<'db> = dyn DefDatabase + 'db,
|
||||
Data = AssocItemLoc<impl ItemTreeModItemNode>,
|
||||
>,
|
||||
id: impl Lookup<Database<'db> = dyn DefDatabase + 'db, Data = AssocItemLoc<impl ItemTreeNode>>,
|
||||
) -> ModuleId {
|
||||
id.lookup(db).container.module(db)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue