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/ra_hir/src | |
parent | 913ab1ec0ad10873134ca429c1496806a9261206 (diff) |
Refactor a bit to prepare for resolving trait assoc items
Diffstat (limited to 'crates/ra_hir/src')
-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 |
5 files changed, 66 insertions, 38 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 | } |