aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r--crates/hir_ty/src/infer/coerce.rs18
-rw-r--r--crates/hir_ty/src/infer/expr.rs21
-rw-r--r--crates/hir_ty/src/infer/pat.rs11
-rw-r--r--crates/hir_ty/src/infer/unify.rs6
4 files changed, 30 insertions, 26 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 667b26a76..cf0a3add4 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -4,8 +4,8 @@
4//! 4//!
5//! See: https://doc.rust-lang.org/nomicon/coercions.html 5//! See: https://doc.rust-lang.org/nomicon/coercions.html
6 6
7use chalk_ir::TyVariableKind; 7use chalk_ir::{Mutability, TyVariableKind};
8use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; 8use hir_def::lang_item::LangItemTarget;
9use test_utils::mark; 9use test_utils::mark;
10 10
11use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty}; 11use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
@@ -73,20 +73,20 @@ impl<'a> InferenceContext<'a> {
73 match (&mut from_ty, to_ty) { 73 match (&mut from_ty, to_ty) {
74 // `*mut T` -> `*const T` 74 // `*mut T` -> `*const T`
75 // `&mut T` -> `&T` 75 // `&mut T` -> `&T`
76 (Ty::RawPtr(m1, ..), Ty::RawPtr(m2 @ Mutability::Shared, ..)) 76 (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..))
77 | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => { 77 | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => {
78 *m1 = *m2; 78 *m1 = *m2;
79 } 79 }
80 // `&T` -> `*const T` 80 // `&T` -> `*const T`
81 // `&mut T` -> `*mut T`/`*const T` 81 // `&mut T` -> `*mut T`/`*const T`
82 (Ty::Ref(.., substs), &Ty::RawPtr(m2 @ Mutability::Shared, ..)) 82 (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..))
83 | (Ty::Ref(Mutability::Mut, substs), &Ty::RawPtr(m2, ..)) => { 83 | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => {
84 from_ty = Ty::RawPtr(m2, substs.clone()); 84 from_ty = Ty::Raw(m2, substs.clone());
85 } 85 }
86 86
87 // Illegal mutability conversion 87 // Illegal mutability conversion
88 (Ty::RawPtr(Mutability::Shared, ..), Ty::RawPtr(Mutability::Mut, ..)) 88 (Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..))
89 | (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false, 89 | (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
90 90
91 // `{function_type}` -> `fn()` 91 // `{function_type}` -> `fn()`
92 (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) { 92 (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) {
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index 928ad37a3..cf1f1038a 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -3,7 +3,7 @@
3use std::iter::{repeat, repeat_with}; 3use std::iter::{repeat, repeat_with};
4use std::{mem, sync::Arc}; 4use std::{mem, sync::Arc};
5 5
6use chalk_ir::TyVariableKind; 6use chalk_ir::{Mutability, TyVariableKind};
7use hir_def::{ 7use 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},
@@ -15,12 +15,14 @@ use syntax::ast::RangeOp;
15use test_utils::mark; 15use test_utils::mark;
16 16
17use crate::{ 17use 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 Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
23 Substs, TraitRef, Ty, 25 TraitRef, Ty,
24}; 26};
25 27
26use super::{ 28use super::{
@@ -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::RawPtr(*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 } => {
@@ -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 {
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index a318e47f3..eb099311c 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -3,17 +3,17 @@
3use std::iter::repeat; 3use std::iter::repeat;
4use std::sync::Arc; 4use std::sync::Arc;
5 5
6use chalk_ir::Mutability;
6use hir_def::{ 7use hir_def::{
7 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, 8 expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
8 path::Path, 9 path::Path,
9 type_ref::Mutability,
10 FieldId, 10 FieldId,
11}; 11};
12use hir_expand::name::Name; 12use hir_expand::name::Name;
13use test_utils::mark; 13use test_utils::mark;
14 14
15use super::{BindingMode, Expectation, InferenceContext}; 15use super::{BindingMode, Expectation, InferenceContext};
16use crate::{utils::variant_data, Substs, Ty}; 16use crate::{lower::lower_to_chalk_mutability, utils::variant_data, Substs, Ty};
17 17
18impl<'a> InferenceContext<'a> { 18impl<'a> InferenceContext<'a> {
19 fn infer_tuple_struct_pat( 19 fn infer_tuple_struct_pat(
@@ -103,7 +103,7 @@ impl<'a> InferenceContext<'a> {
103 expected = inner; 103 expected = inner;
104 default_bm = match default_bm { 104 default_bm = match default_bm {
105 BindingMode::Move => BindingMode::Ref(mutability), 105 BindingMode::Move => BindingMode::Ref(mutability),
106 BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), 106 BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not),
107 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), 107 BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
108 } 108 }
109 } 109 }
@@ -152,9 +152,10 @@ impl<'a> InferenceContext<'a> {
152 } 152 }
153 } 153 }
154 Pat::Ref { pat, mutability } => { 154 Pat::Ref { pat, mutability } => {
155 let mutability = lower_to_chalk_mutability(*mutability);
155 let expectation = match expected.as_reference() { 156 let expectation = match expected.as_reference() {
156 Some((inner_ty, exp_mut)) => { 157 Some((inner_ty, exp_mut)) => {
157 if *mutability != exp_mut { 158 if mutability != exp_mut {
158 // FIXME: emit type error? 159 // FIXME: emit type error?
159 } 160 }
160 inner_ty 161 inner_ty
@@ -162,7 +163,7 @@ impl<'a> InferenceContext<'a> {
162 _ => &Ty::Unknown, 163 _ => &Ty::Unknown,
163 }; 164 };
164 let subty = self.infer_pat(*pat, expectation, default_bm); 165 let subty = self.infer_pat(*pat, expectation, default_bm);
165 Ty::Ref(*mutability, Substs::single(subty)) 166 Ty::Ref(mutability, Substs::single(subty))
166 } 167 }
167 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( 168 Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
168 p.as_ref(), 169 p.as_ref(),
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index b481aa1b3..99a89a7f3 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -68,7 +68,7 @@ impl<'a, 'b> Canonicalizer<'a, 'b> {
68 } else { 68 } else {
69 let root = self.ctx.table.var_unification_table.find(inner); 69 let root = self.ctx.table.var_unification_table.find(inner);
70 let position = self.add(InferenceVar::from_inner(root), kind); 70 let position = self.add(InferenceVar::from_inner(root), kind);
71 Ty::Bound(BoundVar::new(binders, position)) 71 Ty::BoundVar(BoundVar::new(binders, position))
72 } 72 }
73 } 73 }
74 _ => ty, 74 _ => ty,
@@ -110,7 +110,7 @@ impl<T> Canonicalized<T> {
110 pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty { 110 pub(super) fn decanonicalize_ty(&self, mut ty: Ty) -> Ty {
111 ty.walk_mut_binders( 111 ty.walk_mut_binders(
112 &mut |ty, binders| { 112 &mut |ty, binders| {
113 if let &mut Ty::Bound(bound) = ty { 113 if let &mut Ty::BoundVar(bound) = ty {
114 if bound.debruijn >= binders { 114 if bound.debruijn >= binders {
115 let (v, k) = self.free_vars[bound.index]; 115 let (v, k) = self.free_vars[bound.index];
116 *ty = Ty::InferenceVar(v, k); 116 *ty = Ty::InferenceVar(v, k);
@@ -168,7 +168,7 @@ pub(crate) fn unify(tys: &Canonical<(Ty, Ty)>) -> Option<Substs> {
168 // (kind of hacky) 168 // (kind of hacky)
169 for (i, var) in vars.iter().enumerate() { 169 for (i, var) in vars.iter().enumerate() {
170 if &*table.resolve_ty_shallow(var) == var { 170 if &*table.resolve_ty_shallow(var) == var {
171 table.unify(var, &Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, i))); 171 table.unify(var, &Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, i)));
172 } 172 }
173 } 173 }
174 Some( 174 Some(