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;
12 /// Ensures `a` is made equal to `b`. Returns `a` on success.
13 pub struct Equate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
14 fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>,
18 impl<'combine, 'infcx, 'gcx, 'tcx> Equate<'combine, 'infcx, 'gcx, 'tcx> {
19 pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool)
20 -> Equate<'combine, 'infcx, 'gcx, 'tcx>
22 Equate { fields: fields, a_is_expected: a_is_expected }
26 impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx>
27 for Equate<'combine, 'infcx, 'gcx, 'tcx>
29 fn tag(&self) -> &'static str { "Equate" }
31 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
33 fn a_is_expected(&self) -> bool { self.a_is_expected }
35 fn relate_item_substs(&mut self,
37 a_subst: SubstsRef<'tcx>,
38 b_subst: SubstsRef<'tcx>)
39 -> RelateResult<'tcx, SubstsRef<'tcx>>
41 // N.B., once we are equating types, we don't care about
42 // variance, so don't try to lookup the variance here. This
43 // also avoids some cycles (e.g., #41849) since looking up
44 // variance requires computing types which can require
45 // performing trait matching (which then performs equality
48 relate::relate_substs(self, None, a_subst, b_subst)
51 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
55 -> RelateResult<'tcx, T>
60 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
61 debug!("{}.tys({:?}, {:?})", self.tag(),
63 if a == b { return Ok(a); }
65 let infcx = self.fields.infcx;
66 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
67 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
69 debug!("{}.tys: replacements ({:?}, {:?})", self.tag(), a, b);
71 match (&a.sty, &b.sty) {
72 (&ty::Infer(TyVar(a_id)), &ty::Infer(TyVar(b_id))) => {
73 infcx.type_variables.borrow_mut().equate(a_id, b_id);
76 (&ty::Infer(TyVar(a_id)), _) => {
77 self.fields.instantiate(b, RelationDir::EqTo, a_id, self.a_is_expected)?;
80 (_, &ty::Infer(TyVar(b_id))) => {
81 self.fields.instantiate(a, RelationDir::EqTo, b_id, self.a_is_expected)?;
85 self.fields.infcx.super_combine_tys(self, a, b)?;
92 fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
93 -> RelateResult<'tcx, ty::Region<'tcx>> {
94 debug!("{}.regions({:?}, {:?})",
98 let origin = Subtype(self.fields.trace.clone());
99 self.fields.infcx.borrow_region_constraints()
100 .make_eqregion(origin, a, b);
106 a: &'tcx ty::LazyConst<'tcx>,
107 b: &'tcx ty::LazyConst<'tcx>,
108 ) -> RelateResult<'tcx, &'tcx ty::LazyConst<'tcx>> {
109 debug!("{}.consts({:?}, {:?})", self.tag(), a, b);
110 if a == b { return Ok(a); }
112 let infcx = self.fields.infcx;
113 let a = infcx.const_unification_table.borrow_mut().replace_if_possible(a);
114 let b = infcx.const_unification_table.borrow_mut().replace_if_possible(b);
115 let a_is_expected = self.a_is_expected();
116 if let (&ty::LazyConst::Evaluated(a_eval), &ty::LazyConst::Evaluated(b_eval)) = (a, b) {
117 match (a_eval.val, b_eval.val) {
118 (ConstValue::Infer(InferConst::Var(a_vid)),
119 ConstValue::Infer(InferConst::Var(b_vid))) => {
120 infcx.const_unification_table
122 .unify_var_var(a_vid, b_vid)
123 .map_err(|e| const_unification_error(a_is_expected, e))?;
127 (ConstValue::Infer(InferConst::Var(a_id)), _) => {
128 self.fields.infcx.unify_const_variable(a_is_expected, a_id, b)?;
132 (_, ConstValue::Infer(InferConst::Var(b_id))) => {
133 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)