From bb6e1bf811bce09fdab115a4257e47cc0d5ddc82 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 24 Mar 2021 17:00:29 +0100 Subject: Lower traits to TraitRef instead of TypeRef --- crates/hir_def/src/item_tree.rs | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'crates/hir_def/src/item_tree.rs') diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index ca0048b16..db94bb1ef 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -31,7 +31,7 @@ use crate::{ db::DefDatabase, generics::GenericParams, path::{path, AssociatedTypeBinding, GenericArgs, ImportAlias, ModPath, Path, PathKind}, - type_ref::{Mutability, TypeBound, TypeRef}, + type_ref::{Mutability, TraitRef, TypeBound, TypeRef}, visibility::RawVisibility, }; @@ -147,6 +147,7 @@ impl ItemTree { vis, generics, type_refs, + trait_refs, inner_items, } = &mut **data; @@ -173,6 +174,7 @@ impl ItemTree { generics.arena.shrink_to_fit(); type_refs.arena.shrink_to_fit(); type_refs.map.shrink_to_fit(); + trait_refs.map.shrink_to_fit(); inner_items.shrink_to_fit(); } @@ -295,6 +297,32 @@ impl TypeRefStorage { } } +/// `TraitRef` interner. +#[derive(Default, Debug, Eq, PartialEq)] +struct TraitRefStorage { + arena: Arena>, + map: FxHashMap, Idx>>, +} + +impl TraitRefStorage { + // Note: We lie about the `Idx` to hide the interner details. + + fn intern(&mut self, ty: TraitRef) -> Idx { + if let Some(id) = self.map.get(&ty) { + return Idx::from_raw(id.into_raw()); + } + + let ty = Arc::new(ty); + let idx = self.arena.alloc(ty.clone()); + self.map.insert(ty, idx); + Idx::from_raw(idx.into_raw()) + } + + fn lookup(&self, id: Idx) -> &TraitRef { + &self.arena[Idx::from_raw(id.into_raw())] + } +} + #[derive(Default, Debug, Eq, PartialEq)] struct ItemTreeData { imports: Arena, @@ -319,6 +347,7 @@ struct ItemTreeData { vis: ItemVisibilities, generics: GenericParamsStorage, type_refs: TypeRefStorage, + trait_refs: TraitRefStorage, inner_items: FxHashMap, SmallVec<[ModItem; 1]>>, } @@ -556,6 +585,14 @@ impl Index> for ItemTree { } } +impl Index> for ItemTree { + type Output = TraitRef; + + fn index(&self, id: Idx) -> &Self::Output { + self.data().trait_refs.lookup(id) + } +} + impl Index> for ItemTree { type Output = N; fn index(&self, id: FileItemTreeId) -> &N { @@ -692,7 +729,7 @@ pub struct Trait { #[derive(Debug, Clone, Eq, PartialEq)] pub struct Impl { pub generic_params: GenericParamsId, - pub target_trait: Option>, + pub target_trait: Option>, pub target_type: Idx, pub is_negative: bool, pub items: Box<[AssocItem]>, -- cgit v1.2.3 From c2a63b97a80cb738f800da61c64e748994709c31 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 29 Mar 2021 17:46:33 +0200 Subject: Rename target_ty to self_ty --- crates/hir_def/src/item_tree.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/hir_def/src/item_tree.rs') diff --git a/crates/hir_def/src/item_tree.rs b/crates/hir_def/src/item_tree.rs index db94bb1ef..5449bbf5d 100644 --- a/crates/hir_def/src/item_tree.rs +++ b/crates/hir_def/src/item_tree.rs @@ -730,7 +730,7 @@ pub struct Trait { pub struct Impl { pub generic_params: GenericParamsId, pub target_trait: Option>, - pub target_type: Idx, + pub self_ty: Idx, pub is_negative: bool, pub items: Box<[AssocItem]>, pub ast_id: FileAstId, -- cgit v1.2.3