From 42217738e0b121a8e5d48a9a55cb51ef6c98975f Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sun, 14 Mar 2021 17:40:55 +0100 Subject: Don't use Substs for Ref/Raw/Array/Slice --- crates/hir_ty/src/infer/coerce.rs | 10 ++-------- crates/hir_ty/src/infer/expr.rs | 19 ++++++++----------- crates/hir_ty/src/infer/pat.rs | 10 +++++----- crates/hir_ty/src/infer/unify.rs | 24 +++++++++++++++++++----- 4 files changed, 34 insertions(+), 29 deletions(-) (limited to 'crates/hir_ty/src/infer') 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> { // Auto Deref if cannot coerce match (from_ty.interned(&Interner), to_ty.interned(&Interner)) { // FIXME: DerefMut - (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => { - self.unify_autoderef_behind_ref(&st1[0], &st2[0]) - } + (TyKind::Ref(_, st1), TyKind::Ref(_, st2)) => self.unify_autoderef_behind_ref(st1, st2), // Otherwise, normal unify _ => self.unify(&from_ty, to_ty), @@ -178,11 +176,7 @@ impl<'a> InferenceContext<'a> { // Stop when constructor matches. if from_ty.equals_ctor(&to_ty) { // It will not recurse to `coerce`. - return match (from_ty.substs(), to_ty.substs()) { - (Some(st1), Some(st2)) => self.table.unify_substs(st1, st2, 0), - (None, None) => true, - _ => false, - }; + return self.table.unify(&from_ty, &to_ty); } else if self.table.unify_inner_trivial(&derefed_ty, &to_ty, 0) { return true; } 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> { }; let inner_ty = self.infer_expr_inner(*expr, &expectation); match rawness { - Rawness::RawPtr => TyKind::Raw(mutability, Substs::single(inner_ty)), - Rawness::Ref => TyKind::Ref(mutability, Substs::single(inner_ty)), + Rawness::RawPtr => TyKind::Raw(mutability, inner_ty), + Rawness::Ref => TyKind::Ref(mutability, inner_ty), } .intern(&Interner) } @@ -686,7 +686,7 @@ impl<'a> InferenceContext<'a> { } Expr::Array(array) => { let elem_ty = match expected.ty.interned(&Interner) { - TyKind::Array(st) | TyKind::Slice(st) => st.as_single().clone(), + TyKind::Array(st) | TyKind::Slice(st) => st.clone(), _ => self.table.new_type_var(), }; @@ -710,18 +710,17 @@ impl<'a> InferenceContext<'a> { } } - TyKind::Array(Substs::single(elem_ty)).intern(&Interner) + TyKind::Array(elem_ty).intern(&Interner) } Expr::Literal(lit) => match lit { Literal::Bool(..) => TyKind::Scalar(Scalar::Bool).intern(&Interner), Literal::String(..) => { - TyKind::Ref(Mutability::Not, Substs::single(TyKind::Str.intern(&Interner))) - .intern(&Interner) + TyKind::Ref(Mutability::Not, TyKind::Str.intern(&Interner)).intern(&Interner) } Literal::ByteString(..) => { let byte_type = TyKind::Scalar(Scalar::Uint(UintTy::U8)).intern(&Interner); - let array_type = TyKind::Array(Substs::single(byte_type)).intern(&Interner); - TyKind::Ref(Mutability::Not, Substs::single(array_type)).intern(&Interner) + let array_type = TyKind::Array(byte_type).intern(&Interner); + TyKind::Ref(Mutability::Not, array_type).intern(&Interner) } Literal::Char(..) => TyKind::Scalar(Scalar::Char).intern(&Interner), Literal::Int(_v, ty) => match ty { @@ -855,9 +854,7 @@ impl<'a> InferenceContext<'a> { // Apply autoref so the below unification works correctly // FIXME: return correct autorefs from lookup_method let actual_receiver_ty = match expected_receiver_ty.as_reference() { - Some((_, mutability)) => { - TyKind::Ref(mutability, Substs::single(derefed_receiver_ty)).intern(&Interner) - } + Some((_, mutability)) => TyKind::Ref(mutability, derefed_receiver_ty).intern(&Interner), _ => derefed_receiver_ty, }; 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> { _ => self.result.standard_types.unknown.clone(), }; let subty = self.infer_pat(*pat, &expectation, default_bm); - TyKind::Ref(mutability, Substs::single(subty)).intern(&Interner) + TyKind::Ref(mutability, subty).intern(&Interner) } Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( p.as_ref(), @@ -196,7 +196,7 @@ impl<'a> InferenceContext<'a> { let bound_ty = match mode { BindingMode::Ref(mutability) => { - TyKind::Ref(mutability, Substs::single(inner_ty.clone())).intern(&Interner) + TyKind::Ref(mutability, inner_ty.clone()).intern(&Interner) } BindingMode::Move => inner_ty.clone(), }; @@ -206,8 +206,8 @@ impl<'a> InferenceContext<'a> { } Pat::Slice { prefix, slice, suffix } => { let (container_ty, elem_ty): (fn(_) -> _, _) = match expected.interned(&Interner) { - TyKind::Array(st) => (TyKind::Array, st.as_single().clone()), - TyKind::Slice(st) => (TyKind::Slice, st.as_single().clone()), + TyKind::Array(st) => (TyKind::Array, st.clone()), + TyKind::Slice(st) => (TyKind::Slice, st.clone()), _ => (TyKind::Slice, self.err_ty()), }; @@ -215,7 +215,7 @@ impl<'a> InferenceContext<'a> { self.infer_pat(*pat_id, &elem_ty, default_bm); } - let pat_ty = container_ty(Substs::single(elem_ty)).intern(&Interner); + let pat_ty = container_ty(elem_ty).intern(&Interner); if let Some(slice_pat_id) = slice { self.infer_pat(*slice_pat_id, &pat_ty, default_bm); } 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}; use super::{InferenceContext, Obligation}; use crate::{ - BoundVar, Canonical, DebruijnIndex, GenericPredicate, InEnvironment, InferenceVar, Interner, - Scalar, Substs, Ty, TyKind, TypeWalk, + BoundVar, Canonical, DebruijnIndex, FnPointer, GenericPredicate, InEnvironment, InferenceVar, + Interner, Scalar, Substs, Ty, TyKind, TypeWalk, }; impl<'a> InferenceContext<'a> { @@ -283,9 +283,23 @@ impl InferenceTable { let ty1 = self.resolve_ty_shallow(ty1); let ty2 = self.resolve_ty_shallow(ty2); if ty1.equals_ctor(&ty2) { - match (ty1.substs(), ty2.substs()) { - (Some(st1), Some(st2)) => self.unify_substs(st1, st2, depth + 1), - (None, None) => true, + match (ty1.interned(&Interner), ty2.interned(&Interner)) { + (TyKind::Adt(_, substs1), TyKind::Adt(_, substs2)) + | (TyKind::FnDef(_, substs1), TyKind::FnDef(_, substs2)) + | ( + TyKind::Function(FnPointer { substs: substs1, .. }), + TyKind::Function(FnPointer { substs: substs2, .. }), + ) + | (TyKind::Tuple(_, substs1), TyKind::Tuple(_, substs2)) + | (TyKind::OpaqueType(_, substs1), TyKind::OpaqueType(_, substs2)) + | (TyKind::AssociatedType(_, substs1), TyKind::AssociatedType(_, substs2)) + | (TyKind::Closure(.., substs1), TyKind::Closure(.., substs2)) => { + self.unify_substs(substs1, substs2, depth + 1) + } + (TyKind::Ref(_, ty1), TyKind::Ref(_, ty2)) + | (TyKind::Raw(_, ty1), TyKind::Raw(_, ty2)) + | (TyKind::Array(ty1), TyKind::Array(ty2)) + | (TyKind::Slice(ty1), TyKind::Slice(ty2)) => self.unify_inner(ty1, ty2, depth + 1), _ => false, } } else { -- cgit v1.2.3