aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/infer.rs')
-rw-r--r--crates/hir_ty/src/infer.rs49
1 files changed, 28 insertions, 21 deletions
diff --git a/crates/hir_ty/src/infer.rs b/crates/hir_ty/src/infer.rs
index 4d771a91e..acde99b04 100644
--- a/crates/hir_ty/src/infer.rs
+++ b/crates/hir_ty/src/infer.rs
@@ -41,7 +41,8 @@ use super::{
41 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, 41 InEnvironment, ProjectionTy, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk,
42}; 42};
43use crate::{ 43use crate::{
44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode, AliasTy, 44 db::HirDatabase, infer::diagnostics::InferenceDiagnostic, lower::ImplTraitLoweringMode,
45 AliasTy, Interner, TyKind,
45}; 46};
46 47
47pub(crate) use unify::unify; 48pub(crate) use unify::unify;
@@ -169,7 +170,7 @@ impl Index<ExprId> for InferenceResult {
169 type Output = Ty; 170 type Output = Ty;
170 171
171 fn index(&self, expr: ExprId) -> &Ty { 172 fn index(&self, expr: ExprId) -> &Ty {
172 self.type_of_expr.get(expr).unwrap_or(&Ty::Unknown) 173 self.type_of_expr.get(expr).unwrap_or(&Ty(TyKind::Unknown))
173 } 174 }
174} 175}
175 176
@@ -177,7 +178,7 @@ impl Index<PatId> for InferenceResult {
177 type Output = Ty; 178 type Output = Ty;
178 179
179 fn index(&self, pat: PatId) -> &Ty { 180 fn index(&self, pat: PatId) -> &Ty {
180 self.type_of_pat.get(pat).unwrap_or(&Ty::Unknown) 181 self.type_of_pat.get(pat).unwrap_or(&Ty(TyKind::Unknown))
181 } 182 }
182} 183}
183 184
@@ -226,7 +227,7 @@ impl<'a> InferenceContext<'a> {
226 result: InferenceResult::default(), 227 result: InferenceResult::default(),
227 table: unify::InferenceTable::new(), 228 table: unify::InferenceTable::new(),
228 obligations: Vec::default(), 229 obligations: Vec::default(),
229 return_ty: Ty::Unknown, // set in collect_fn_signature 230 return_ty: TyKind::Unknown.intern(&Interner), // set in collect_fn_signature
230 trait_env: TraitEnvironment::lower(db, &resolver), 231 trait_env: TraitEnvironment::lower(db, &resolver),
231 db, 232 db,
232 owner, 233 owner,
@@ -237,15 +238,19 @@ impl<'a> InferenceContext<'a> {
237 } 238 }
238 } 239 }
239 240
241 fn err_ty(&self) -> Ty {
242 TyKind::Unknown.intern(&Interner)
243 }
244
240 fn resolve_all(mut self) -> InferenceResult { 245 fn resolve_all(mut self) -> InferenceResult {
241 // FIXME resolve obligations as well (use Guidance if necessary) 246 // FIXME resolve obligations as well (use Guidance if necessary)
242 let mut result = std::mem::take(&mut self.result); 247 let mut result = std::mem::take(&mut self.result);
243 for ty in result.type_of_expr.values_mut() { 248 for ty in result.type_of_expr.values_mut() {
244 let resolved = self.table.resolve_ty_completely(mem::replace(ty, Ty::Unknown)); 249 let resolved = self.table.resolve_ty_completely(ty.clone());
245 *ty = resolved; 250 *ty = resolved;
246 } 251 }
247 for ty in result.type_of_pat.values_mut() { 252 for ty in result.type_of_pat.values_mut() {
248 let resolved = self.table.resolve_ty_completely(mem::replace(ty, Ty::Unknown)); 253 let resolved = self.table.resolve_ty_completely(ty.clone());
249 *ty = resolved; 254 *ty = resolved;
250 } 255 }
251 result 256 result
@@ -298,8 +303,8 @@ impl<'a> InferenceContext<'a> {
298 303
299 /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it. 304 /// Replaces Ty::Unknown by a new type var, so we can maybe still infer it.
300 fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty { 305 fn insert_type_vars_shallow(&mut self, ty: Ty) -> Ty {
301 match ty { 306 match ty.interned(&Interner) {
302 Ty::Unknown => self.table.new_type_var(), 307 TyKind::Unknown => self.table.new_type_var(),
303 _ => ty, 308 _ => ty,
304 } 309 }
305 } 310 }
@@ -383,7 +388,7 @@ impl<'a> InferenceContext<'a> {
383 self.obligations.push(Obligation::Projection(projection)); 388 self.obligations.push(Obligation::Projection(projection));
384 self.resolve_ty_as_possible(ty) 389 self.resolve_ty_as_possible(ty)
385 } 390 }
386 None => Ty::Unknown, 391 None => self.err_ty(),
387 } 392 }
388 } 393 }
389 394
@@ -395,8 +400,10 @@ impl<'a> InferenceContext<'a> {
395 /// to do it as well. 400 /// to do it as well.
396 fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty { 401 fn normalize_associated_types_in(&mut self, ty: Ty) -> Ty {
397 let ty = self.resolve_ty_as_possible(ty); 402 let ty = self.resolve_ty_as_possible(ty);
398 ty.fold(&mut |ty| match ty { 403 ty.fold(&mut |ty| match ty.interned(&Interner) {
399 Ty::Alias(AliasTy::Projection(proj_ty)) => self.normalize_projection_ty(proj_ty), 404 TyKind::Alias(AliasTy::Projection(proj_ty)) => {
405 self.normalize_projection_ty(proj_ty.clone())
406 }
400 _ => ty, 407 _ => ty,
401 }) 408 })
402 } 409 }
@@ -412,7 +419,7 @@ impl<'a> InferenceContext<'a> {
412 fn resolve_variant(&mut self, path: Option<&Path>) -> (Ty, Option<VariantId>) { 419 fn resolve_variant(&mut self, path: Option<&Path>) -> (Ty, Option<VariantId>) {
413 let path = match path { 420 let path = match path {
414 Some(path) => path, 421 Some(path) => path,
415 None => return (Ty::Unknown, None), 422 None => return (self.err_ty(), None),
416 }; 423 };
417 let resolver = &self.resolver; 424 let resolver = &self.resolver;
418 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver); 425 let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
@@ -421,7 +428,7 @@ impl<'a> InferenceContext<'a> {
421 let (resolution, unresolved) = 428 let (resolution, unresolved) =
422 match resolver.resolve_path_in_type_ns(self.db.upcast(), path.mod_path()) { 429 match resolver.resolve_path_in_type_ns(self.db.upcast(), path.mod_path()) {
423 Some(it) => it, 430 Some(it) => it,
424 None => return (Ty::Unknown, None), 431 None => return (self.err_ty(), None),
425 }; 432 };
426 return match resolution { 433 return match resolution {
427 TypeNs::AdtId(AdtId::StructId(strukt)) => { 434 TypeNs::AdtId(AdtId::StructId(strukt)) => {
@@ -462,11 +469,11 @@ impl<'a> InferenceContext<'a> {
462 } 469 }
463 } 470 }
464 // FIXME potentially resolve assoc type 471 // FIXME potentially resolve assoc type
465 (Ty::Unknown, None) 472 (self.err_ty(), None)
466 } 473 }
467 Some(_) => { 474 Some(_) => {
468 // FIXME diagnostic 475 // FIXME diagnostic
469 (Ty::Unknown, None) 476 (self.err_ty(), None)
470 } 477 }
471 } 478 }
472 } 479 }
@@ -480,15 +487,15 @@ impl<'a> InferenceContext<'a> {
480 } 487 }
481 TypeNs::AdtSelfType(_) => { 488 TypeNs::AdtSelfType(_) => {
482 // FIXME this could happen in array size expressions, once we're checking them 489 // FIXME this could happen in array size expressions, once we're checking them
483 (Ty::Unknown, None) 490 (self.err_ty(), None)
484 } 491 }
485 TypeNs::GenericParam(_) => { 492 TypeNs::GenericParam(_) => {
486 // FIXME potentially resolve assoc type 493 // FIXME potentially resolve assoc type
487 (Ty::Unknown, None) 494 (self.err_ty(), None)
488 } 495 }
489 TypeNs::AdtId(AdtId::EnumId(_)) | TypeNs::BuiltinType(_) | TypeNs::TraitId(_) => { 496 TypeNs::AdtId(AdtId::EnumId(_)) | TypeNs::BuiltinType(_) | TypeNs::TraitId(_) => {
490 // FIXME diagnostic 497 // FIXME diagnostic
491 (Ty::Unknown, None) 498 (self.err_ty(), None)
492 } 499 }
493 }; 500 };
494 501
@@ -500,7 +507,7 @@ impl<'a> InferenceContext<'a> {
500 result 507 result
501 } else { 508 } else {
502 // FIXME diagnostic 509 // FIXME diagnostic
503 (Ty::Unknown, None) 510 (TyKind::Unknown.intern(&Interner), None)
504 } 511 }
505 } 512 }
506 513
@@ -711,12 +718,12 @@ impl Expectation {
711 718
712 /// This expresses no expectation on the type. 719 /// This expresses no expectation on the type.
713 fn none() -> Self { 720 fn none() -> Self {
714 Expectation { ty: Ty::Unknown, rvalue_hint: false } 721 Expectation { ty: TyKind::Unknown.intern(&Interner), rvalue_hint: false }
715 } 722 }
716 723
717 fn coercion_target(&self) -> &Ty { 724 fn coercion_target(&self) -> &Ty {
718 if self.rvalue_hint { 725 if self.rvalue_hint {
719 &Ty::Unknown 726 &Ty(TyKind::Unknown)
720 } else { 727 } else {
721 &self.ty 728 &self.ty
722 } 729 }