diff options
author | Ville Penttinen <[email protected]> | 2019-02-21 21:57:07 +0000 |
---|---|---|
committer | Ville Penttinen <[email protected]> | 2019-02-21 21:57:07 +0000 |
commit | 2e7bc905be156a007a4ee8f1a1bd1d73a1d19ade (patch) | |
tree | 2159410676f8fcddfd8e23b0fbc0fcb759c6ce7a /crates/ra_hir/src/ty.rs | |
parent | 816971ebc9207c5fb5779d448613dd171c27f398 (diff) |
Remove Const inference for now, refactor PathResult
Diffstat (limited to 'crates/ra_hir/src/ty.rs')
-rw-r--r-- | crates/ra_hir/src/ty.rs | 55 |
1 files changed, 19 insertions, 36 deletions
diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 2751ab3ab..b1f35ab1f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs | |||
@@ -32,20 +32,17 @@ use rustc_hash::FxHashMap; | |||
32 | 32 | ||
33 | use test_utils::tested_by; | 33 | use test_utils::tested_by; |
34 | 34 | ||
35 | use ra_syntax::ast::NameOwner; | ||
36 | |||
37 | use crate::{ | 35 | use crate::{ |
38 | Function, Struct, StructField, Enum, EnumVariant, Path, Name, | 36 | Function, Struct, StructField, Enum, EnumVariant, Path, Name, |
39 | Const, | ||
40 | FnSignature, ModuleDef, AdtDef, | 37 | FnSignature, ModuleDef, AdtDef, |
41 | HirDatabase, | 38 | HirDatabase, |
42 | type_ref::{TypeRef, Mutability}, | 39 | type_ref::{TypeRef, Mutability}, |
43 | name::{KnownName, AsName}, | 40 | name::{KnownName}, |
44 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, | 41 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat, self}, |
45 | generics::GenericParams, | 42 | generics::GenericParams, |
46 | path::GenericArg, | 43 | path::GenericArg, |
47 | adt::VariantDef, | 44 | adt::VariantDef, |
48 | resolve::{Resolver, Resolution, PathResult}, nameres::Namespace | 45 | resolve::{Resolver, Resolution}, nameres::Namespace |
49 | }; | 46 | }; |
50 | 47 | ||
51 | /// The ID of a type variable. | 48 | /// The ID of a type variable. |
@@ -681,19 +678,6 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty { | |||
681 | Ty::FnDef { def: def.into(), sig, name, substs } | 678 | Ty::FnDef { def: def.into(), sig, name, substs } |
682 | } | 679 | } |
683 | 680 | ||
684 | fn type_for_const(db: &impl HirDatabase, resolver: &Resolver, def: Const) -> Ty { | ||
685 | let node = def.source(db).1; | ||
686 | |||
687 | let tr = node | ||
688 | .type_ref() | ||
689 | .map(TypeRef::from_ast) | ||
690 | .as_ref() | ||
691 | .map(|tr| Ty::from_hir(db, resolver, tr)) | ||
692 | .unwrap_or_else(|| Ty::Unknown); | ||
693 | |||
694 | tr | ||
695 | } | ||
696 | |||
697 | /// Compute the type of a tuple struct constructor. | 681 | /// Compute the type of a tuple struct constructor. |
698 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { | 682 | fn type_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> Ty { |
699 | let var_data = def.variant_data(db); | 683 | let var_data = def.variant_data(db); |
@@ -1190,21 +1174,28 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1190 | fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option<Ty> { | 1174 | fn infer_path_expr(&mut self, resolver: &Resolver, path: &Path) -> Option<Ty> { |
1191 | let resolved = resolver.resolve_path(self.db, &path); | 1175 | let resolved = resolver.resolve_path(self.db, &path); |
1192 | 1176 | ||
1193 | let (resolved, segment_index) = match resolved { | 1177 | let (def, remaining_index) = resolved.into_inner(); |
1194 | PathResult::FullyResolved(def) => (def.take_values()?, None), | 1178 | |
1195 | PathResult::PartiallyResolved(def, index) => (def.take_types()?, Some(index)), | 1179 | // if the remaining_index is None, we expect the path |
1196 | }; | 1180 | // to be fully resolved, in this case we continue with |
1181 | // the default by attempting to `take_values´ from the resolution. | ||
1182 | // Otherwise the path was partially resolved, which means | ||
1183 | // we might have resolved into a type for which | ||
1184 | // we may find some associated item starting at the | ||
1185 | // path.segment pointed to by `remaining_index´ | ||
1186 | let resolved = | ||
1187 | if remaining_index.is_none() { def.take_values()? } else { def.take_types()? }; | ||
1197 | 1188 | ||
1198 | match resolved { | 1189 | match resolved { |
1199 | Resolution::Def(def) => { | 1190 | Resolution::Def(def) => { |
1200 | let typable: Option<TypableDef> = def.into(); | 1191 | let typable: Option<TypableDef> = def.into(); |
1201 | let typable = typable?; | 1192 | let typable = typable?; |
1202 | 1193 | ||
1203 | if let Some(segment_index) = segment_index { | 1194 | if let Some(remaining_index) = remaining_index { |
1204 | let ty = self.db.type_for_def(typable, Namespace::Types); | 1195 | let ty = self.db.type_for_def(typable, Namespace::Types); |
1205 | // TODO: What to do if segment_index is not the last segment | 1196 | // TODO: Keep resolving the segments |
1206 | // in the path | 1197 | // if we have more segments to process |
1207 | let segment = &path.segments[segment_index]; | 1198 | let segment = &path.segments[remaining_index]; |
1208 | 1199 | ||
1209 | // Attempt to find an impl_item for the type which has a name matching | 1200 | // Attempt to find an impl_item for the type which has a name matching |
1210 | // the current segment | 1201 | // the current segment |
@@ -1216,17 +1207,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
1216 | } | 1207 | } |
1217 | None | 1208 | None |
1218 | } | 1209 | } |
1219 | crate::ImplItem::Const(c) => { | ||
1220 | let node = c.source(self.db).1; | ||
1221 | |||
1222 | if let Some(name) = node.name().map(|n| n.as_name()) { | ||
1223 | if segment.name == name { | ||
1224 | return Some(type_for_const(self.db, resolver, c)); | ||
1225 | } | ||
1226 | } | ||
1227 | 1210 | ||
1228 | None | 1211 | // TODO: Resolve associated const |
1229 | } | 1212 | crate::ImplItem::Const(_) => None, |
1230 | 1213 | ||
1231 | // TODO: Resolve associated types | 1214 | // TODO: Resolve associated types |
1232 | crate::ImplItem::Type(_) => None, | 1215 | crate::ImplItem::Type(_) => None, |