aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_def/src/traits.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-11-20 13:23:38 +0000
committerGitHub <[email protected]>2019-11-20 13:23:38 +0000
commitb568bcfe6d94d5f4c6cdc012b766473e5b771a26 (patch)
treee54c306ac0512e3610430574dc8bea39e4b50218 /crates/ra_hir_def/src/traits.rs
parent2d47f380baad4eacd87331c4b86c0ecb28239499 (diff)
parentcebeedc66fc40097eae20bf1767a285d00269966 (diff)
Merge #2325
2325: Next gen IDs for functions r=matklad a=matklad 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 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir_def/src/traits.rs')
-rw-r--r--crates/ra_hir_def/src/traits.rs25
1 files changed, 20 insertions, 5 deletions
diff --git a/crates/ra_hir_def/src/traits.rs b/crates/ra_hir_def/src/traits.rs
index a8ba31594..6e36bc0d0 100644
--- a/crates/ra_hir_def/src/traits.rs
+++ b/crates/ra_hir_def/src/traits.rs
@@ -2,14 +2,17 @@
2 2
3use std::sync::Arc; 3use std::sync::Arc;
4 4
5use hir_expand::name::{AsName, Name}; 5use hir_expand::{
6 name::{AsName, Name},
7 AstId,
8};
6 9
7use ra_syntax::ast::{self, NameOwner}; 10use ra_syntax::ast::{self, NameOwner};
8use rustc_hash::FxHashMap; 11use rustc_hash::FxHashMap;
9 12
10use crate::{ 13use crate::{
11 db::DefDatabase2, AssocItemId, AstItemDef, ConstId, FunctionId, LocationCtx, ModuleDefId, 14 db::DefDatabase2, AssocItemId, AstItemDef, ConstId, FunctionContainerId, FunctionLoc, Intern,
12 ModuleId, TraitId, TypeAliasId, 15 LocationCtx, ModuleDefId, ModuleId, TraitId, TypeAliasId,
13}; 16};
14 17
15#[derive(Debug, Clone, PartialEq, Eq)] 18#[derive(Debug, Clone, PartialEq, Eq)]
@@ -26,11 +29,17 @@ impl TraitData {
26 let module = tr.module(db); 29 let module = tr.module(db);
27 let ctx = LocationCtx::new(db, module, src.file_id); 30 let ctx = LocationCtx::new(db, module, src.file_id);
28 let auto = src.value.is_auto(); 31 let auto = src.value.is_auto();
32 let ast_id_map = db.ast_id_map(src.file_id);
29 let items = if let Some(item_list) = src.value.item_list() { 33 let items = if let Some(item_list) = src.value.item_list() {
30 item_list 34 item_list
31 .impl_items() 35 .impl_items()
32 .map(|item_node| match item_node { 36 .map(|item_node| match item_node {
33 ast::ImplItem::FnDef(it) => FunctionId::from_ast(ctx, &it).into(), 37 ast::ImplItem::FnDef(it) => FunctionLoc {
38 container: FunctionContainerId::TraitId(tr),
39 ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)),
40 }
41 .intern(db)
42 .into(),
34 ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).into(), 43 ast::ImplItem::ConstDef(it) => ConstId::from_ast(ctx, &it).into(),
35 ast::ImplItem::TypeAliasDef(it) => TypeAliasId::from_ast(ctx, &it).into(), 44 ast::ImplItem::TypeAliasDef(it) => TypeAliasId::from_ast(ctx, &it).into(),
36 }) 45 })
@@ -54,7 +63,13 @@ impl TraitItemsIndex {
54 for decl in crate_def_map[module.module_id].scope.declarations() { 63 for decl in crate_def_map[module.module_id].scope.declarations() {
55 if let ModuleDefId::TraitId(tr) = decl { 64 if let ModuleDefId::TraitId(tr) = decl {
56 for item in db.trait_data(tr).items.iter() { 65 for item in db.trait_data(tr).items.iter() {
57 index.traits_by_def.insert(*item, tr); 66 match item {
67 AssocItemId::FunctionId(_) => (),
68 _ => {
69 let prev = index.traits_by_def.insert(*item, tr);
70 assert!(prev.is_none());
71 }
72 }
58 } 73 }
59 } 74 }
60 } 75 }