diff options
author | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
---|---|---|
committer | Seivan Heidari <[email protected]> | 2019-11-28 07:19:14 +0000 |
commit | 18a0937585b836ec5ed054b9ae48e0156ab6d9ef (patch) | |
tree | 9de2c0267ddcc00df717f90034d0843d751a851b /crates/ra_hir_def | |
parent | a7394b44c870f585eacfeb3036a33471aff49ff8 (diff) | |
parent | 484acc8a61d599662ed63a4cbda091d38a982551 (diff) |
Merge branch 'master' of https://github.com/rust-analyzer/rust-analyzer into feature/themes
Diffstat (limited to 'crates/ra_hir_def')
-rw-r--r-- | crates/ra_hir_def/src/adt.rs | 43 | ||||
-rw-r--r-- | crates/ra_hir_def/src/attr.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 60 | ||||
-rw-r--r-- | crates/ra_hir_def/src/db.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/docs.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/generics.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lang_item.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 57 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 36 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/path_resolution.rs | 16 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/tests.rs | 7 | ||||
-rw-r--r-- | crates/ra_hir_def/src/path.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/test_db.rs | 4 |
16 files changed, 180 insertions, 111 deletions
diff --git a/crates/ra_hir_def/src/adt.rs b/crates/ra_hir_def/src/adt.rs index c9f30923e..3666529b0 100644 --- a/crates/ra_hir_def/src/adt.rs +++ b/crates/ra_hir_def/src/adt.rs | |||
@@ -12,25 +12,25 @@ use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | |||
12 | 12 | ||
13 | use crate::{ | 13 | use crate::{ |
14 | db::DefDatabase, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, HasChildSource, | 14 | db::DefDatabase, trace::Trace, type_ref::TypeRef, AstItemDef, EnumId, HasChildSource, |
15 | LocalEnumVariantId, LocalStructFieldId, StructOrUnionId, VariantId, | 15 | LocalEnumVariantId, LocalStructFieldId, StructId, UnionId, VariantId, |
16 | }; | 16 | }; |
17 | 17 | ||
18 | /// Note that we use `StructData` for unions as well! | 18 | /// Note that we use `StructData` for unions as well! |
19 | #[derive(Debug, Clone, PartialEq, Eq)] | 19 | #[derive(Debug, Clone, PartialEq, Eq)] |
20 | pub struct StructData { | 20 | pub struct StructData { |
21 | pub name: Option<Name>, | 21 | pub name: Name, |
22 | pub variant_data: Arc<VariantData>, | 22 | pub variant_data: Arc<VariantData>, |
23 | } | 23 | } |
24 | 24 | ||
25 | #[derive(Debug, Clone, PartialEq, Eq)] | 25 | #[derive(Debug, Clone, PartialEq, Eq)] |
26 | pub struct EnumData { | 26 | pub struct EnumData { |
27 | pub name: Option<Name>, | 27 | pub name: Name, |
28 | pub variants: Arena<LocalEnumVariantId, EnumVariantData>, | 28 | pub variants: Arena<LocalEnumVariantId, EnumVariantData>, |
29 | } | 29 | } |
30 | 30 | ||
31 | #[derive(Debug, Clone, PartialEq, Eq)] | 31 | #[derive(Debug, Clone, PartialEq, Eq)] |
32 | pub struct EnumVariantData { | 32 | pub struct EnumVariantData { |
33 | pub name: Option<Name>, | 33 | pub name: Name, |
34 | pub variant_data: Arc<VariantData>, | 34 | pub variant_data: Arc<VariantData>, |
35 | } | 35 | } |
36 | 36 | ||
@@ -49,26 +49,38 @@ pub struct StructFieldData { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | impl StructData { | 51 | impl StructData { |
52 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructOrUnionId) -> Arc<StructData> { | 52 | pub(crate) fn struct_data_query(db: &impl DefDatabase, id: StructId) -> Arc<StructData> { |
53 | let src = id.source(db); | 53 | let src = id.source(db); |
54 | let name = src.value.name().map(|n| n.as_name()); | 54 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
55 | let variant_data = VariantData::new(src.value.kind()); | 55 | let variant_data = VariantData::new(src.value.kind()); |
56 | let variant_data = Arc::new(variant_data); | 56 | let variant_data = Arc::new(variant_data); |
57 | Arc::new(StructData { name, variant_data }) | 57 | Arc::new(StructData { name, variant_data }) |
58 | } | 58 | } |
59 | pub(crate) fn union_data_query(db: &impl DefDatabase, id: UnionId) -> Arc<StructData> { | ||
60 | let src = id.source(db); | ||
61 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); | ||
62 | let variant_data = VariantData::new( | ||
63 | src.value | ||
64 | .record_field_def_list() | ||
65 | .map(ast::StructKind::Record) | ||
66 | .unwrap_or(ast::StructKind::Unit), | ||
67 | ); | ||
68 | let variant_data = Arc::new(variant_data); | ||
69 | Arc::new(StructData { name, variant_data }) | ||
70 | } | ||
59 | } | 71 | } |
60 | 72 | ||
61 | impl EnumData { | 73 | impl EnumData { |
62 | pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> { | 74 | pub(crate) fn enum_data_query(db: &impl DefDatabase, e: EnumId) -> Arc<EnumData> { |
63 | let src = e.source(db); | 75 | let src = e.source(db); |
64 | let name = src.value.name().map(|n| n.as_name()); | 76 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
65 | let mut trace = Trace::new_for_arena(); | 77 | let mut trace = Trace::new_for_arena(); |
66 | lower_enum(&mut trace, &src.value); | 78 | lower_enum(&mut trace, &src.value); |
67 | Arc::new(EnumData { name, variants: trace.into_arena() }) | 79 | Arc::new(EnumData { name, variants: trace.into_arena() }) |
68 | } | 80 | } |
69 | 81 | ||
70 | pub(crate) fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { | 82 | pub fn variant(&self, name: &Name) -> Option<LocalEnumVariantId> { |
71 | let (id, _) = self.variants.iter().find(|(_id, data)| data.name.as_ref() == Some(name))?; | 83 | let (id, _) = self.variants.iter().find(|(_id, data)| &data.name == name)?; |
72 | Some(id) | 84 | Some(id) |
73 | } | 85 | } |
74 | } | 86 | } |
@@ -92,7 +104,7 @@ fn lower_enum( | |||
92 | trace.alloc( | 104 | trace.alloc( |
93 | || var.clone(), | 105 | || var.clone(), |
94 | || EnumVariantData { | 106 | || EnumVariantData { |
95 | name: var.name().map(|it| it.as_name()), | 107 | name: var.name().map_or_else(Name::missing, |it| it.as_name()), |
96 | variant_data: Arc::new(VariantData::new(var.kind())), | 108 | variant_data: Arc::new(VariantData::new(var.kind())), |
97 | }, | 109 | }, |
98 | ); | 110 | ); |
@@ -117,6 +129,10 @@ impl VariantData { | |||
117 | } | 129 | } |
118 | } | 130 | } |
119 | 131 | ||
132 | pub fn field(&self, name: &Name) -> Option<LocalStructFieldId> { | ||
133 | self.fields().iter().find_map(|(id, data)| if &data.name == name { Some(id) } else { None }) | ||
134 | } | ||
135 | |||
120 | pub fn is_unit(&self) -> bool { | 136 | pub fn is_unit(&self) -> bool { |
121 | match self { | 137 | match self { |
122 | VariantData::Unit => true, | 138 | VariantData::Unit => true, |
@@ -137,7 +153,12 @@ impl HasChildSource for VariantId { | |||
137 | let src = it.parent.child_source(db); | 153 | let src = it.parent.child_source(db); |
138 | src.map(|map| map[it.local_id].kind()) | 154 | src.map(|map| map[it.local_id].kind()) |
139 | } | 155 | } |
140 | VariantId::StructId(it) => it.0.source(db).map(|it| it.kind()), | 156 | VariantId::StructId(it) => it.source(db).map(|it| it.kind()), |
157 | VariantId::UnionId(it) => it.source(db).map(|it| { | ||
158 | it.record_field_def_list() | ||
159 | .map(ast::StructKind::Record) | ||
160 | .unwrap_or(ast::StructKind::Unit) | ||
161 | }), | ||
141 | }; | 162 | }; |
142 | let mut trace = Trace::new_for_map(); | 163 | let mut trace = Trace::new_for_map(); |
143 | lower_struct(&mut trace, &src.value); | 164 | lower_struct(&mut trace, &src.value); |
diff --git a/crates/ra_hir_def/src/attr.rs b/crates/ra_hir_def/src/attr.rs index 53456fc08..fffb22201 100644 --- a/crates/ra_hir_def/src/attr.rs +++ b/crates/ra_hir_def/src/attr.rs | |||
@@ -35,7 +35,7 @@ impl Attrs { | |||
35 | match def { | 35 | match def { |
36 | AttrDefId::ModuleId(module) => { | 36 | AttrDefId::ModuleId(module) => { |
37 | let def_map = db.crate_def_map(module.krate); | 37 | let def_map = db.crate_def_map(module.krate); |
38 | let src = match def_map[module.module_id].declaration_source(db) { | 38 | let src = match def_map[module.local_id].declaration_source(db) { |
39 | Some(it) => it, | 39 | Some(it) => it, |
40 | None => return Attrs::default(), | 40 | None => return Attrs::default(), |
41 | }; | 41 | }; |
@@ -54,9 +54,9 @@ impl Attrs { | |||
54 | Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) | 54 | Attrs::from_attrs_owner(db, src.map(|it| it as &dyn AttrsOwner)) |
55 | } | 55 | } |
56 | AttrDefId::AdtId(it) => match it { | 56 | AttrDefId::AdtId(it) => match it { |
57 | AdtId::StructId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), | 57 | AdtId::StructId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), |
58 | AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), | 58 | AdtId::EnumId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), |
59 | AdtId::UnionId(it) => attrs_from_ast(it.0.lookup_intern(db).ast_id, db), | 59 | AdtId::UnionId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), |
60 | }, | 60 | }, |
61 | AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), | 61 | AttrDefId::TraitId(it) => attrs_from_ast(it.lookup_intern(db).ast_id, db), |
62 | AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), | 62 | AttrDefId::MacroDefId(it) => attrs_from_ast(it.ast_id, db), |
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index d77ccb272..a57a0176d 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -6,8 +6,7 @@ pub mod scope; | |||
6 | use std::{ops::Index, sync::Arc}; | 6 | use std::{ops::Index, sync::Arc}; |
7 | 7 | ||
8 | use hir_expand::{ | 8 | use hir_expand::{ |
9 | either::Either, hygiene::Hygiene, AstId, HirFileId, MacroCallLoc, MacroDefId, MacroFileKind, | 9 | either::Either, hygiene::Hygiene, AstId, HirFileId, MacroDefId, MacroFileKind, Source, |
10 | Source, | ||
11 | }; | 10 | }; |
12 | use ra_arena::{map::ArenaMap, Arena}; | 11 | use ra_arena::{map::ArenaMap, Arena}; |
13 | use ra_syntax::{ast, AstNode, AstPtr}; | 12 | use ra_syntax::{ast, AstNode, AstPtr}; |
@@ -47,7 +46,7 @@ impl Expander { | |||
47 | 46 | ||
48 | if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) { | 47 | if let Some(path) = macro_call.path().and_then(|path| self.parse_path(path)) { |
49 | if let Some(def) = self.resolve_path_as_macro(db, &path) { | 48 | if let Some(def) = self.resolve_path_as_macro(db, &path) { |
50 | let call_id = db.intern_macro(MacroCallLoc { def, ast_id }); | 49 | let call_id = def.as_call_id(db, ast_id); |
51 | let file_id = call_id.as_file(MacroFileKind::Expr); | 50 | let file_id = call_id.as_file(MacroFileKind::Expr); |
52 | if let Some(node) = db.parse_or_expand(file_id) { | 51 | if let Some(node) = db.parse_or_expand(file_id) { |
53 | if let Some(expr) = ast::Expr::cast(node) { | 52 | if let Some(expr) = ast::Expr::cast(node) { |
@@ -83,7 +82,7 @@ impl Expander { | |||
83 | } | 82 | } |
84 | 83 | ||
85 | fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &Path) -> Option<MacroDefId> { | 84 | fn resolve_path_as_macro(&self, db: &impl DefDatabase, path: &Path) -> Option<MacroDefId> { |
86 | self.crate_def_map.resolve_path(db, self.module.module_id, path).0.take_macros() | 85 | self.crate_def_map.resolve_path(db, self.module.local_id, path).0.take_macros() |
87 | } | 86 | } |
88 | } | 87 | } |
89 | 88 | ||
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index 68bea34df..fee10b237 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -86,39 +86,53 @@ impl TypeAliasData { | |||
86 | 86 | ||
87 | #[derive(Debug, Clone, PartialEq, Eq)] | 87 | #[derive(Debug, Clone, PartialEq, Eq)] |
88 | pub struct TraitData { | 88 | pub struct TraitData { |
89 | pub name: Option<Name>, | 89 | pub name: Name, |
90 | pub items: Vec<AssocItemId>, | 90 | pub items: Vec<(Name, AssocItemId)>, |
91 | pub auto: bool, | 91 | pub auto: bool, |
92 | } | 92 | } |
93 | 93 | ||
94 | impl TraitData { | 94 | impl TraitData { |
95 | pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> { | 95 | pub(crate) fn trait_data_query(db: &impl DefDatabase, tr: TraitId) -> Arc<TraitData> { |
96 | let src = tr.source(db); | 96 | let src = tr.source(db); |
97 | let name = src.value.name().map(|n| n.as_name()); | 97 | let name = src.value.name().map_or_else(Name::missing, |n| n.as_name()); |
98 | let auto = src.value.is_auto(); | 98 | let auto = src.value.is_auto(); |
99 | let ast_id_map = db.ast_id_map(src.file_id); | 99 | let ast_id_map = db.ast_id_map(src.file_id); |
100 | |||
101 | let container = ContainerId::TraitId(tr); | ||
100 | let items = if let Some(item_list) = src.value.item_list() { | 102 | let items = if let Some(item_list) = src.value.item_list() { |
101 | item_list | 103 | item_list |
102 | .impl_items() | 104 | .impl_items() |
103 | .map(|item_node| match item_node { | 105 | .map(|item_node| match item_node { |
104 | ast::ImplItem::FnDef(it) => FunctionLoc { | 106 | ast::ImplItem::FnDef(it) => { |
105 | container: ContainerId::TraitId(tr), | 107 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
106 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | 108 | let def = FunctionLoc { |
109 | container, | ||
110 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | ||
111 | } | ||
112 | .intern(db) | ||
113 | .into(); | ||
114 | (name, def) | ||
107 | } | 115 | } |
108 | .intern(db) | 116 | ast::ImplItem::ConstDef(it) => { |
109 | .into(), | 117 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
110 | ast::ImplItem::ConstDef(it) => ConstLoc { | 118 | let def = ConstLoc { |
111 | container: ContainerId::TraitId(tr), | 119 | container, |
112 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | 120 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), |
121 | } | ||
122 | .intern(db) | ||
123 | .into(); | ||
124 | (name, def) | ||
113 | } | 125 | } |
114 | .intern(db) | 126 | ast::ImplItem::TypeAliasDef(it) => { |
115 | .into(), | 127 | let name = it.name().map_or_else(Name::missing, |it| it.as_name()); |
116 | ast::ImplItem::TypeAliasDef(it) => TypeAliasLoc { | 128 | let def = TypeAliasLoc { |
117 | container: ContainerId::TraitId(tr), | 129 | container, |
118 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), | 130 | ast_id: AstId::new(src.file_id, ast_id_map.ast_id(&it)), |
131 | } | ||
132 | .intern(db) | ||
133 | .into(); | ||
134 | (name, def) | ||
119 | } | 135 | } |
120 | .intern(db) | ||
121 | .into(), | ||
122 | }) | 136 | }) |
123 | .collect() | 137 | .collect() |
124 | } else { | 138 | } else { |
@@ -128,11 +142,18 @@ impl TraitData { | |||
128 | } | 142 | } |
129 | 143 | ||
130 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { | 144 | pub fn associated_types(&self) -> impl Iterator<Item = TypeAliasId> + '_ { |
131 | self.items.iter().filter_map(|item| match item { | 145 | self.items.iter().filter_map(|(_name, item)| match item { |
132 | AssocItemId::TypeAliasId(t) => Some(*t), | 146 | AssocItemId::TypeAliasId(t) => Some(*t), |
133 | _ => None, | 147 | _ => None, |
134 | }) | 148 | }) |
135 | } | 149 | } |
150 | |||
151 | pub fn associated_type_by_name(&self, name: &Name) -> Option<TypeAliasId> { | ||
152 | self.items.iter().find_map(|(item_name, item)| match item { | ||
153 | AssocItemId::TypeAliasId(t) if item_name == name => Some(*t), | ||
154 | _ => None, | ||
155 | }) | ||
156 | } | ||
136 | } | 157 | } |
137 | 158 | ||
138 | #[derive(Debug, Clone, PartialEq, Eq)] | 159 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -193,6 +214,7 @@ impl ImplData { | |||
193 | 214 | ||
194 | #[derive(Debug, Clone, PartialEq, Eq)] | 215 | #[derive(Debug, Clone, PartialEq, Eq)] |
195 | pub struct ConstData { | 216 | pub struct ConstData { |
217 | /// const _: () = (); | ||
196 | pub name: Option<Name>, | 218 | pub name: Option<Name>, |
197 | pub type_ref: TypeRef, | 219 | pub type_ref: TypeRef, |
198 | } | 220 | } |
diff --git a/crates/ra_hir_def/src/db.rs b/crates/ra_hir_def/src/db.rs index 32adb11bd..ef5611ffc 100644 --- a/crates/ra_hir_def/src/db.rs +++ b/crates/ra_hir_def/src/db.rs | |||
@@ -18,8 +18,8 @@ use crate::{ | |||
18 | CrateDefMap, | 18 | CrateDefMap, |
19 | }, | 19 | }, |
20 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, | 20 | AttrDefId, ConstId, ConstLoc, DefWithBodyId, EnumId, FunctionId, FunctionLoc, GenericDefId, |
21 | ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructOrUnionId, TraitId, TypeAliasId, | 21 | ImplId, ItemLoc, ModuleId, StaticId, StaticLoc, StructId, TraitId, TypeAliasId, TypeAliasLoc, |
22 | TypeAliasLoc, | 22 | UnionId, |
23 | }; | 23 | }; |
24 | 24 | ||
25 | #[salsa::query_group(InternDatabaseStorage)] | 25 | #[salsa::query_group(InternDatabaseStorage)] |
@@ -27,7 +27,9 @@ pub trait InternDatabase: SourceDatabase { | |||
27 | #[salsa::interned] | 27 | #[salsa::interned] |
28 | fn intern_function(&self, loc: FunctionLoc) -> FunctionId; | 28 | fn intern_function(&self, loc: FunctionLoc) -> FunctionId; |
29 | #[salsa::interned] | 29 | #[salsa::interned] |
30 | fn intern_struct_or_union(&self, loc: ItemLoc<ast::StructDef>) -> StructOrUnionId; | 30 | fn intern_struct(&self, loc: ItemLoc<ast::StructDef>) -> StructId; |
31 | #[salsa::interned] | ||
32 | fn intern_union(&self, loc: ItemLoc<ast::UnionDef>) -> UnionId; | ||
31 | #[salsa::interned] | 33 | #[salsa::interned] |
32 | fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> EnumId; | 34 | fn intern_enum(&self, loc: ItemLoc<ast::EnumDef>) -> EnumId; |
33 | #[salsa::interned] | 35 | #[salsa::interned] |
@@ -57,7 +59,9 @@ pub trait DefDatabase: InternDatabase + AstDatabase { | |||
57 | fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>; | 59 | fn crate_def_map(&self, krate: CrateId) -> Arc<CrateDefMap>; |
58 | 60 | ||
59 | #[salsa::invoke(StructData::struct_data_query)] | 61 | #[salsa::invoke(StructData::struct_data_query)] |
60 | fn struct_data(&self, id: StructOrUnionId) -> Arc<StructData>; | 62 | fn struct_data(&self, id: StructId) -> Arc<StructData>; |
63 | #[salsa::invoke(StructData::union_data_query)] | ||
64 | fn union_data(&self, id: UnionId) -> Arc<StructData>; | ||
61 | 65 | ||
62 | #[salsa::invoke(EnumData::enum_data_query)] | 66 | #[salsa::invoke(EnumData::enum_data_query)] |
63 | fn enum_data(&self, e: EnumId) -> Arc<EnumData>; | 67 | fn enum_data(&self, e: EnumId) -> Arc<EnumData>; |
diff --git a/crates/ra_hir_def/src/docs.rs b/crates/ra_hir_def/src/docs.rs index 90a8627bc..34ed9b7a5 100644 --- a/crates/ra_hir_def/src/docs.rs +++ b/crates/ra_hir_def/src/docs.rs | |||
@@ -36,7 +36,7 @@ impl Documentation { | |||
36 | match def { | 36 | match def { |
37 | AttrDefId::ModuleId(module) => { | 37 | AttrDefId::ModuleId(module) => { |
38 | let def_map = db.crate_def_map(module.krate); | 38 | let def_map = db.crate_def_map(module.krate); |
39 | let src = def_map[module.module_id].declaration_source(db)?; | 39 | let src = def_map[module.local_id].declaration_source(db)?; |
40 | docs_from_ast(&src.value) | 40 | docs_from_ast(&src.value) |
41 | } | 41 | } |
42 | AttrDefId::StructFieldId(it) => { | 42 | AttrDefId::StructFieldId(it) => { |
@@ -47,9 +47,9 @@ impl Documentation { | |||
47 | } | 47 | } |
48 | } | 48 | } |
49 | AttrDefId::AdtId(it) => match it { | 49 | AttrDefId::AdtId(it) => match it { |
50 | AdtId::StructId(it) => docs_from_ast(&it.0.source(db).value), | 50 | AdtId::StructId(it) => docs_from_ast(&it.source(db).value), |
51 | AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), | 51 | AdtId::EnumId(it) => docs_from_ast(&it.source(db).value), |
52 | AdtId::UnionId(it) => docs_from_ast(&it.0.source(db).value), | 52 | AdtId::UnionId(it) => docs_from_ast(&it.source(db).value), |
53 | }, | 53 | }, |
54 | AttrDefId::EnumVariantId(it) => { | 54 | AttrDefId::EnumVariantId(it) => { |
55 | let src = it.parent.child_source(db); | 55 | let src = it.parent.child_source(db); |
diff --git a/crates/ra_hir_def/src/generics.rs b/crates/ra_hir_def/src/generics.rs index 015fe772e..3f94e40e4 100644 --- a/crates/ra_hir_def/src/generics.rs +++ b/crates/ra_hir_def/src/generics.rs | |||
@@ -60,10 +60,8 @@ impl GenericParams { | |||
60 | // FIXME: add `: Sized` bound for everything except for `Self` in traits | 60 | // FIXME: add `: Sized` bound for everything except for `Self` in traits |
61 | match def { | 61 | match def { |
62 | GenericDefId::FunctionId(it) => generics.fill(&it.lookup(db).source(db).value, start), | 62 | GenericDefId::FunctionId(it) => generics.fill(&it.lookup(db).source(db).value, start), |
63 | GenericDefId::AdtId(AdtId::StructId(it)) => { | 63 | GenericDefId::AdtId(AdtId::StructId(it)) => generics.fill(&it.source(db).value, start), |
64 | generics.fill(&it.0.source(db).value, start) | 64 | GenericDefId::AdtId(AdtId::UnionId(it)) => generics.fill(&it.source(db).value, start), |
65 | } | ||
66 | GenericDefId::AdtId(AdtId::UnionId(it)) => generics.fill(&it.0.source(db).value, start), | ||
67 | GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value, start), | 65 | GenericDefId::AdtId(AdtId::EnumId(it)) => generics.fill(&it.source(db).value, start), |
68 | GenericDefId::TraitId(it) => { | 66 | GenericDefId::TraitId(it) => { |
69 | // traits get the Self type as an implicit first type parameter | 67 | // traits get the Self type as an implicit first type parameter |
diff --git a/crates/ra_hir_def/src/lang_item.rs b/crates/ra_hir_def/src/lang_item.rs index f15c23db9..f4fdbdcfc 100644 --- a/crates/ra_hir_def/src/lang_item.rs +++ b/crates/ra_hir_def/src/lang_item.rs | |||
@@ -41,7 +41,7 @@ impl LangItems { | |||
41 | crate_def_map | 41 | crate_def_map |
42 | .modules | 42 | .modules |
43 | .iter() | 43 | .iter() |
44 | .filter_map(|(module_id, _)| db.module_lang_items(ModuleId { krate, module_id })) | 44 | .filter_map(|(local_id, _)| db.module_lang_items(ModuleId { krate, local_id })) |
45 | .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v)))); | 45 | .for_each(|it| lang_items.items.extend(it.items.iter().map(|(k, v)| (k.clone(), *v)))); |
46 | 46 | ||
47 | Arc::new(lang_items) | 47 | Arc::new(lang_items) |
@@ -80,7 +80,7 @@ impl LangItems { | |||
80 | fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) { | 80 | fn collect_lang_items(&mut self, db: &impl DefDatabase, module: ModuleId) { |
81 | // Look for impl targets | 81 | // Look for impl targets |
82 | let def_map = db.crate_def_map(module.krate); | 82 | let def_map = db.crate_def_map(module.krate); |
83 | let module_data = &def_map[module.module_id]; | 83 | let module_data = &def_map[module.local_id]; |
84 | for &impl_block in module_data.impls.iter() { | 84 | for &impl_block in module_data.impls.iter() { |
85 | self.collect_lang_item(db, impl_block, LangItemTarget::ImplBlockId) | 85 | self.collect_lang_item(db, impl_block, LangItemTarget::ImplBlockId) |
86 | } | 86 | } |
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 8e8c2d749..bc5530896 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -27,7 +27,7 @@ pub mod body; | |||
27 | pub mod resolver; | 27 | pub mod resolver; |
28 | 28 | ||
29 | mod trace; | 29 | mod trace; |
30 | mod nameres; | 30 | pub mod nameres; |
31 | 31 | ||
32 | #[cfg(test)] | 32 | #[cfg(test)] |
33 | mod test_db; | 33 | mod test_db; |
@@ -50,7 +50,7 @@ impl_arena_id!(LocalImportId); | |||
50 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 50 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
51 | pub struct ModuleId { | 51 | pub struct ModuleId { |
52 | pub krate: CrateId, | 52 | pub krate: CrateId, |
53 | pub module_id: LocalModuleId, | 53 | pub local_id: LocalModuleId, |
54 | } | 54 | } |
55 | 55 | ||
56 | /// An ID of a module, **local** to a specific crate | 56 | /// An ID of a module, **local** to a specific crate |
@@ -141,30 +141,26 @@ impl Lookup for FunctionId { | |||
141 | } | 141 | } |
142 | 142 | ||
143 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 143 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
144 | pub struct StructOrUnionId(salsa::InternId); | 144 | pub struct StructId(salsa::InternId); |
145 | impl_intern_key!(StructOrUnionId); | 145 | impl_intern_key!(StructId); |
146 | impl AstItemDef<ast::StructDef> for StructOrUnionId { | 146 | impl AstItemDef<ast::StructDef> for StructId { |
147 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { | 147 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { |
148 | db.intern_struct_or_union(loc) | 148 | db.intern_struct(loc) |
149 | } | 149 | } |
150 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { | 150 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { |
151 | db.lookup_intern_struct_or_union(self) | 151 | db.lookup_intern_struct(self) |
152 | } | 152 | } |
153 | } | 153 | } |
154 | 154 | ||
155 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 155 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
156 | pub struct StructId(pub StructOrUnionId); | 156 | pub struct UnionId(salsa::InternId); |
157 | impl From<StructId> for StructOrUnionId { | 157 | impl_intern_key!(UnionId); |
158 | fn from(id: StructId) -> StructOrUnionId { | 158 | impl AstItemDef<ast::UnionDef> for UnionId { |
159 | id.0 | 159 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::UnionDef>) -> Self { |
160 | db.intern_union(loc) | ||
160 | } | 161 | } |
161 | } | 162 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::UnionDef> { |
162 | 163 | db.lookup_intern_union(self) | |
163 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
164 | pub struct UnionId(pub StructOrUnionId); | ||
165 | impl From<UnionId> for StructOrUnionId { | ||
166 | fn from(id: UnionId) -> StructOrUnionId { | ||
167 | id.0 | ||
168 | } | 164 | } |
169 | } | 165 | } |
170 | 166 | ||
@@ -402,6 +398,16 @@ impl_froms!( | |||
402 | ConstId | 398 | ConstId |
403 | ); | 399 | ); |
404 | 400 | ||
401 | impl From<AssocItemId> for GenericDefId { | ||
402 | fn from(item: AssocItemId) -> Self { | ||
403 | match item { | ||
404 | AssocItemId::FunctionId(f) => f.into(), | ||
405 | AssocItemId::ConstId(c) => c.into(), | ||
406 | AssocItemId::TypeAliasId(t) => t.into(), | ||
407 | } | ||
408 | } | ||
409 | } | ||
410 | |||
405 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 411 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
406 | pub enum AttrDefId { | 412 | pub enum AttrDefId { |
407 | ModuleId(ModuleId), | 413 | ModuleId(ModuleId), |
@@ -435,6 +441,7 @@ impl_froms!( | |||
435 | pub enum VariantId { | 441 | pub enum VariantId { |
436 | EnumVariantId(EnumVariantId), | 442 | EnumVariantId(EnumVariantId), |
437 | StructId(StructId), | 443 | StructId(StructId), |
444 | UnionId(UnionId), | ||
438 | } | 445 | } |
439 | impl_froms!(VariantId: EnumVariantId, StructId); | 446 | impl_froms!(VariantId: EnumVariantId, StructId); |
440 | 447 | ||
@@ -485,13 +492,23 @@ impl HasModule for ConstLoc { | |||
485 | impl HasModule for AdtId { | 492 | impl HasModule for AdtId { |
486 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | 493 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { |
487 | match self { | 494 | match self { |
488 | AdtId::StructId(it) => it.0.module(db), | 495 | AdtId::StructId(it) => it.module(db), |
489 | AdtId::UnionId(it) => it.0.module(db), | 496 | AdtId::UnionId(it) => it.module(db), |
490 | AdtId::EnumId(it) => it.module(db), | 497 | AdtId::EnumId(it) => it.module(db), |
491 | } | 498 | } |
492 | } | 499 | } |
493 | } | 500 | } |
494 | 501 | ||
502 | impl HasModule for DefWithBodyId { | ||
503 | fn module(&self, db: &impl db::DefDatabase) -> ModuleId { | ||
504 | match self { | ||
505 | DefWithBodyId::FunctionId(it) => it.lookup(db).module(db), | ||
506 | DefWithBodyId::StaticId(it) => it.lookup(db).module(db), | ||
507 | DefWithBodyId::ConstId(it) => it.lookup(db).module(db), | ||
508 | } | ||
509 | } | ||
510 | } | ||
511 | |||
495 | impl HasModule for StaticLoc { | 512 | impl HasModule for StaticLoc { |
496 | fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { | 513 | fn module(&self, _db: &impl db::DefDatabase) -> ModuleId { |
497 | self.container | 514 | self.container |
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index 41becf8df..fd8245113 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | use hir_expand::{ | 6 | use hir_expand::{ |
7 | builtin_macro::find_builtin_macro, | 7 | builtin_macro::find_builtin_macro, |
8 | name::{self, AsName, Name}, | 8 | name::{self, AsName, Name}, |
9 | HirFileId, MacroCallId, MacroCallLoc, MacroDefId, MacroDefKind, MacroFileKind, | 9 | HirFileId, MacroCallId, MacroDefId, MacroDefKind, MacroFileKind, |
10 | }; | 10 | }; |
11 | use ra_cfg::CfgOptions; | 11 | use ra_cfg::CfgOptions; |
12 | use ra_db::{CrateId, FileId}; | 12 | use ra_db::{CrateId, FileId}; |
@@ -25,7 +25,7 @@ use crate::{ | |||
25 | per_ns::PerNs, | 25 | per_ns::PerNs, |
26 | AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, | 26 | AdtId, AstId, AstItemDef, ConstLoc, ContainerId, EnumId, EnumVariantId, FunctionLoc, ImplId, |
27 | Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, | 27 | Intern, LocalImportId, LocalModuleId, LocationCtx, ModuleDefId, ModuleId, StaticLoc, StructId, |
28 | StructOrUnionId, TraitId, TypeAliasLoc, UnionId, | 28 | TraitId, TypeAliasLoc, UnionId, |
29 | }; | 29 | }; |
30 | 30 | ||
31 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { | 31 | pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> CrateDefMap { |
@@ -37,7 +37,7 @@ pub(super) fn collect_defs(db: &impl DefDatabase, mut def_map: CrateDefMap) -> C | |||
37 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); | 37 | log::debug!("crate dep {:?} -> {:?}", dep.name, dep.crate_id); |
38 | def_map.extern_prelude.insert( | 38 | def_map.extern_prelude.insert( |
39 | dep.as_name(), | 39 | dep.as_name(), |
40 | ModuleId { krate: dep.crate_id, module_id: dep_def_map.root }.into(), | 40 | ModuleId { krate: dep.crate_id, local_id: dep_def_map.root }.into(), |
41 | ); | 41 | ); |
42 | 42 | ||
43 | // look for the prelude | 43 | // look for the prelude |
@@ -323,7 +323,7 @@ where | |||
323 | tested_by!(glob_across_crates); | 323 | tested_by!(glob_across_crates); |
324 | // glob import from other crate => we can just import everything once | 324 | // glob import from other crate => we can just import everything once |
325 | let item_map = self.db.crate_def_map(m.krate); | 325 | let item_map = self.db.crate_def_map(m.krate); |
326 | let scope = &item_map[m.module_id].scope; | 326 | let scope = &item_map[m.local_id].scope; |
327 | 327 | ||
328 | // Module scoped macros is included | 328 | // Module scoped macros is included |
329 | let items = scope | 329 | let items = scope |
@@ -337,7 +337,7 @@ where | |||
337 | // glob import from same crate => we do an initial | 337 | // glob import from same crate => we do an initial |
338 | // import, and then need to propagate any further | 338 | // import, and then need to propagate any further |
339 | // additions | 339 | // additions |
340 | let scope = &self.def_map[m.module_id].scope; | 340 | let scope = &self.def_map[m.local_id].scope; |
341 | 341 | ||
342 | // Module scoped macros is included | 342 | // Module scoped macros is included |
343 | let items = scope | 343 | let items = scope |
@@ -349,7 +349,7 @@ where | |||
349 | self.update(module_id, Some(import_id), &items); | 349 | self.update(module_id, Some(import_id), &items); |
350 | // record the glob import in case we add further items | 350 | // record the glob import in case we add further items |
351 | self.glob_imports | 351 | self.glob_imports |
352 | .entry(m.module_id) | 352 | .entry(m.local_id) |
353 | .or_default() | 353 | .or_default() |
354 | .push((module_id, import_id)); | 354 | .push((module_id, import_id)); |
355 | } | 355 | } |
@@ -362,7 +362,7 @@ where | |||
362 | .variants | 362 | .variants |
363 | .iter() | 363 | .iter() |
364 | .filter_map(|(local_id, variant_data)| { | 364 | .filter_map(|(local_id, variant_data)| { |
365 | let name = variant_data.name.clone()?; | 365 | let name = variant_data.name.clone(); |
366 | let variant = EnumVariantId { parent: e, local_id }; | 366 | let variant = EnumVariantId { parent: e, local_id }; |
367 | let res = Resolution { | 367 | let res = Resolution { |
368 | def: PerNs::both(variant.into(), variant.into()), | 368 | def: PerNs::both(variant.into(), variant.into()), |
@@ -480,7 +480,7 @@ where | |||
480 | ); | 480 | ); |
481 | 481 | ||
482 | if let Some(def) = resolved_res.resolved_def.take_macros() { | 482 | if let Some(def) = resolved_res.resolved_def.take_macros() { |
483 | let call_id = self.db.intern_macro(MacroCallLoc { def, ast_id: *ast_id }); | 483 | let call_id = def.as_call_id(self.db, *ast_id); |
484 | resolved.push((*module_id, call_id, def)); | 484 | resolved.push((*module_id, call_id, def)); |
485 | res = ReachedFixedPoint::No; | 485 | res = ReachedFixedPoint::No; |
486 | return false; | 486 | return false; |
@@ -590,7 +590,7 @@ where | |||
590 | raw::RawItemKind::Impl(imp) => { | 590 | raw::RawItemKind::Impl(imp) => { |
591 | let module = ModuleId { | 591 | let module = ModuleId { |
592 | krate: self.def_collector.def_map.krate, | 592 | krate: self.def_collector.def_map.krate, |
593 | module_id: self.module_id, | 593 | local_id: self.module_id, |
594 | }; | 594 | }; |
595 | let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); | 595 | let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); |
596 | let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); | 596 | let imp_id = ImplId::from_ast_id(ctx, self.raw_items[imp].ast_id); |
@@ -673,7 +673,7 @@ where | |||
673 | modules[self.module_id].children.insert(name.clone(), res); | 673 | modules[self.module_id].children.insert(name.clone(), res); |
674 | let resolution = Resolution { | 674 | let resolution = Resolution { |
675 | def: PerNs::types( | 675 | def: PerNs::types( |
676 | ModuleId { krate: self.def_collector.def_map.krate, module_id: res }.into(), | 676 | ModuleId { krate: self.def_collector.def_map.krate, local_id: res }.into(), |
677 | ), | 677 | ), |
678 | import: None, | 678 | import: None, |
679 | }; | 679 | }; |
@@ -682,8 +682,7 @@ where | |||
682 | } | 682 | } |
683 | 683 | ||
684 | fn define_def(&mut self, def: &raw::DefData) { | 684 | fn define_def(&mut self, def: &raw::DefData) { |
685 | let module = | 685 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: self.module_id }; |
686 | ModuleId { krate: self.def_collector.def_map.krate, module_id: self.module_id }; | ||
687 | let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); | 686 | let ctx = LocationCtx::new(self.def_collector.db, module, self.file_id); |
688 | 687 | ||
689 | let name = def.name.clone(); | 688 | let name = def.name.clone(); |
@@ -698,14 +697,12 @@ where | |||
698 | PerNs::values(def.into()) | 697 | PerNs::values(def.into()) |
699 | } | 698 | } |
700 | raw::DefKind::Struct(ast_id) => { | 699 | raw::DefKind::Struct(ast_id) => { |
701 | let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); | 700 | let id = StructId::from_ast_id(ctx, ast_id).into(); |
702 | let s = StructId(id).into(); | 701 | PerNs::both(id, id) |
703 | PerNs::both(s, s) | ||
704 | } | 702 | } |
705 | raw::DefKind::Union(ast_id) => { | 703 | raw::DefKind::Union(ast_id) => { |
706 | let id = StructOrUnionId::from_ast_id(ctx, ast_id).into(); | 704 | let id = UnionId::from_ast_id(ctx, ast_id).into(); |
707 | let u = UnionId(id).into(); | 705 | PerNs::both(id, id) |
708 | PerNs::both(u, u) | ||
709 | } | 706 | } |
710 | raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), | 707 | raw::DefKind::Enum(ast_id) => PerNs::types(EnumId::from_ast_id(ctx, ast_id).into()), |
711 | raw::DefKind::Const(ast_id) => { | 708 | raw::DefKind::Const(ast_id) => { |
@@ -775,8 +772,7 @@ where | |||
775 | if let Some(macro_def) = mac.path.as_ident().and_then(|name| { | 772 | if let Some(macro_def) = mac.path.as_ident().and_then(|name| { |
776 | self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) | 773 | self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name) |
777 | }) { | 774 | }) { |
778 | let macro_call_id = | 775 | let macro_call_id = macro_def.as_call_id(self.def_collector.db, ast_id); |
779 | self.def_collector.db.intern_macro(MacroCallLoc { def: macro_def, ast_id }); | ||
780 | 776 | ||
781 | self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_def); | 777 | self.def_collector.collect_macro_expansion(self.module_id, macro_call_id, macro_def); |
782 | return; | 778 | return; |
diff --git a/crates/ra_hir_def/src/nameres/path_resolution.rs b/crates/ra_hir_def/src/nameres/path_resolution.rs index 9455f22bb..b72c55bd1 100644 --- a/crates/ra_hir_def/src/nameres/path_resolution.rs +++ b/crates/ra_hir_def/src/nameres/path_resolution.rs | |||
@@ -74,19 +74,19 @@ impl CrateDefMap { | |||
74 | PathKind::DollarCrate(krate) => { | 74 | PathKind::DollarCrate(krate) => { |
75 | if krate == self.krate { | 75 | if krate == self.krate { |
76 | tested_by!(macro_dollar_crate_self); | 76 | tested_by!(macro_dollar_crate_self); |
77 | PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) | 77 | PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) |
78 | } else { | 78 | } else { |
79 | let def_map = db.crate_def_map(krate); | 79 | let def_map = db.crate_def_map(krate); |
80 | let module = ModuleId { krate, module_id: def_map.root }; | 80 | let module = ModuleId { krate, local_id: def_map.root }; |
81 | tested_by!(macro_dollar_crate_other); | 81 | tested_by!(macro_dollar_crate_other); |
82 | PerNs::types(module.into()) | 82 | PerNs::types(module.into()) |
83 | } | 83 | } |
84 | } | 84 | } |
85 | PathKind::Crate => { | 85 | PathKind::Crate => { |
86 | PerNs::types(ModuleId { krate: self.krate, module_id: self.root }.into()) | 86 | PerNs::types(ModuleId { krate: self.krate, local_id: self.root }.into()) |
87 | } | 87 | } |
88 | PathKind::Self_ => { | 88 | PathKind::Self_ => { |
89 | PerNs::types(ModuleId { krate: self.krate, module_id: original_module }.into()) | 89 | PerNs::types(ModuleId { krate: self.krate, local_id: original_module }.into()) |
90 | } | 90 | } |
91 | // plain import or absolute path in 2015: crate-relative with | 91 | // plain import or absolute path in 2015: crate-relative with |
92 | // fallback to extern prelude (with the simplification in | 92 | // fallback to extern prelude (with the simplification in |
@@ -113,7 +113,7 @@ impl CrateDefMap { | |||
113 | } | 113 | } |
114 | PathKind::Super => { | 114 | PathKind::Super => { |
115 | if let Some(p) = self.modules[original_module].parent { | 115 | if let Some(p) = self.modules[original_module].parent { |
116 | PerNs::types(ModuleId { krate: self.krate, module_id: p }.into()) | 116 | PerNs::types(ModuleId { krate: self.krate, local_id: p }.into()) |
117 | } else { | 117 | } else { |
118 | log::debug!("super path in root module"); | 118 | log::debug!("super path in root module"); |
119 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); | 119 | return ResolvePathResult::empty(ReachedFixedPoint::Yes); |
@@ -160,7 +160,7 @@ impl CrateDefMap { | |||
160 | Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; | 160 | Path { segments: path.segments[i..].to_vec(), kind: PathKind::Self_ }; |
161 | log::debug!("resolving {:?} in other crate", path); | 161 | log::debug!("resolving {:?} in other crate", path); |
162 | let defp_map = db.crate_def_map(module.krate); | 162 | let defp_map = db.crate_def_map(module.krate); |
163 | let (def, s) = defp_map.resolve_path(db, module.module_id, &path); | 163 | let (def, s) = defp_map.resolve_path(db, module.local_id, &path); |
164 | return ResolvePathResult::with( | 164 | return ResolvePathResult::with( |
165 | def, | 165 | def, |
166 | ReachedFixedPoint::Yes, | 166 | ReachedFixedPoint::Yes, |
@@ -169,7 +169,7 @@ impl CrateDefMap { | |||
169 | } | 169 | } |
170 | 170 | ||
171 | // Since it is a qualified path here, it should not contains legacy macros | 171 | // Since it is a qualified path here, it should not contains legacy macros |
172 | match self[module.module_id].scope.get(&segment.name) { | 172 | match self[module.local_id].scope.get(&segment.name) { |
173 | Some(res) => res.def, | 173 | Some(res) => res.def, |
174 | _ => { | 174 | _ => { |
175 | log::debug!("path segment {:?} not found", segment.name); | 175 | log::debug!("path segment {:?} not found", segment.name); |
@@ -254,7 +254,7 @@ impl CrateDefMap { | |||
254 | keep = db.crate_def_map(prelude.krate); | 254 | keep = db.crate_def_map(prelude.krate); |
255 | &keep | 255 | &keep |
256 | }; | 256 | }; |
257 | def_map[prelude.module_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) | 257 | def_map[prelude.local_id].scope.get(name).map_or_else(PerNs::none, |res| res.def) |
258 | } else { | 258 | } else { |
259 | PerNs::none() | 259 | PerNs::none() |
260 | } | 260 | } |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 401af031c..6eb106094 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -176,7 +176,7 @@ pub(super) struct DefData { | |||
176 | pub(super) enum DefKind { | 176 | pub(super) enum DefKind { |
177 | Function(FileAstId<ast::FnDef>), | 177 | Function(FileAstId<ast::FnDef>), |
178 | Struct(FileAstId<ast::StructDef>), | 178 | Struct(FileAstId<ast::StructDef>), |
179 | Union(FileAstId<ast::StructDef>), | 179 | Union(FileAstId<ast::UnionDef>), |
180 | Enum(FileAstId<ast::EnumDef>), | 180 | Enum(FileAstId<ast::EnumDef>), |
181 | Const(FileAstId<ast::ConstDef>), | 181 | Const(FileAstId<ast::ConstDef>), |
182 | Static(FileAstId<ast::StaticDef>), | 182 | Static(FileAstId<ast::StaticDef>), |
@@ -246,11 +246,12 @@ impl RawItemsCollector { | |||
246 | ast::ModuleItem::StructDef(it) => { | 246 | ast::ModuleItem::StructDef(it) => { |
247 | let id = self.source_ast_id_map.ast_id(&it); | 247 | let id = self.source_ast_id_map.ast_id(&it); |
248 | let name = it.name(); | 248 | let name = it.name(); |
249 | if it.is_union() { | 249 | (DefKind::Struct(id), name) |
250 | (DefKind::Union(id), name) | 250 | } |
251 | } else { | 251 | ast::ModuleItem::UnionDef(it) => { |
252 | (DefKind::Struct(id), name) | 252 | let id = self.source_ast_id_map.ast_id(&it); |
253 | } | 253 | let name = it.name(); |
254 | (DefKind::Union(id), name) | ||
254 | } | 255 | } |
255 | ast::ModuleItem::EnumDef(it) => { | 256 | ast::ModuleItem::EnumDef(it) => { |
256 | (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) | 257 | (DefKind::Enum(self.source_ast_id_map.ast_id(&it)), it.name()) |
diff --git a/crates/ra_hir_def/src/nameres/tests.rs b/crates/ra_hir_def/src/nameres/tests.rs index f502f1cb3..87fcd617c 100644 --- a/crates/ra_hir_def/src/nameres/tests.rs +++ b/crates/ra_hir_def/src/nameres/tests.rs | |||
@@ -82,6 +82,12 @@ fn crate_def_map_smoke_test() { | |||
82 | 82 | ||
83 | //- /foo/bar.rs | 83 | //- /foo/bar.rs |
84 | pub struct Baz; | 84 | pub struct Baz; |
85 | |||
86 | union U { | ||
87 | to_be: bool, | ||
88 | not_to_be: u8, | ||
89 | } | ||
90 | |||
85 | enum E { V } | 91 | enum E { V } |
86 | ", | 92 | ", |
87 | ); | 93 | ); |
@@ -99,6 +105,7 @@ fn crate_def_map_smoke_test() { | |||
99 | ⋮crate::foo::bar | 105 | ⋮crate::foo::bar |
100 | ⋮Baz: t v | 106 | ⋮Baz: t v |
101 | ⋮E: t | 107 | ⋮E: t |
108 | ⋮U: t v | ||
102 | "###) | 109 | "###) |
103 | } | 110 | } |
104 | 111 | ||
diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 0e606fd0e..6810a26db 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs | |||
@@ -97,7 +97,7 @@ impl Path { | |||
97 | 97 | ||
98 | /// Converts an `ast::Path` to `Path`. Works with use trees. | 98 | /// Converts an `ast::Path` to `Path`. Works with use trees. |
99 | /// It correctly handles `$crate` based path from macro call. | 99 | /// It correctly handles `$crate` based path from macro call. |
100 | pub(crate) fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> { | 100 | pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> { |
101 | let mut kind = PathKind::Plain; | 101 | let mut kind = PathKind::Plain; |
102 | let mut segments = Vec::new(); | 102 | let mut segments = Vec::new(); |
103 | loop { | 103 | loop { |
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 95b3c926d..0847f6dcf 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -61,6 +61,8 @@ pub enum TypeNs { | |||
61 | GenericParam(u32), | 61 | GenericParam(u32), |
62 | AdtId(AdtId), | 62 | AdtId(AdtId), |
63 | AdtSelfType(AdtId), | 63 | AdtSelfType(AdtId), |
64 | // Yup, enum variants are added to the types ns, but any usage of variant as | ||
65 | // type is an error. | ||
64 | EnumVariantId(EnumVariantId), | 66 | EnumVariantId(EnumVariantId), |
65 | TypeAliasId(TypeAliasId), | 67 | TypeAliasId(TypeAliasId), |
66 | BuiltinType(BuiltinType), | 68 | BuiltinType(BuiltinType), |
@@ -323,7 +325,7 @@ impl Resolver { | |||
323 | if let Scope::ModuleScope(m) = scope { | 325 | if let Scope::ModuleScope(m) = scope { |
324 | if let Some(prelude) = m.crate_def_map.prelude { | 326 | if let Some(prelude) = m.crate_def_map.prelude { |
325 | let prelude_def_map = db.crate_def_map(prelude.krate); | 327 | let prelude_def_map = db.crate_def_map(prelude.krate); |
326 | traits.extend(prelude_def_map[prelude.module_id].scope.traits()); | 328 | traits.extend(prelude_def_map[prelude.local_id].scope.traits()); |
327 | } | 329 | } |
328 | traits.extend(m.crate_def_map[m.module_id].scope.traits()); | 330 | traits.extend(m.crate_def_map[m.module_id].scope.traits()); |
329 | } | 331 | } |
@@ -400,7 +402,7 @@ impl Scope { | |||
400 | }); | 402 | }); |
401 | if let Some(prelude) = m.crate_def_map.prelude { | 403 | if let Some(prelude) = m.crate_def_map.prelude { |
402 | let prelude_def_map = db.crate_def_map(prelude.krate); | 404 | let prelude_def_map = db.crate_def_map(prelude.krate); |
403 | prelude_def_map[prelude.module_id].scope.entries().for_each(|(name, res)| { | 405 | prelude_def_map[prelude.local_id].scope.entries().for_each(|(name, res)| { |
404 | f(name.clone(), ScopeDef::PerNs(res.def)); | 406 | f(name.clone(), ScopeDef::PerNs(res.def)); |
405 | }); | 407 | }); |
406 | } | 408 | } |
@@ -482,7 +484,7 @@ impl Resolver { | |||
482 | } | 484 | } |
483 | } | 485 | } |
484 | 486 | ||
485 | pub trait HasResolver { | 487 | pub trait HasResolver: Copy { |
486 | /// Builds a resolver for type references inside this def. | 488 | /// Builds a resolver for type references inside this def. |
487 | fn resolver(self, db: &impl DefDatabase) -> Resolver; | 489 | fn resolver(self, db: &impl DefDatabase) -> Resolver; |
488 | } | 490 | } |
@@ -490,7 +492,7 @@ pub trait HasResolver { | |||
490 | impl HasResolver for ModuleId { | 492 | impl HasResolver for ModuleId { |
491 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | 493 | fn resolver(self, db: &impl DefDatabase) -> Resolver { |
492 | let def_map = db.crate_def_map(self.krate); | 494 | let def_map = db.crate_def_map(self.krate); |
493 | Resolver::default().push_module_scope(def_map, self.module_id) | 495 | Resolver::default().push_module_scope(def_map, self.local_id) |
494 | } | 496 | } |
495 | } | 497 | } |
496 | 498 | ||
@@ -500,7 +502,7 @@ impl HasResolver for TraitId { | |||
500 | } | 502 | } |
501 | } | 503 | } |
502 | 504 | ||
503 | impl<T: Into<AdtId>> HasResolver for T { | 505 | impl<T: Into<AdtId> + Copy> HasResolver for T { |
504 | fn resolver(self, db: &impl DefDatabase) -> Resolver { | 506 | fn resolver(self, db: &impl DefDatabase) -> Resolver { |
505 | let def = self.into(); | 507 | let def = self.into(); |
506 | def.module(db) | 508 | def.module(db) |
diff --git a/crates/ra_hir_def/src/test_db.rs b/crates/ra_hir_def/src/test_db.rs index 439e8a412..54e3a84bd 100644 --- a/crates/ra_hir_def/src/test_db.rs +++ b/crates/ra_hir_def/src/test_db.rs | |||
@@ -24,7 +24,9 @@ impl salsa::Database for TestDB { | |||
24 | fn salsa_runtime(&self) -> &salsa::Runtime<Self> { | 24 | fn salsa_runtime(&self) -> &salsa::Runtime<Self> { |
25 | &self.runtime | 25 | &self.runtime |
26 | } | 26 | } |
27 | 27 | fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> { | |
28 | &mut self.runtime | ||
29 | } | ||
28 | fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) { | 30 | fn salsa_event(&self, event: impl Fn() -> salsa::Event<TestDB>) { |
29 | let mut events = self.events.lock().unwrap(); | 31 | let mut events = self.events.lock().unwrap(); |
30 | if let Some(events) = &mut *events { | 32 | if let Some(events) = &mut *events { |