1 use super::combine::{CombineFields, RelationDir};
4 use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
5 use crate::ty::subst::SubstsRef;
7 use crate::ty::{self, Ty, TyCtxt};
9 use rustc_hir::def_id::DefId;
11 /// Ensures `a` is made equal to `b`. Returns `a` on success.
12 pub struct Equate<'combine, 'infcx, 'tcx> {
13 fields: &'combine mut CombineFields<'infcx, 'tcx>,
17 impl<'combine, 'infcx, 'tcx> Equate<'combine, 'infcx, 'tcx> {
19 fields: &'combine mut CombineFields<'infcx, 'tcx>,
21 ) -> Equate<'combine, 'infcx, 'tcx> {
22 Equate { fields: fields, a_is_expected: a_is_expected }
26 impl TypeRelation<'tcx> for Equate<'combine, 'infcx, 'tcx> {
27 fn tag(&self) -> &'static str {
31 fn tcx(&self) -> TyCtxt<'tcx> {
35 fn param_env(&self) -> ty::ParamEnv<'tcx> {
39 fn a_is_expected(&self) -> bool {
43 fn relate_item_substs(
46 a_subst: SubstsRef<'tcx>,
47 b_subst: SubstsRef<'tcx>,
48 ) -> RelateResult<'tcx, SubstsRef<'tcx>> {
49 // N.B., once we are equating types, we don't care about
50 // variance, so don't try to lookup the variance here. This
51 // also avoids some cycles (e.g., #41849) since looking up
52 // variance requires computing types which can require
53 // performing trait matching (which then performs equality
56 relate::relate_substs(self, None, a_subst, b_subst)
59 fn relate_with_variance<T: Relate<'tcx>>(
64 ) -> RelateResult<'tcx, T> {
68 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
69 debug!("{}.tys({:?}, {:?})", self.tag(), a, b);
74 let infcx = self.fields.infcx;
75 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
76 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
78 debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
80 match (&a.kind, &b.kind) {
81 (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
82 infcx.type_variables.borrow_mut().equate(a_id, b_id);
85 (&ty::Infer(TyVar(a_id)), _) => {
86 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
89 (_, &ty::Infer(TyVar(b_id))) => {
90 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
94 self.fields.infcx.super_combine_tys(self, a, b)?;
105 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
106 debug!("{}.regions({:?}, {:?})", self.tag(), a, b);
107 let origin = Subtype(box self.fields.trace.clone());
108 self.fields.infcx.borrow_region_constraints().make_eqregion(origin, a, b);
114 a: &'tcx ty::Const<'tcx>,
115 b: &'tcx ty::Const<'tcx>,
116 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
117 self.fields.infcx.super_combine_consts(self, a, b)
124 ) -> RelateResult<'tcx, ty::Binder<T>>
128 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
129 self.fields.higher_ranked_sub(b, a, self.a_is_expected)