From 4c35df47e84b0eff51faa95b4c9636bb5f3da5eb Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 7 Apr 2021 20:08:50 +0200 Subject: [PATCH] Move `equals_ctor` to `TyExt` I'd prefer getting rid of it, but it's used in the impl search and not super easy to replace there (I think ideally the impl search would do proper unification, but that's a bit more complicated). --- crates/hir_ty/src/chalk_ext.rs | 35 +++++++++++++++++++++++++++++++- crates/hir_ty/src/infer/unify.rs | 3 ++- crates/hir_ty/src/lib.rs | 32 +---------------------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 8e8a1aa4816..6a353423a71 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -8,7 +8,7 @@ use hir_def::{ use crate::{ db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id, from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId, - CallableSig, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, + CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause, Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause, }; @@ -34,6 +34,9 @@ pub trait TyExt { fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option>; fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option; + + /// FIXME: Get rid of this, it's not a good abstraction + fn equals_ctor(&self, other: &Ty) -> bool; } impl TyExt for Ty { @@ -238,6 +241,36 @@ impl TyExt for Ty { _ => None, } } + + fn equals_ctor(&self, other: &Ty) -> bool { + match (self.kind(&Interner), other.kind(&Interner)) { + (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, + (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => { + true + } + (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, + (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, + (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => { + ty_id == ty_id2 + } + (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2, + (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2, + (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..)) + | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => { + mutability == mutability2 + } + ( + TyKind::Function(FnPointer { num_binders, sig, .. }), + TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }), + ) => num_binders == num_binders2 && sig == sig2, + (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => { + cardinality == cardinality2 + } + (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true, + (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2, + _ => false, + } + } } pub trait ProjectionTyExt { diff --git a/crates/hir_ty/src/infer/unify.rs b/crates/hir_ty/src/infer/unify.rs index d717e33759f..2ea9dd92099 100644 --- a/crates/hir_ty/src/infer/unify.rs +++ b/crates/hir_ty/src/infer/unify.rs @@ -8,7 +8,8 @@ use ena::unify::{InPlaceUnificationTable, NoError, UnifyKey, UnifyValue}; use super::{DomainGoal, InferenceContext}; use crate::{ AliasEq, AliasTy, BoundVar, Canonical, CanonicalVarKinds, DebruijnIndex, FnPointer, FnSubst, - InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyKind, TypeWalk, WhereClause, + InEnvironment, InferenceVar, Interner, Scalar, Substitution, Ty, TyExt, TyKind, TypeWalk, + WhereClause, }; impl<'a> InferenceContext<'a> { diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 84645c43556..2e851d3e0c2 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -168,37 +168,7 @@ impl CallableSig { } } -impl Ty { - pub fn equals_ctor(&self, other: &Ty) -> bool { - match (self.kind(&Interner), other.kind(&Interner)) { - (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, - (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => { - true - } - (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, - (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, - (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => { - ty_id == ty_id2 - } - (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2, - (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2, - (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..)) - | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => { - mutability == mutability2 - } - ( - TyKind::Function(FnPointer { num_binders, sig, .. }), - TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }), - ) => num_binders == num_binders2 && sig == sig2, - (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => { - cardinality == cardinality2 - } - (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true, - (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2, - _ => false, - } - } -} +impl Ty {} #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub enum ImplTraitId {