aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/infer
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-03-14 16:40:55 +0000
committerFlorian Diebold <[email protected]>2021-03-14 19:21:05 +0000
commit42217738e0b121a8e5d48a9a55cb51ef6c98975f (patch)
tree63b3fc22a011ae0ee37a91cb19f5dcfe390507f1 /crates/hir_ty/src/infer
parentaf466f8542173002361eb134e66102908c7cd024 (diff)
Don't use Substs for Ref/Raw/Array/Slice
Diffstat (limited to 'crates/hir_ty/src/infer')
-rw-r--r--crates/hir_ty/src/infer/coerce.rs10
-rw-r--r--crates/hir_ty/src/infer/expr.rs19
-rw-r--r--crates/hir_ty/src/infer/pat.rs10
-rw-r--r--crates/hir_ty/src/infer/unify.rs24
4 files changed, 34 insertions, 29 deletions
diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs
index 435f7d0db..137419264 100644
--- a/crates/hir_ty/src/infer/coerce.rs
+++ b/crates/hir_ty/src/infer/coerce.rs
@@ -111,9 +111,7 @@ impl<'a> InferenceContext<'a> {
111 // Auto Deref if cannot coerce 111 // Auto Deref if cannot coerce
112 match (from_ty.interned(&Interner), to_ty.interned(&Interner)) { 112 match (from_ty.interned(&Interner), to_ty.interned(&Interner)) {
113 // FIXME: DerefMut 113 // FIXME: DerefMut
114 (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => { 114 (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2),
115 self.unify_autoderef_behind_ref(&st1[0], &st2[0])
116 }
117 115
118 // Otherwise, normal unify 116 // Otherwise, normal unify
119 _ => self.unify(&from_ty, to_ty), 117 _ => self.unify(&from_ty, to_ty),
@@ -178,11 +176,7 @@ impl<'a> InferenceContext<'a> {
178 // Stop when constructor matches. 176 // Stop when constructor matches.
179 if from_ty.equals_ctor(&to_ty) { 177 if from_ty.equals_ctor(&to_ty) {
180 // It will not recurse to `coerce`. 178 // It will not recurse to `coerce`.
181 return match (from_ty.substs(), to_ty.substs()) { 179 return self.table.unify(&from_ty, &to_ty);
182 (Some(st1), Some(st2)) => self.table.unify_substs(st1, st2, 0),
183 (None, None) => true,
184 _ => false,
185 };
186 } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { 180 } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) {
187 return true; 181 return true;
188 } 182 }
diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs
index e9ca2b86f..8c58a1b6c 100644
--- a/crates/hir_ty/src/infer/expr.rs
+++ b/crates/hir_ty/src/infer/expr.rs
@@ -504,8 +504,8 @@ impl<'a> InferenceContext<'a> {
504 }; 504 };
505 let inner_ty = self.infer_expr_inner(*expr, &expectation); 505 let inner_ty = self.infer_expr_inner(*expr, &expectation);
506 match rawness { 506 match rawness {
507 Rawness::RawPtr => TyKind::Raw(mutability, Substs::single(inner_ty)), 507 Rawness::RawPtr => TyKind::Raw(mutability, inner_ty),
508 Rawness::Ref => TyKind::Ref(mutability, Substs::single(inner_ty)), 508 Rawness::Ref => TyKind::Ref(mutability, inner_ty),
509 } 509 }
510 .intern(&Interner) 510 .intern(&Interner)
511 } 511 }
@@ -686,7 +686,7 @@ impl<'a> InferenceContext<'a> {
686 } 686 }
687 Expr::Array(array) => { 687 Expr::Array(array) => {
688 let elem_ty = match expected.ty.interned(&Interner) { 688 let elem_ty = match expected.ty.interned(&Interner) {
689 TyKind::Array(st) | TyKind::Slice(st) => st.as_single().clone(), 689 TyKind::Array(st) | TyKind::Slice(st) => st.clone(),
690 _ => self.table.new_type_var(), 690 _ => self.table.new_type_var(),
691 }; 691 };
692 692
@@ -710,18 +710,17 @@ impl<'a> InferenceContext<'a> {
710 } 710 }
711 } 711 }
712 712
713 TyKind::Array(Substs::single(elem_ty)).intern(&Interner) 713 TyKind::Array(elem_ty).intern(&Interner)
714 } 714 }
715 Expr::Literal(lit) => match lit { 715 Expr::Literal(lit) => match lit {
716 Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), 716 Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner),
717 Literal::String(..) => { 717 Literal::String(..) => {
718 TyKind::Ref(Mutability::Not, Substs::single(TyKind::Str.intern(&Interner))) 718 TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner)
719 .intern(&Interner)
720 } 719 }
721 Literal::ByteString(..) => { 720 Literal::ByteString(..) => {
722 let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); 721 let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner);
723 let array_type = TyKind::Array(Substs::single(byte_type)).intern(&Interner); 722 let array_type = TyKind::Array(byte_type).intern(&Interner);
724 TyKind::Ref(Mutability::Not, Substs::single(array_type)).intern(&Interner) 723 TyKind::Ref(Mutability::Not, array_type).intern(&Interner)
725 } 724 }
726 Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), 725 Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner),
727 Literal::Int(_v, ty) => match ty { 726 Literal::Int(_v, ty) => match ty {
@@ -855,9 +854,7 @@ impl<'a> InferenceContext<'a> {
855 // Apply autoref so the below unification works correctly 854 // Apply autoref so the below unification works correctly
856 // FIXME: return correct autorefs from lookup_method 855 // FIXME: return correct autorefs from lookup_method
857 let actual_receiver_ty = match expected_receiver_ty.as_reference() { 856 let actual_receiver_ty = match expected_receiver_ty.as_reference() {
858 Some((_, mutability)) => { 857 Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner),
859 TyKind::Ref(mutability, Substs::single(derefed_receiver_ty)).intern(&Interner)
860 }
861 _ => derefed_receiver_ty, 858 _ => derefed_receiver_ty,
862 }; 859 };
863 self.unify(&expected_receiver_ty, &actual_receiver_ty); 860 self.unify(&expected_receiver_ty, &actual_receiver_ty);
diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs
index 2e812ab94..9e8ca18ef 100644
--- a/crates/hir_ty/src/infer/pat.rs
+++ b/crates/hir_ty/src/infer/pat.rs
@@ -163,7 +163,7 @@ impl<'a> InferenceContext<'a> {
163 _ => self.result.standard_types.unknown.clone(), 163 _ => self.result.standard_types.unknown.clone(),
164 }; 164 };
165 let subty = self.infer_pat(*pat, &expectation, default_bm); 165 let subty = self.infer_pat(*pat, &expectation, default_bm);
166 TyKind::Ref(mutability, Substs::single(subty)).intern(&Interner) 166 TyKind::Ref(mutability, subty).intern(&Interner)
167 } 167 }
168 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(
169 p.as_ref(), 169 p.as_ref(),
@@ -196,7 +196,7 @@ impl<'a> InferenceContext<'a> {
196 196
197 let bound_ty = match mode { 197 let bound_ty = match mode {
198 BindingMode::Ref(mutability) => { 198 BindingMode::Ref(mutability) => {
199 TyKind::Ref(mutability, Substs::single(inner_ty.clone())).intern(&Interner) 199 TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner)
200 } 200 }
201 BindingMode::Move => inner_ty.clone(), 201 BindingMode::Move => inner_ty.clone(),
202 }; 202 };
@@ -206,8 +206,8 @@ impl<'a> InferenceContext<'a> {
206 } 206 }
207 Pat::Slice { prefix, slice, suffix } => { 207 Pat::Slice { prefix, slice, suffix } => {
208 let (container_ty, elem_ty): (fn(_) -> _, _) = match expected.interned(&Interner) { 208 let (container_ty, elem_ty): (fn(_) -> _, _) = match expected.interned(&Interner) {
209 TyKind::Array(st) => (TyKind::Array, st.as_single().clone()), 209 TyKind::Array(st) => (TyKind::Array, st.clone()),
210 TyKind::Slice(st) => (TyKind::Slice, st.as_single().clone()), 210 TyKind::Slice(st) => (TyKind::Slice, st.clone()),
211 _ => (TyKind::Slice, self.err_ty()), 211 _ => (TyKind::Slice, self.err_ty()),
212 }; 212 };
213 213
@@ -215,7 +215,7 @@ impl<'a> InferenceContext<'a> {
215 self.infer_pat(*pat_id, &elem_ty, default_bm); 215 self.infer_pat(*pat_id, &elem_ty, default_bm);
216 } 216 }
217 217
218 let pat_ty = container_ty(Substs::single(elem_ty)).intern(&Interner); 218 let pat_ty = container_ty(elem_ty).intern(&Interner);
219 if let Some(slice_pat_id) = slice { 219 if let Some(slice_pat_id) = slice {
220 self.infer_pat(*slice_pat_id, &pat_ty, default_bm); 220 self.infer_pat(*slice_pat_id, &pat_ty, default_bm);
221 } 221 }
diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs
index 2501a4e0a..66f8fe8a3 100644
--- a/crates/hir_ty/src/infer/unify.rs
+++ b/crates/hir_ty/src/infer/unify.rs
@@ -7,8 +7,8 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue};
7 7
8use super::{InferenceContext, Obligation}; 8use super::{InferenceContext, Obligation};
9use crate::{ 9use crate::{
10 BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferenceVar, Interner, 10 BoundVar, Canonical, DebruijnIndex, FnPointer, GenericPredicate, InEnvironment, InferenceVar,
11 Scalar, Substs, Ty, TyKind, TypeWalk, 11 Interner, Scalar, Substs, Ty, TyKind, TypeWalk,
12}; 12};
13 13
14impl<'a> InferenceContext<'a> { 14impl<'a> InferenceContext<'a> {
@@ -283,9 +283,23 @@ impl InferenceTable {
283 let ty1 = self.resolve_ty_shallow(ty1); 283 let ty1 = self.resolve_ty_shallow(ty1);
284 let ty2 = self.resolve_ty_shallow(ty2); 284 let ty2 = self.resolve_ty_shallow(ty2);
285 if ty1.equals_ctor(&ty2) { 285 if ty1.equals_ctor(&ty2) {
286 match (ty1.substs(), ty2.substs()) { 286 match (ty1.interned(&Interner), ty2.interned(&Interner)) {
287 (Some(st1), Some(st2)) => self.unify_substs(st1, st2, depth + 1), 287 (TyKind::Adt(_, substs1), TyKind::Adt(_, substs2))
288 (None, None) => true, 288 | (TyKind::FnDef(_, substs1), TyKind::FnDef(_, substs2))
289 | (
290 TyKind::Function(FnPointer { substs: substs1, .. }),
291 TyKind::Function(FnPointer { substs: substs2, .. }),
292 )
293 | (TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2))
294 | (TyKind::OpaqueType(_, substs1), TyKind::OpaqueType(_, substs2))
295 | (TyKind::AssociatedType(_, substs1), TyKind::AssociatedType(_, substs2))
296 | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => {
297 self.unify_substs(substs1, substs2, depth + 1)
298 }
299 (TyKind::Ref(_, ty1), TyKind::Ref(_, ty2))
300 | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2))
301 | (TyKind::Array(ty1), TyKind::Array(ty2))
302 | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1),
289 _ => false, 303 _ => false,
290 } 304 }
291 } else { 305 } else {