aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer/coerce.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/infer/coerce.rs')
-rw-r--r--crates/hir_ty/src/infer/coerce.rs92
1 files changed, 42 insertions, 50 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 32c7c57cd..7e8846f27 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -4,12 +4,12 @@
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 hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; 7use chalk_ir::{Mutability, TyVariableKind};
8use test_utils::mark; 8use hir_def::lang_item::LangItemTarget;
9 9
10use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty, TypeCtor}; 10use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
11 11
12use super::{unify::TypeVarValue, InEnvironment, InferTy, InferenceContext}; 12use super::{InEnvironment, InferenceContext};
13 13
14impl<'a> InferenceContext<'a> { 14impl<'a> InferenceContext<'a> {
15 /// Unify two types, but may coerce the first one to the second one 15 /// Unify two types, but may coerce the first one to the second one
@@ -33,8 +33,8 @@ impl<'a> InferenceContext<'a> {
33 } else if self.coerce(ty2, ty1) { 33 } else if self.coerce(ty2, ty1) {
34 ty1.clone() 34 ty1.clone()
35 } else { 35 } else {
36 if let (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnDef(_))) = (ty1, ty2) { 36 if let (Ty::FnDef(..), Ty::FnDef(..)) = (ty1, ty2) {
37 mark::hit!(coerce_fn_reification); 37 cov_mark::hit!(coerce_fn_reification);
38 // Special case: two function types. Try to coerce both to 38 // Special case: two function types. Try to coerce both to
39 // pointers to have a chance at getting a match. See 39 // pointers to have a chance at getting a match. See
40 // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916 40 // https://github.com/rust-lang/rust/blob/7b805396bf46dce972692a6846ce2ad8481c5f85/src/librustc_typeck/check/coercion.rs#L877-L916
@@ -44,7 +44,7 @@ impl<'a> InferenceContext<'a> {
44 let ptr_ty2 = Ty::fn_ptr(sig2); 44 let ptr_ty2 = Ty::fn_ptr(sig2);
45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2) 45 self.coerce_merge_branch(&ptr_ty1, &ptr_ty2)
46 } else { 46 } else {
47 mark::hit!(coerce_merge_fail_fallback); 47 cov_mark::hit!(coerce_merge_fail_fallback);
48 ty1.clone() 48 ty1.clone()
49 } 49 }
50 } 50 }
@@ -53,12 +53,11 @@ impl<'a> InferenceContext<'a> {
53 fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool { 53 fn coerce_inner(&mut self, mut from_ty: Ty, to_ty: &Ty) -> bool {
54 match (&from_ty, to_ty) { 54 match (&from_ty, to_ty) {
55 // Never type will make type variable to fallback to Never Type instead of Unknown. 55 // Never type will make type variable to fallback to Never Type instead of Unknown.
56 (ty_app!(TypeCtor::Never), Ty::Infer(InferTy::TypeVar(tv))) => { 56 (Ty::Never, Ty::InferenceVar(tv, TyVariableKind::General)) => {
57 let var = self.table.new_maybe_never_type_var(); 57 self.table.type_variable_table.set_diverging(*tv, true);
58 self.table.var_unification_table.union_value(*tv, TypeVarValue::Known(var));
59 return true; 58 return true;
60 } 59 }
61 (ty_app!(TypeCtor::Never), _) => return true, 60 (Ty::Never, _) => return true,
62 61
63 // Trivial cases, this should go after `never` check to 62 // Trivial cases, this should go after `never` check to
64 // avoid infer result type to be never 63 // avoid infer result type to be never
@@ -71,38 +70,33 @@ impl<'a> InferenceContext<'a> {
71 70
72 // Pointer weakening and function to pointer 71 // Pointer weakening and function to pointer
73 match (&mut from_ty, to_ty) { 72 match (&mut from_ty, to_ty) {
74 // `*mut T`, `&mut T, `&T`` -> `*const T` 73 // `*mut T` -> `*const T`
75 // `&mut T` -> `&T` 74 // `&mut T` -> `&T`
76 // `&mut T` -> `*mut T` 75 (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..))
77 (ty_app!(c1@TypeCtor::RawPtr(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) 76 | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => {
78 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::RawPtr(Mutability::Shared))) 77 *m1 = *m2;
79 | (ty_app!(c1@TypeCtor::Ref(_)), ty_app!(c2@TypeCtor::Ref(Mutability::Shared))) 78 }
80 | (ty_app!(c1@TypeCtor::Ref(Mutability::Mut)), ty_app!(c2@TypeCtor::RawPtr(_))) => { 79 // `&T` -> `*const T`
81 *c1 = *c2; 80 // `&mut T` -> `*mut T`/`*const T`
81 (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..))
82 | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => {
83 from_ty = Ty::Raw(m2, substs.clone());
82 } 84 }
83 85
84 // Illegal mutablity conversion 86 // Illegal mutability conversion
85 ( 87 (Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..))
86 ty_app!(TypeCtor::RawPtr(Mutability::Shared)), 88 | (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
87 ty_app!(TypeCtor::RawPtr(Mutability::Mut)),
88 )
89 | (
90 ty_app!(TypeCtor::Ref(Mutability::Shared)),
91 ty_app!(TypeCtor::Ref(Mutability::Mut)),
92 ) => return false,
93 89
94 // `{function_type}` -> `fn()` 90 // `{function_type}` -> `fn()`
95 (ty_app!(TypeCtor::FnDef(_)), ty_app!(TypeCtor::FnPtr { .. })) => { 91 (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) {
96 match from_ty.callable_sig(self.db) { 92 None => return false,
97 None => return false, 93 Some(sig) => {
98 Some(sig) => { 94 from_ty = Ty::fn_ptr(sig);
99 from_ty = Ty::fn_ptr(sig);
100 }
101 } 95 }
102 } 96 },
103 97
104 (ty_app!(TypeCtor::Closure { .. }, params), ty_app!(TypeCtor::FnPtr { .. })) => { 98 (Ty::Closure(.., substs), Ty::Function { .. }) => {
105 from_ty = params[0].clone(); 99 from_ty = substs[0].clone();
106 } 100 }
107 101
108 _ => {} 102 _ => {}
@@ -115,9 +109,7 @@ impl<'a> InferenceContext<'a> {
115 // Auto Deref if cannot coerce 109 // Auto Deref if cannot coerce
116 match (&from_ty, to_ty) { 110 match (&from_ty, to_ty) {
117 // FIXME: DerefMut 111 // FIXME: DerefMut
118 (ty_app!(TypeCtor::Ref(_), st1), ty_app!(TypeCtor::Ref(_), st2)) => { 112 (Ty::Ref(_, st1), Ty::Ref(_, st2)) => self.unify_autoderef_behind_ref(&st1[0], &st2[0]),
119 self.unify_autoderef_behind_ref(&st1[0], &st2[0])
120 }
121 113
122 // Otherwise, normal unify 114 // Otherwise, normal unify
123 _ => self.unify(&from_ty, to_ty), 115 _ => self.unify(&from_ty, to_ty),
@@ -178,17 +170,17 @@ impl<'a> InferenceContext<'a> {
178 }, 170 },
179 ) { 171 ) {
180 let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value); 172 let derefed_ty = canonicalized.decanonicalize_ty(derefed_ty.value);
181 match (&*self.resolve_ty_shallow(&derefed_ty), &*to_ty) { 173 let from_ty = self.resolve_ty_shallow(&derefed_ty);
182 // Stop when constructor matches. 174 // Stop when constructor matches.
183 (ty_app!(from_ctor, st1), ty_app!(to_ctor, st2)) if from_ctor == to_ctor => { 175 if from_ty.equals_ctor(&to_ty) {
184 // It will not recurse to `coerce`. 176 // It will not recurse to `coerce`.
185 return self.table.unify_substs(st1, st2, 0); 177 return match (from_ty.substs(), to_ty.substs()) {
186 } 178 (Some(st1), Some(st2)) => self.table.unify_substs(st1, st2, 0),
187 _ => { 179 (None, None) => true,
188 if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { 180 _ => false,
189 return true; 181 };
190 } 182 } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) {
191 } 183 return true;
192 } 184 }
193 } 185 }
194 186