From 06fa3d8389c833b01f482bf35b0f850e627612b9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 14:22:06 +0300 Subject: Move traits to hir_def --- crates/ra_hir_def/src/impls.rs | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 crates/ra_hir_def/src/impls.rs (limited to 'crates/ra_hir_def/src/impls.rs') diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs new file mode 100644 index 000000000..4323dfcb6 --- /dev/null +++ b/crates/ra_hir_def/src/impls.rs @@ -0,0 +1,71 @@ +//! Defines hir-level representation of impls. +//! +//! The handling is similar, but is not quite the same as for other items, +//! because `impl`s don't have names. + +use std::sync::Arc; + +use ra_syntax::ast; + +use crate::{ + db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionId, ImplId, + LocationCtx, TypeAliasId, +}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ImplData { + target_trait: Option, + target_type: TypeRef, + items: Vec, + negative: bool, +} + +impl ImplData { + pub(crate) fn impl_data_query(db: &impl DefDatabase2, id: ImplId) -> Arc { + let src = id.source(db); + let items = db.ast_id_map(src.file_id); + + let target_trait = src.value.target_trait().map(TypeRef::from_ast); + let target_type = TypeRef::from_ast_opt(src.value.target_type()); + let negative = src.value.is_negative(); + + let items = if let Some(item_list) = src.value.item_list() { + let ctx = LocationCtx::new(db, id.module(db), src.file_id); + item_list + .impl_items() + .map(|item_node| match item_node { + ast::ImplItem::FnDef(it) => { + FunctionId::from_ast_id(ctx, items.ast_id(&it)).into() + } + ast::ImplItem::ConstDef(it) => { + ConstId::from_ast_id(ctx, items.ast_id(&it)).into() + } + ast::ImplItem::TypeAliasDef(it) => { + TypeAliasId::from_ast_id(ctx, items.ast_id(&it)).into() + } + }) + .collect() + } else { + Vec::new() + }; + + let res = ImplData { target_trait, target_type, items, negative }; + Arc::new(res) + } + + pub fn target_trait(&self) -> Option<&TypeRef> { + self.target_trait.as_ref() + } + + pub fn target_type(&self) -> &TypeRef { + &self.target_type + } + + pub fn items(&self) -> &[AssocItemId] { + &self.items + } + + pub fn is_negative(&self) -> bool { + self.negative + } +} -- cgit v1.2.3 From cebeedc66fc40097eae20bf1767a285d00269966 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 16:03:59 +0300 Subject: Next gen IDs for functions The current system with AstIds has two primaraly drawbacks: * It is possible to manufacture IDs out of thin air. For example, it's possible to create IDs for items which are not considered in CrateDefMap due to cfg. Or it is possible to mixup structs and unions, because they share ID space. * Getting the ID of a parent requires a secondary index. Instead, the plan is to pursue the more traditional approach, where each items stores the id of the parent declaration. This makes `FromSource` more awkward, but also more correct: now, to get from an AST to HIR, we first do this recursively for the parent item, and the just search the children of the parent for the matching def --- crates/ra_hir_def/src/impls.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'crates/ra_hir_def/src/impls.rs') diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs index 4323dfcb6..9be38c5e1 100644 --- a/crates/ra_hir_def/src/impls.rs +++ b/crates/ra_hir_def/src/impls.rs @@ -5,11 +5,12 @@ use std::sync::Arc; +use hir_expand::AstId; use ra_syntax::ast; use crate::{ - db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionId, ImplId, - LocationCtx, TypeAliasId, + db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionContainerId, + FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasId, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -35,7 +36,12 @@ impl ImplData { .impl_items() .map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { - FunctionId::from_ast_id(ctx, items.ast_id(&it)).into() + let func_id = FunctionLoc { + container: FunctionContainerId::ImplId(id), + ast_id: AstId::new(src.file_id, items.ast_id(&it)), + } + .intern(db); + func_id.into() } ast::ImplItem::ConstDef(it) => { ConstId::from_ast_id(ctx, items.ast_id(&it)).into() -- cgit v1.2.3 From 64c21ed19594b323e72605ba8c5dd4c6eee433f6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 17:39:58 +0300 Subject: Switch type aliases to new sources --- crates/ra_hir_def/src/impls.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/impls.rs') diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs index 9be38c5e1..703e4d503 100644 --- a/crates/ra_hir_def/src/impls.rs +++ b/crates/ra_hir_def/src/impls.rs @@ -10,7 +10,7 @@ use ra_syntax::ast; use crate::{ db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionContainerId, - FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasId, + FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasContainerId, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -36,18 +36,23 @@ impl ImplData { .impl_items() .map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { - let func_id = FunctionLoc { + let def = FunctionLoc { container: FunctionContainerId::ImplId(id), ast_id: AstId::new(src.file_id, items.ast_id(&it)), } .intern(db); - func_id.into() + def.into() } ast::ImplItem::ConstDef(it) => { ConstId::from_ast_id(ctx, items.ast_id(&it)).into() } ast::ImplItem::TypeAliasDef(it) => { - TypeAliasId::from_ast_id(ctx, items.ast_id(&it)).into() + let def = TypeAliasLoc { + container: TypeAliasContainerId::ImplId(id), + ast_id: AstId::new(src.file_id, items.ast_id(&it)), + } + .intern(db); + def.into() } }) .collect() -- cgit v1.2.3 From ee95a35664e6fe9153f6324cfc57872ca365887c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 17:49:57 +0300 Subject: Don't duplicate ContainerId type --- crates/ra_hir_def/src/impls.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/impls.rs') diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs index 703e4d503..574086ac7 100644 --- a/crates/ra_hir_def/src/impls.rs +++ b/crates/ra_hir_def/src/impls.rs @@ -9,8 +9,8 @@ use hir_expand::AstId; use ra_syntax::ast; use crate::{ - db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, FunctionContainerId, - FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasContainerId, TypeAliasLoc, + db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, ContainerId, + FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -37,7 +37,7 @@ impl ImplData { .map(|item_node| match item_node { ast::ImplItem::FnDef(it) => { let def = FunctionLoc { - container: FunctionContainerId::ImplId(id), + container: ContainerId::ImplId(id), ast_id: AstId::new(src.file_id, items.ast_id(&it)), } .intern(db); @@ -48,7 +48,7 @@ impl ImplData { } ast::ImplItem::TypeAliasDef(it) => { let def = TypeAliasLoc { - container: TypeAliasContainerId::ImplId(id), + container: ContainerId::ImplId(id), ast_id: AstId::new(src.file_id, items.ast_id(&it)), } .intern(db); -- cgit v1.2.3 From 111891dc2dc1d2c7ea87144e8e3ddefe23fc7b6d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 20 Nov 2019 18:00:01 +0300 Subject: Move constants to new ID This allows us to get rid of trait item index --- crates/ra_hir_def/src/impls.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_def/src/impls.rs') diff --git a/crates/ra_hir_def/src/impls.rs b/crates/ra_hir_def/src/impls.rs index 574086ac7..750a869f2 100644 --- a/crates/ra_hir_def/src/impls.rs +++ b/crates/ra_hir_def/src/impls.rs @@ -9,8 +9,8 @@ use hir_expand::AstId; use ra_syntax::ast; use crate::{ - db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstId, ContainerId, - FunctionLoc, ImplId, Intern, LocationCtx, TypeAliasLoc, + db::DefDatabase2, type_ref::TypeRef, AssocItemId, AstItemDef, ConstLoc, ContainerId, + FunctionLoc, ImplId, Intern, TypeAliasLoc, }; #[derive(Debug, Clone, PartialEq, Eq)] @@ -31,7 +31,6 @@ impl ImplData { let negative = src.value.is_negative(); let items = if let Some(item_list) = src.value.item_list() { - let ctx = LocationCtx::new(db, id.module(db), src.file_id); item_list .impl_items() .map(|item_node| match item_node { @@ -44,7 +43,12 @@ impl ImplData { def.into() } ast::ImplItem::ConstDef(it) => { - ConstId::from_ast_id(ctx, items.ast_id(&it)).into() + let def = ConstLoc { + container: ContainerId::ImplId(id), + ast_id: AstId::new(src.file_id, items.ast_id(&it)), + } + .intern(db); + def.into() } ast::ImplItem::TypeAliasDef(it) => { let def = TypeAliasLoc { -- cgit v1.2.3