diff options
author | Florian Diebold <[email protected]> | 2019-09-14 15:26:03 +0100 |
---|---|---|
committer | Florian Diebold <[email protected]> | 2019-09-17 18:47:45 +0100 |
commit | 828d60574f8ecbc33fe4987913c6f713e41af1ae (patch) | |
tree | cd51badc345665f6d1ea5eb09cb9a00ff4093b73 /crates | |
parent | 913ab1ec0ad10873134ca429c1496806a9261206 (diff) |
Refactor a bit to prepare for resolving trait assoc items
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 31 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/resolve.rs | 8 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 2 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 57 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 15 | ||||
-rw-r--r-- | crates/ra_ide_api/src/goto_definition.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/hover.rs | 6 | ||||
-rw-r--r-- | crates/ra_ide_api/src/name_ref_kind.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_api/src/syntax_highlighting.rs | 6 |
10 files changed, 83 insertions, 52 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 84e15385c..892208c1a 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -749,6 +749,10 @@ impl Const { | |||
749 | db.const_data(self) | 749 | db.const_data(self) |
750 | } | 750 | } |
751 | 751 | ||
752 | pub fn name(&self, db: &impl HirDatabase) -> Option<Name> { | ||
753 | self.data(db).name().cloned() | ||
754 | } | ||
755 | |||
752 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { | 756 | pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> { |
753 | db.infer(self.into()) | 757 | db.infer(self.into()) |
754 | } | 758 | } |
@@ -1019,3 +1023,30 @@ impl Container { | |||
1019 | } | 1023 | } |
1020 | } | 1024 | } |
1021 | } | 1025 | } |
1026 | |||
1027 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
1028 | pub enum AssocItem { | ||
1029 | Function(Function), | ||
1030 | Const(Const), | ||
1031 | TypeAlias(TypeAlias), | ||
1032 | } | ||
1033 | |||
1034 | impl From<TraitItem> for AssocItem { | ||
1035 | fn from(t: TraitItem) -> Self { | ||
1036 | match t { | ||
1037 | TraitItem::Function(f) => AssocItem::Function(f), | ||
1038 | TraitItem::Const(c) => AssocItem::Const(c), | ||
1039 | TraitItem::TypeAlias(t) => AssocItem::TypeAlias(t), | ||
1040 | } | ||
1041 | } | ||
1042 | } | ||
1043 | |||
1044 | impl From<crate::ImplItem> for AssocItem { | ||
1045 | fn from(i: crate::ImplItem) -> Self { | ||
1046 | match i { | ||
1047 | crate::ImplItem::Method(f) => AssocItem::Function(f), | ||
1048 | crate::ImplItem::Const(c) => AssocItem::Const(c), | ||
1049 | crate::ImplItem::TypeAlias(t) => AssocItem::TypeAlias(t), | ||
1050 | } | ||
1051 | } | ||
1052 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 80cf8d9c0..db82a463c 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -85,7 +85,7 @@ pub use self::{ | |||
85 | pub use self::code_model::{ | 85 | pub use self::code_model::{ |
86 | docs::{DocDef, Docs, Documentation}, | 86 | docs::{DocDef, Docs, Documentation}, |
87 | src::{HasBodySource, HasSource, Source}, | 87 | src::{HasBodySource, HasSource, Source}, |
88 | Adt, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, Enum, | 88 | Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, |
89 | EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, ModuleSource, | 89 | Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, |
90 | Static, Struct, StructField, Trait, TypeAlias, Union, | 90 | ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, |
91 | }; | 91 | }; |
diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs index bb6915901..7f4c78859 100644 --- a/crates/ra_hir/src/resolve.rs +++ b/crates/ra_hir/src/resolve.rs | |||
@@ -50,7 +50,7 @@ pub(crate) enum Scope { | |||
50 | ExprScope(ExprScope), | 50 | ExprScope(ExprScope), |
51 | } | 51 | } |
52 | 52 | ||
53 | #[derive(Debug, Clone, PartialEq, Eq)] | 53 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
54 | pub enum TypeNs { | 54 | pub enum TypeNs { |
55 | SelfType(ImplBlock), | 55 | SelfType(ImplBlock), |
56 | GenericParam(u32), | 56 | GenericParam(u32), |
@@ -59,19 +59,19 @@ pub enum TypeNs { | |||
59 | TypeAlias(TypeAlias), | 59 | TypeAlias(TypeAlias), |
60 | BuiltinType(BuiltinType), | 60 | BuiltinType(BuiltinType), |
61 | Trait(Trait), | 61 | Trait(Trait), |
62 | // Module belong to type ns, but the resovler is used when all module paths | 62 | // Module belong to type ns, but the resolver is used when all module paths |
63 | // are fully resolved. | 63 | // are fully resolved. |
64 | // Module(Module) | 64 | // Module(Module) |
65 | } | 65 | } |
66 | 66 | ||
67 | #[derive(Debug)] | 67 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
68 | pub enum ResolveValueResult<'a> { | 68 | pub enum ResolveValueResult<'a> { |
69 | ValueNs(ValueNs), | 69 | ValueNs(ValueNs), |
70 | Partial(TypeNs, usize), | 70 | Partial(TypeNs, usize), |
71 | TypeRef(&'a TypeRef), | 71 | TypeRef(&'a TypeRef), |
72 | } | 72 | } |
73 | 73 | ||
74 | #[derive(Debug)] | 74 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
75 | pub enum ValueNs { | 75 | pub enum ValueNs { |
76 | LocalBinding(PatId), | 76 | LocalBinding(PatId), |
77 | Function(Function), | 77 | Function(Function), |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 2a907c9f1..4d895f0a1 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -190,7 +190,7 @@ pub enum PathResolution { | |||
190 | GenericParam(u32), | 190 | GenericParam(u32), |
191 | SelfType(crate::ImplBlock), | 191 | SelfType(crate::ImplBlock), |
192 | Macro(MacroDef), | 192 | Macro(MacroDef), |
193 | AssocItem(crate::ImplItem), | 193 | AssocItem(crate::AssocItem), |
194 | } | 194 | } |
195 | 195 | ||
196 | #[derive(Debug, Clone, PartialEq, Eq)] | 196 | #[derive(Debug, Clone, PartialEq, Eq)] |
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index bf9609d8c..6aaf61c0e 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -48,7 +48,7 @@ use crate::{ | |||
48 | resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, | 48 | resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs}, |
49 | ty::infer::diagnostics::InferenceDiagnostic, | 49 | ty::infer::diagnostics::InferenceDiagnostic, |
50 | type_ref::{Mutability, TypeRef}, | 50 | type_ref::{Mutability, TypeRef}, |
51 | Adt, ConstData, DefWithBody, Either, FnData, Function, HasBody, ImplItem, Name, Path, | 51 | Adt, AssocItem, ConstData, DefWithBody, Either, FnData, Function, HasBody, ImplItem, Name, Path, |
52 | StructField, | 52 | StructField, |
53 | }; | 53 | }; |
54 | 54 | ||
@@ -121,7 +121,7 @@ pub struct InferenceResult { | |||
121 | /// For each struct literal, records the variant it resolves to. | 121 | /// For each struct literal, records the variant it resolves to. |
122 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, | 122 | variant_resolutions: FxHashMap<ExprOrPatId, VariantDef>, |
123 | /// For each associated item record what it resolves to | 123 | /// For each associated item record what it resolves to |
124 | assoc_resolutions: FxHashMap<ExprOrPatId, ImplItem>, | 124 | assoc_resolutions: FxHashMap<ExprOrPatId, AssocItem>, |
125 | diagnostics: Vec<InferenceDiagnostic>, | 125 | diagnostics: Vec<InferenceDiagnostic>, |
126 | pub(super) type_of_expr: ArenaMap<ExprId, Ty>, | 126 | pub(super) type_of_expr: ArenaMap<ExprId, Ty>, |
127 | pub(super) type_of_pat: ArenaMap<PatId, Ty>, | 127 | pub(super) type_of_pat: ArenaMap<PatId, Ty>, |
@@ -141,10 +141,10 @@ impl InferenceResult { | |||
141 | pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> { | 141 | pub fn variant_resolution_for_pat(&self, id: PatId) -> Option<VariantDef> { |
142 | self.variant_resolutions.get(&id.into()).copied() | 142 | self.variant_resolutions.get(&id.into()).copied() |
143 | } | 143 | } |
144 | pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<ImplItem> { | 144 | pub fn assoc_resolutions_for_expr(&self, id: ExprId) -> Option<AssocItem> { |
145 | self.assoc_resolutions.get(&id.into()).copied() | 145 | self.assoc_resolutions.get(&id.into()).copied() |
146 | } | 146 | } |
147 | pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<ImplItem> { | 147 | pub fn assoc_resolutions_for_pat(&self, id: PatId) -> Option<AssocItem> { |
148 | self.assoc_resolutions.get(&id.into()).copied() | 148 | self.assoc_resolutions.get(&id.into()).copied() |
149 | } | 149 | } |
150 | pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> { | 150 | pub fn type_mismatch_for_expr(&self, expr: ExprId) -> Option<&TypeMismatch> { |
@@ -235,7 +235,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
235 | self.result.variant_resolutions.insert(id, variant); | 235 | self.result.variant_resolutions.insert(id, variant); |
236 | } | 236 | } |
237 | 237 | ||
238 | fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: ImplItem) { | 238 | fn write_assoc_resolution(&mut self, id: ExprOrPatId, item: AssocItem) { |
239 | self.result.assoc_resolutions.insert(id, item); | 239 | self.result.assoc_resolutions.insert(id, item); |
240 | } | 240 | } |
241 | 241 | ||
@@ -560,8 +560,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
560 | let ty = mem::replace(&mut ty, Ty::Unknown); | 560 | let ty = mem::replace(&mut ty, Ty::Unknown); |
561 | def_or_ty = ty.iterate_impl_items(self.db, krate, |item| { | 561 | def_or_ty = ty.iterate_impl_items(self.db, krate, |item| { |
562 | match item { | 562 | match item { |
563 | crate::ImplItem::Method(_) => None, | 563 | crate::ImplItem::Method(_) | crate::ImplItem::Const(_) => None, |
564 | crate::ImplItem::Const(_) => None, | ||
565 | 564 | ||
566 | // FIXME: Resolve associated types | 565 | // FIXME: Resolve associated types |
567 | crate::ImplItem::TypeAlias(_) => { | 566 | crate::ImplItem::TypeAlias(_) => { |
@@ -573,34 +572,32 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
573 | } | 572 | } |
574 | 573 | ||
575 | let segment = path.segments.last().unwrap(); | 574 | let segment = path.segments.last().unwrap(); |
576 | let def = ty.clone().iterate_impl_items(self.db, krate, |item| { | 575 | let def = ty.clone().iterate_impl_items(self.db, krate, |item| match item { |
577 | let matching_def: Option<ValueNs> = match item { | 576 | crate::ImplItem::Method(func) => { |
578 | crate::ImplItem::Method(func) => { | 577 | if segment.name == func.name(self.db) { |
579 | if segment.name == func.name(self.db) { | 578 | Some(ValueNs::Function(func)) |
580 | Some(ValueNs::Function(func)) | 579 | } else { |
581 | } else { | 580 | None |
582 | None | ||
583 | } | ||
584 | } | 581 | } |
582 | } | ||
585 | 583 | ||
586 | crate::ImplItem::Const(konst) => { | 584 | crate::ImplItem::Const(konst) => { |
587 | let data = konst.data(self.db); | 585 | if konst.name(self.db).map_or(false, |n| n == segment.name) { |
588 | if Some(&segment.name) == data.name() { | 586 | Some(ValueNs::Const(konst)) |
589 | Some(ValueNs::Const(konst)) | 587 | } else { |
590 | } else { | 588 | None |
591 | None | ||
592 | } | ||
593 | } | ||
594 | crate::ImplItem::TypeAlias(_) => None, | ||
595 | }; | ||
596 | match matching_def { | ||
597 | Some(_) => { | ||
598 | self.write_assoc_resolution(id, item); | ||
599 | matching_def | ||
600 | } | 589 | } |
601 | None => None, | ||
602 | } | 590 | } |
591 | crate::ImplItem::TypeAlias(_) => None, | ||
603 | })?; | 592 | })?; |
593 | self.write_assoc_resolution( | ||
594 | id, | ||
595 | match def { | ||
596 | ValueNs::Function(f) => AssocItem::Function(f), | ||
597 | ValueNs::Const(c) => AssocItem::Const(c), | ||
598 | _ => unreachable!(), | ||
599 | }, | ||
600 | ); | ||
604 | let self_types = self.find_self_types(&def, ty); | 601 | let self_types = self.find_self_types(&def, ty); |
605 | Some((def, self_types)) | 602 | Some((def, self_types)) |
606 | } | 603 | } |
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 03382ab3c..11f73ccfd 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::{FieldSource, HasSource, ImplItem, ModuleSource}; | 1 | use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; |
2 | use ra_db::{FileId, SourceDatabase}; | 2 | use ra_db::{FileId, SourceDatabase}; |
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | algo::visit::{visitor, Visitor}, | 4 | algo::visit::{visitor, Visitor}, |
@@ -221,11 +221,14 @@ impl NavigationTarget { | |||
221 | ) | 221 | ) |
222 | } | 222 | } |
223 | 223 | ||
224 | pub(crate) fn from_impl_item(db: &RootDatabase, impl_item: hir::ImplItem) -> NavigationTarget { | 224 | pub(crate) fn from_assoc_item( |
225 | match impl_item { | 225 | db: &RootDatabase, |
226 | ImplItem::Method(it) => NavigationTarget::from_def_source(db, it), | 226 | assoc_item: hir::AssocItem, |
227 | ImplItem::Const(it) => NavigationTarget::from_def_source(db, it), | 227 | ) -> NavigationTarget { |
228 | ImplItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it), | 228 | match assoc_item { |
229 | AssocItem::Function(it) => NavigationTarget::from_def_source(db, it), | ||
230 | AssocItem::Const(it) => NavigationTarget::from_def_source(db, it), | ||
231 | AssocItem::TypeAlias(it) => NavigationTarget::from_def_source(db, it), | ||
229 | } | 232 | } |
230 | } | 233 | } |
231 | 234 | ||
diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 28529a2de..503dcacff 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs | |||
@@ -60,7 +60,7 @@ pub(crate) fn reference_definition( | |||
60 | match classify_name_ref(db, &analyzer, name_ref) { | 60 | match classify_name_ref(db, &analyzer, name_ref) { |
61 | Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), | 61 | Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)), |
62 | Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), | 62 | Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)), |
63 | Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_impl_item(db, assoc)), | 63 | Some(AssocItem(assoc)) => return Exact(NavigationTarget::from_assoc_item(db, assoc)), |
64 | Some(Method(func)) => return Exact(NavigationTarget::from_def_source(db, func)), | 64 | Some(Method(func)) => return Exact(NavigationTarget::from_def_source(db, func)), |
65 | Some(Def(def)) => match NavigationTarget::from_def(db, def) { | 65 | Some(Def(def)) => match NavigationTarget::from_def(db, def) { |
66 | Some(nav) => return Exact(nav), | 66 | Some(nav) => return Exact(nav), |
diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 28a6bef12..655bcdb16 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs | |||
@@ -117,9 +117,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn | |||
117 | } | 117 | } |
118 | } | 118 | } |
119 | Some(AssocItem(it)) => res.extend(match it { | 119 | Some(AssocItem(it)) => res.extend(match it { |
120 | hir::ImplItem::Method(it) => from_def_source(db, it), | 120 | hir::AssocItem::Function(it) => from_def_source(db, it), |
121 | hir::ImplItem::Const(it) => from_def_source(db, it), | 121 | hir::AssocItem::Const(it) => from_def_source(db, it), |
122 | hir::ImplItem::TypeAlias(it) => from_def_source(db, it), | 122 | hir::AssocItem::TypeAlias(it) => from_def_source(db, it), |
123 | }), | 123 | }), |
124 | Some(Def(it)) => { | 124 | Some(Def(it)) => { |
125 | match it { | 125 | match it { |
diff --git a/crates/ra_ide_api/src/name_ref_kind.rs b/crates/ra_ide_api/src/name_ref_kind.rs index 6c2a7b260..aff03464a 100644 --- a/crates/ra_ide_api/src/name_ref_kind.rs +++ b/crates/ra_ide_api/src/name_ref_kind.rs | |||
@@ -8,7 +8,7 @@ pub enum NameRefKind { | |||
8 | Method(hir::Function), | 8 | Method(hir::Function), |
9 | Macro(hir::MacroDef), | 9 | Macro(hir::MacroDef), |
10 | FieldAccess(hir::StructField), | 10 | FieldAccess(hir::StructField), |
11 | AssocItem(hir::ImplItem), | 11 | AssocItem(hir::AssocItem), |
12 | Def(hir::ModuleDef), | 12 | Def(hir::ModuleDef), |
13 | SelfType(hir::Ty), | 13 | SelfType(hir::Ty), |
14 | Pat(AstPtr<ast::BindPat>), | 14 | Pat(AstPtr<ast::BindPat>), |
diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 86ab3a260..3d7f91c1d 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs | |||
@@ -102,9 +102,9 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa | |||
102 | Some(Method(_)) => "function", | 102 | Some(Method(_)) => "function", |
103 | Some(Macro(_)) => "macro", | 103 | Some(Macro(_)) => "macro", |
104 | Some(FieldAccess(_)) => "field", | 104 | Some(FieldAccess(_)) => "field", |
105 | Some(AssocItem(hir::ImplItem::Method(_))) => "function", | 105 | Some(AssocItem(hir::AssocItem::Function(_))) => "function", |
106 | Some(AssocItem(hir::ImplItem::Const(_))) => "constant", | 106 | Some(AssocItem(hir::AssocItem::Const(_))) => "constant", |
107 | Some(AssocItem(hir::ImplItem::TypeAlias(_))) => "type", | 107 | Some(AssocItem(hir::AssocItem::TypeAlias(_))) => "type", |
108 | Some(Def(hir::ModuleDef::Module(_))) => "module", | 108 | Some(Def(hir::ModuleDef::Module(_))) => "module", |
109 | Some(Def(hir::ModuleDef::Function(_))) => "function", | 109 | Some(Def(hir::ModuleDef::Function(_))) => "function", |
110 | Some(Def(hir::ModuleDef::Adt(_))) => "type", | 110 | Some(Def(hir::ModuleDef::Adt(_))) => "type", |