diff options
Diffstat (limited to 'crates/hir_ty/src/infer/expr.rs')
-rw-r--r-- | crates/hir_ty/src/infer/expr.rs | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 7852b3d23..ec2c13154 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs | |||
@@ -3,23 +3,25 @@ | |||
3 | use std::iter::{repeat, repeat_with}; | 3 | use std::iter::{repeat, repeat_with}; |
4 | use std::{mem, sync::Arc}; | 4 | use std::{mem, sync::Arc}; |
5 | 5 | ||
6 | use chalk_ir::TyVariableKind; | 6 | use chalk_ir::{Mutability, TyVariableKind}; |
7 | use hir_def::{ | 7 | use hir_def::{ |
8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, | 8 | expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, |
9 | path::{GenericArg, GenericArgs}, | 9 | path::{GenericArg, GenericArgs}, |
10 | resolver::resolver_for_expr, | 10 | resolver::resolver_for_expr, |
11 | AdtId, AssocContainerId, FieldId, Lookup, | 11 | AssocContainerId, FieldId, Lookup, |
12 | }; | 12 | }; |
13 | use hir_expand::name::{name, Name}; | 13 | use hir_expand::name::{name, Name}; |
14 | use syntax::ast::RangeOp; | 14 | use syntax::ast::RangeOp; |
15 | use test_utils::mark; | 15 | use test_utils::mark; |
16 | 16 | ||
17 | use crate::{ | 17 | use crate::{ |
18 | autoderef, method_resolution, op, | 18 | autoderef, |
19 | lower::lower_to_chalk_mutability, | ||
20 | method_resolution, op, | ||
19 | primitive::{self, UintTy}, | 21 | primitive::{self, UintTy}, |
20 | traits::{FnTrait, InEnvironment}, | 22 | traits::{FnTrait, InEnvironment}, |
21 | utils::{generics, variant_data, Generics}, | 23 | utils::{generics, variant_data, Generics}, |
22 | Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, | 24 | AdtId, Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, |
23 | Substs, TraitRef, Ty, | 25 | Substs, TraitRef, Ty, |
24 | }; | 26 | }; |
25 | 27 | ||
@@ -427,14 +429,14 @@ impl<'a> InferenceContext<'a> { | |||
427 | Ty::Tuple(_, substs) => { | 429 | Ty::Tuple(_, substs) => { |
428 | name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) | 430 | name.as_tuple_index().and_then(|idx| substs.0.get(idx).cloned()) |
429 | } | 431 | } |
430 | Ty::Adt(AdtId::StructId(s), parameters) => { | 432 | Ty::Adt(AdtId(hir_def::AdtId::StructId(s)), parameters) => { |
431 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { | 433 | self.db.struct_data(s).variant_data.field(name).map(|local_id| { |
432 | let field = FieldId { parent: s.into(), local_id }; | 434 | let field = FieldId { parent: s.into(), local_id }; |
433 | self.write_field_resolution(tgt_expr, field); | 435 | self.write_field_resolution(tgt_expr, field); |
434 | self.db.field_types(s.into())[field.local_id].clone().subst(¶meters) | 436 | self.db.field_types(s.into())[field.local_id].clone().subst(¶meters) |
435 | }) | 437 | }) |
436 | } | 438 | } |
437 | Ty::Adt(AdtId::UnionId(u), parameters) => { | 439 | Ty::Adt(AdtId(hir_def::AdtId::UnionId(u)), parameters) => { |
438 | self.db.union_data(u).variant_data.field(name).map(|local_id| { | 440 | self.db.union_data(u).variant_data.field(name).map(|local_id| { |
439 | let field = FieldId { parent: u.into(), local_id }; | 441 | let field = FieldId { parent: u.into(), local_id }; |
440 | self.write_field_resolution(tgt_expr, field); | 442 | self.write_field_resolution(tgt_expr, field); |
@@ -462,10 +464,11 @@ impl<'a> InferenceContext<'a> { | |||
462 | cast_ty | 464 | cast_ty |
463 | } | 465 | } |
464 | Expr::Ref { expr, rawness, mutability } => { | 466 | Expr::Ref { expr, rawness, mutability } => { |
467 | let mutability = lower_to_chalk_mutability(*mutability); | ||
465 | let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = | 468 | let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = |
466 | &expected.ty.as_reference_or_ptr() | 469 | &expected.ty.as_reference_or_ptr() |
467 | { | 470 | { |
468 | if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { | 471 | if *exp_mutability == Mutability::Mut && mutability == Mutability::Not { |
469 | // FIXME: throw type error - expected mut reference but found shared ref, | 472 | // FIXME: throw type error - expected mut reference but found shared ref, |
470 | // which cannot be coerced | 473 | // which cannot be coerced |
471 | } | 474 | } |
@@ -479,8 +482,8 @@ impl<'a> InferenceContext<'a> { | |||
479 | }; | 482 | }; |
480 | let inner_ty = self.infer_expr_inner(*expr, &expectation); | 483 | let inner_ty = self.infer_expr_inner(*expr, &expectation); |
481 | match rawness { | 484 | match rawness { |
482 | Rawness::RawPtr => Ty::Raw(*mutability, Substs::single(inner_ty)), | 485 | Rawness::RawPtr => Ty::Raw(mutability, Substs::single(inner_ty)), |
483 | Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)), | 486 | Rawness::Ref => Ty::Ref(mutability, Substs::single(inner_ty)), |
484 | } | 487 | } |
485 | } | 488 | } |
486 | Expr::Box { expr } => { | 489 | Expr::Box { expr } => { |
@@ -495,7 +498,7 @@ impl<'a> InferenceContext<'a> { | |||
495 | _ => (), | 498 | _ => (), |
496 | } | 499 | } |
497 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); | 500 | sb = sb.fill(repeat_with(|| self.table.new_type_var())); |
498 | Ty::Adt(box_, sb.build()) | 501 | Ty::adt_ty(box_, sb.build()) |
499 | } else { | 502 | } else { |
500 | Ty::Unknown | 503 | Ty::Unknown |
501 | } | 504 | } |
@@ -583,31 +586,31 @@ impl<'a> InferenceContext<'a> { | |||
583 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); | 586 | let rhs_ty = rhs.map(|e| self.infer_expr(e, &rhs_expect)); |
584 | match (range_type, lhs_ty, rhs_ty) { | 587 | match (range_type, lhs_ty, rhs_ty) { |
585 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { | 588 | (RangeOp::Exclusive, None, None) => match self.resolve_range_full() { |
586 | Some(adt) => Ty::Adt(adt, Substs::empty()), | 589 | Some(adt) => Ty::adt_ty(adt, Substs::empty()), |
587 | None => Ty::Unknown, | 590 | None => Ty::Unknown, |
588 | }, | 591 | }, |
589 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { | 592 | (RangeOp::Exclusive, None, Some(ty)) => match self.resolve_range_to() { |
590 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), | 593 | Some(adt) => Ty::adt_ty(adt, Substs::single(ty)), |
591 | None => Ty::Unknown, | 594 | None => Ty::Unknown, |
592 | }, | 595 | }, |
593 | (RangeOp::Inclusive, None, Some(ty)) => { | 596 | (RangeOp::Inclusive, None, Some(ty)) => { |
594 | match self.resolve_range_to_inclusive() { | 597 | match self.resolve_range_to_inclusive() { |
595 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), | 598 | Some(adt) => Ty::adt_ty(adt, Substs::single(ty)), |
596 | None => Ty::Unknown, | 599 | None => Ty::Unknown, |
597 | } | 600 | } |
598 | } | 601 | } |
599 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { | 602 | (RangeOp::Exclusive, Some(_), Some(ty)) => match self.resolve_range() { |
600 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), | 603 | Some(adt) => Ty::adt_ty(adt, Substs::single(ty)), |
601 | None => Ty::Unknown, | 604 | None => Ty::Unknown, |
602 | }, | 605 | }, |
603 | (RangeOp::Inclusive, Some(_), Some(ty)) => { | 606 | (RangeOp::Inclusive, Some(_), Some(ty)) => { |
604 | match self.resolve_range_inclusive() { | 607 | match self.resolve_range_inclusive() { |
605 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), | 608 | Some(adt) => Ty::adt_ty(adt, Substs::single(ty)), |
606 | None => Ty::Unknown, | 609 | None => Ty::Unknown, |
607 | } | 610 | } |
608 | } | 611 | } |
609 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { | 612 | (RangeOp::Exclusive, Some(ty), None) => match self.resolve_range_from() { |
610 | Some(adt) => Ty::Adt(adt, Substs::single(ty)), | 613 | Some(adt) => Ty::adt_ty(adt, Substs::single(ty)), |
611 | None => Ty::Unknown, | 614 | None => Ty::Unknown, |
612 | }, | 615 | }, |
613 | (RangeOp::Inclusive, _, None) => Ty::Unknown, | 616 | (RangeOp::Inclusive, _, None) => Ty::Unknown, |
@@ -684,11 +687,11 @@ impl<'a> InferenceContext<'a> { | |||
684 | } | 687 | } |
685 | Expr::Literal(lit) => match lit { | 688 | Expr::Literal(lit) => match lit { |
686 | Literal::Bool(..) => Ty::Scalar(Scalar::Bool), | 689 | Literal::Bool(..) => Ty::Scalar(Scalar::Bool), |
687 | Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)), | 690 | Literal::String(..) => Ty::Ref(Mutability::Not, Substs::single(Ty::Str)), |
688 | Literal::ByteString(..) => { | 691 | Literal::ByteString(..) => { |
689 | let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); | 692 | let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); |
690 | let array_type = Ty::Array(Substs::single(byte_type)); | 693 | let array_type = Ty::Array(Substs::single(byte_type)); |
691 | Ty::Ref(Mutability::Shared, Substs::single(array_type)) | 694 | Ty::Ref(Mutability::Not, Substs::single(array_type)) |
692 | } | 695 | } |
693 | Literal::Char(..) => Ty::Scalar(Scalar::Char), | 696 | Literal::Char(..) => Ty::Scalar(Scalar::Char), |
694 | Literal::Int(_v, ty) => match ty { | 697 | Literal::Int(_v, ty) => match ty { |