1 use super::combine::{CombineFields, RelationDir, const_unification_error};
4 use crate::hir::def_id::DefId;
6 use crate::ty::{self, Ty, TyCtxt, InferConst};
8 use crate::ty::subst::SubstsRef;
9 use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
10 use crate::mir::interpret::ConstValue;
11 use crate::infer::unify_key::replace_if_possible;
13 /// Ensures `a` is made equal to `b`. Returns `a` on success.
14 pub struct Equate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
15 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
19 impl<'combine, 'infcx, 'gcx, 'tcx> Equate<'combine, 'infcx, 'gcx, 'tcx> {
20 pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
21 -> Equate<'combine, 'infcx, 'gcx, 'tcx>
23 Equate { fields: fields, a_is_expected: a_is_expected }
27 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
28 for Equate<'combine, 'infcx, 'gcx, 'tcx>
30 fn tag(&self) -> &'static str { "Equate" }
32 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
34 fn a_is_expected(&self) -> bool { self.a_is_expected }
36 fn relate_item_substs(&mut self,
38 a_subst: SubstsRef<'tcx>,
39 b_subst: SubstsRef<'tcx>)
40 -> RelateResult<'tcx, SubstsRef<'tcx>>
42 // N.B., once we are equating types, we don't care about
43 // variance, so don't try to lookup the variance here. This
44 // also avoids some cycles (e.g., #41849) since looking up
45 // variance requires computing types which can require
46 // performing trait matching (which then performs equality
49 relate::relate_substs(self, None, a_subst, b_subst)
52 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
56 -> RelateResult<'tcx, T>
61 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
62 debug!("{}.tys({:?}, {:?})", self.tag(),
64 if a == b { return Ok(a); }
66 let infcx = self.fields.infcx;
67 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
68 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
70 debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
72 match (&a.sty, &b.sty) {
73 (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
74 infcx.type_variables.borrow_mut().equate(a_id, b_id);
77 (&ty::Infer(TyVar(a_id)), _) => {
78 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
81 (_, &ty::Infer(TyVar(b_id))) => {
82 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
86 self.fields.infcx.super_combine_tys(self, a, b)?;
93 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
94 -> RelateResult<'tcx, ty::Region<'tcx>> {
95 debug!("{}.regions({:?}, {:?})",
99 let origin = Subtype(self.fields.trace.clone());
100 self.fields.infcx.borrow_region_constraints()
101 .make_eqregion(origin, a, b);
107 a: &'tcx ty::Const<'tcx>,
108 b: &'tcx ty::Const<'tcx>,
109 ) -> RelateResult<'tcx, &'tcx ty::Const<'tcx>> {
110 debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
111 if a == b { return Ok(a); }
113 let infcx = self.fields.infcx;
114 let a = replace_if_possible(infcx.const_unification_table.borrow_mut(), a);
115 let b = replace_if_possible(infcx.const_unification_table.borrow_mut(), b);
116 let a_is_expected = self.a_is_expected();
118 match (a.val, b.val) {
119 (ConstValue::Infer(InferConst::Var(a_vid)),
120 ConstValue::Infer(InferConst::Var(b_vid))) => {
121 infcx.const_unification_table
123 .unify_var_var(a_vid, b_vid)
124 .map_err(|e| const_unification_error(a_is_expected, e))?;
128 (ConstValue::Infer(InferConst::Var(a_id)), _) => {
129 self.fields.infcx.unify_const_variable(a_is_expected, a_id, b)?;
133 (_, ConstValue::Infer(InferConst::Var(b_id))) => {
134 self.fields.infcx.unify_const_variable(!a_is_expected, b_id, a)?;
141 self.fields.infcx.super_combine_consts(self, a, b)?;
145 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
146 -> RelateResult<'tcx, ty::Binder<T>>
147 where T: Relate<'tcx>
149 self.fields.higher_ranked_sub(a, b, self.a_is_expected)?;
150 self.fields.higher_ranked_sub(b, a, self.a_is_expected)