diff options
author | Edwin Cheng <[email protected]> | 2020-05-04 17:17:22 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-05-04 17:17:22 +0100 |
commit | 8d96db3417fdabb1bad7a9919ea8ae1db25296f1 (patch) | |
tree | 66e1aa8c88596c9fffb97d6065ba11478248e189 | |
parent | 6a48a94d47bfd6a340cbdb3f68b5bce4167c8168 (diff) |
Favor types for Record in name resolution
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir_def/src/item_scope.rs | 11 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/collector.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir_def/src/nameres/raw.rs | 18 |
4 files changed, 45 insertions, 12 deletions
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index 687216dc3..49f5420d1 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -18,7 +18,7 @@ use ra_syntax::{ | |||
18 | use test_utils::tested_by; | 18 | use test_utils::tested_by; |
19 | 19 | ||
20 | use crate::{ | 20 | use crate::{ |
21 | adt::StructKind, | 21 | adt::{self, StructKind}, |
22 | body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, | 22 | body::{Body, BodySourceMap, Expander, PatPtr, SyntheticSyntax}, |
23 | builtin_type::{BuiltinFloat, BuiltinInt}, | 23 | builtin_type::{BuiltinFloat, BuiltinInt}, |
24 | db::DefDatabase, | 24 | db::DefDatabase, |
@@ -575,9 +575,16 @@ impl ExprCollector<'_> { | |||
575 | self.body.item_scope.define_def(def); | 575 | self.body.item_scope.define_def(def); |
576 | if let Some(name) = name { | 576 | if let Some(name) = name { |
577 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly | 577 | let vis = crate::visibility::Visibility::Public; // FIXME determine correctly |
578 | self.body | 578 | let favor_types = match def { |
579 | .item_scope | 579 | ModuleDefId::AdtId(AdtId::StructId(s)) => { |
580 | .push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis)); | 580 | self.db.struct_data(s).variant_data.kind() == adt::StructKind::Record |
581 | } | ||
582 | _ => false, | ||
583 | }; | ||
584 | self.body.item_scope.push_res( | ||
585 | name.as_name(), | ||
586 | crate::per_ns::PerNs::from_def(def, vis, favor_types), | ||
587 | ); | ||
581 | } | 588 | } |
582 | } | 589 | } |
583 | } | 590 | } |
diff --git a/crates/ra_hir_def/src/item_scope.rs b/crates/ra_hir_def/src/item_scope.rs index 259b9ff03..6d8d1f8a3 100644 --- a/crates/ra_hir_def/src/item_scope.rs +++ b/crates/ra_hir_def/src/item_scope.rs | |||
@@ -151,13 +151,20 @@ impl ItemScope { | |||
151 | } | 151 | } |
152 | 152 | ||
153 | impl PerNs { | 153 | impl PerNs { |
154 | pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs { | 154 | pub(crate) fn from_def(def: ModuleDefId, v: Visibility, favor_types: bool) -> PerNs { |
155 | match def { | 155 | match def { |
156 | ModuleDefId::ModuleId(_) => PerNs::types(def, v), | 156 | ModuleDefId::ModuleId(_) => PerNs::types(def, v), |
157 | ModuleDefId::FunctionId(_) => PerNs::values(def, v), | 157 | ModuleDefId::FunctionId(_) => PerNs::values(def, v), |
158 | ModuleDefId::AdtId(adt) => match adt { | 158 | ModuleDefId::AdtId(adt) => match adt { |
159 | AdtId::StructId(_) | AdtId::UnionId(_) => PerNs::both(def, def, v), | 159 | AdtId::UnionId(_) => PerNs::both(def, def, v), |
160 | AdtId::EnumId(_) => PerNs::types(def, v), | 160 | AdtId::EnumId(_) => PerNs::types(def, v), |
161 | AdtId::StructId(_) => { | ||
162 | if favor_types { | ||
163 | PerNs::types(def, v) | ||
164 | } else { | ||
165 | PerNs::both(def, def, v) | ||
166 | } | ||
167 | } | ||
161 | }, | 168 | }, |
162 | ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v), | 169 | ModuleDefId::EnumVariantId(_) => PerNs::both(def, def, v), |
163 | ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v), | 170 | ModuleDefId::ConstId(_) | ModuleDefId::StaticId(_) => PerNs::values(def, v), |
diff --git a/crates/ra_hir_def/src/nameres/collector.rs b/crates/ra_hir_def/src/nameres/collector.rs index bf3968bd6..49b33ca94 100644 --- a/crates/ra_hir_def/src/nameres/collector.rs +++ b/crates/ra_hir_def/src/nameres/collector.rs | |||
@@ -830,7 +830,7 @@ impl ModCollector<'_, '_> { | |||
830 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; | 830 | let module = ModuleId { krate: self.def_collector.def_map.krate, local_id: res }; |
831 | let def: ModuleDefId = module.into(); | 831 | let def: ModuleDefId = module.into(); |
832 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); | 832 | self.def_collector.def_map.modules[self.module_id].scope.define_def(def); |
833 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis); | 833 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis, false))], vis); |
834 | res | 834 | res |
835 | } | 835 | } |
836 | 836 | ||
@@ -844,6 +844,8 @@ impl ModCollector<'_, '_> { | |||
844 | let name = def.name.clone(); | 844 | let name = def.name.clone(); |
845 | let container = ContainerId::ModuleId(module); | 845 | let container = ContainerId::ModuleId(module); |
846 | let vis = &def.visibility; | 846 | let vis = &def.visibility; |
847 | let mut favor_types = false; | ||
848 | |||
847 | let def: ModuleDefId = match def.kind { | 849 | let def: ModuleDefId = match def.kind { |
848 | raw::DefKind::Function(ast_id) => FunctionLoc { | 850 | raw::DefKind::Function(ast_id) => FunctionLoc { |
849 | container: container.into(), | 851 | container: container.into(), |
@@ -851,7 +853,8 @@ impl ModCollector<'_, '_> { | |||
851 | } | 853 | } |
852 | .intern(self.def_collector.db) | 854 | .intern(self.def_collector.db) |
853 | .into(), | 855 | .into(), |
854 | raw::DefKind::Struct(ast_id) => { | 856 | raw::DefKind::Struct(ast_id, mode) => { |
857 | favor_types = mode == raw::StructDefKind::Record; | ||
855 | StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } | 858 | StructLoc { container, ast_id: AstId::new(self.file_id, ast_id) } |
856 | .intern(self.def_collector.db) | 859 | .intern(self.def_collector.db) |
857 | .into() | 860 | .into() |
@@ -894,7 +897,11 @@ impl ModCollector<'_, '_> { | |||
894 | .def_map | 897 | .def_map |
895 | .resolve_visibility(self.def_collector.db, self.module_id, vis) | 898 | .resolve_visibility(self.def_collector.db, self.module_id, vis) |
896 | .unwrap_or(Visibility::Public); | 899 | .unwrap_or(Visibility::Public); |
897 | self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis) | 900 | self.def_collector.update( |
901 | self.module_id, | ||
902 | &[(name, PerNs::from_def(def, vis, favor_types))], | ||
903 | vis, | ||
904 | ) | ||
898 | } | 905 | } |
899 | 906 | ||
900 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { | 907 | fn collect_derives(&mut self, attrs: &Attrs, def: &raw::DefData) { |
diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index a71503c76..f2716a295 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs | |||
@@ -156,9 +156,16 @@ pub(super) struct DefData { | |||
156 | } | 156 | } |
157 | 157 | ||
158 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | 158 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] |
159 | pub(super) enum StructDefKind { | ||
160 | Record, | ||
161 | Tuple, | ||
162 | Unit, | ||
163 | } | ||
164 | |||
165 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
159 | pub(super) enum DefKind { | 166 | pub(super) enum DefKind { |
160 | Function(FileAstId<ast::FnDef>), | 167 | Function(FileAstId<ast::FnDef>), |
161 | Struct(FileAstId<ast::StructDef>), | 168 | Struct(FileAstId<ast::StructDef>, StructDefKind), |
162 | Union(FileAstId<ast::UnionDef>), | 169 | Union(FileAstId<ast::UnionDef>), |
163 | Enum(FileAstId<ast::EnumDef>), | 170 | Enum(FileAstId<ast::EnumDef>), |
164 | Const(FileAstId<ast::ConstDef>), | 171 | Const(FileAstId<ast::ConstDef>), |
@@ -171,7 +178,7 @@ impl DefKind { | |||
171 | pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> { | 178 | pub fn ast_id(&self) -> FileAstId<ast::ModuleItem> { |
172 | match self { | 179 | match self { |
173 | DefKind::Function(it) => it.upcast(), | 180 | DefKind::Function(it) => it.upcast(), |
174 | DefKind::Struct(it) => it.upcast(), | 181 | DefKind::Struct(it, _) => it.upcast(), |
175 | DefKind::Union(it) => it.upcast(), | 182 | DefKind::Union(it) => it.upcast(), |
176 | DefKind::Enum(it) => it.upcast(), | 183 | DefKind::Enum(it) => it.upcast(), |
177 | DefKind::Const(it) => it.upcast(), | 184 | DefKind::Const(it) => it.upcast(), |
@@ -236,9 +243,14 @@ impl RawItemsCollector { | |||
236 | return; | 243 | return; |
237 | } | 244 | } |
238 | ast::ModuleItem::StructDef(it) => { | 245 | ast::ModuleItem::StructDef(it) => { |
246 | let kind = match it.kind() { | ||
247 | ast::StructKind::Record(_) => StructDefKind::Record, | ||
248 | ast::StructKind::Tuple(_) => StructDefKind::Tuple, | ||
249 | ast::StructKind::Unit => StructDefKind::Unit, | ||
250 | }; | ||
239 | let id = self.source_ast_id_map.ast_id(&it); | 251 | let id = self.source_ast_id_map.ast_id(&it); |
240 | let name = it.name(); | 252 | let name = it.name(); |
241 | (DefKind::Struct(id), name) | 253 | (DefKind::Struct(id, kind), name) |
242 | } | 254 | } |
243 | ast::ModuleItem::UnionDef(it) => { | 255 | ast::ModuleItem::UnionDef(it) => { |
244 | let id = self.source_ast_id_map.ast_id(&it); | 256 | let id = self.source_ast_id_map.ast_id(&it); |