1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use super::combine::CombineFields;
13 use super::type_variable::{EqTo};
15 use ty::{self, Ty, TyCtxt};
17 use ty::relate::{Relate, RelateResult, TypeRelation};
18 use traits::PredicateObligations;
20 /// Ensures `a` is made equal to `b`. Returns `a` on success.
21 pub struct Equate<'infcx, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> {
22 fields: CombineFields<'infcx, 'gcx, 'tcx>
25 impl<'infcx, 'gcx, 'tcx> Equate<'infcx, 'gcx, 'tcx> {
26 pub fn new(fields: CombineFields<'infcx, 'gcx, 'tcx>) -> Equate<'infcx, 'gcx, 'tcx> {
27 Equate { fields: fields }
30 pub fn obligations(self) -> PredicateObligations<'tcx> {
31 self.fields.obligations
35 impl<'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> for Equate<'infcx, 'gcx, 'tcx> {
36 fn tag(&self) -> &'static str { "Equate" }
38 fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() }
40 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
42 fn relate_with_variance<T: Relate<'tcx>>(&mut self,
46 -> RelateResult<'tcx, T>
51 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
52 debug!("{}.tys({:?}, {:?})", self.tag(),
54 if a == b { return Ok(a); }
56 let infcx = self.fields.infcx;
57 let a = infcx.type_variables.borrow_mut().replace_if_possible(a);
58 let b = infcx.type_variables.borrow_mut().replace_if_possible(b);
59 match (&a.sty, &b.sty) {
60 (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
61 infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
65 (&ty::TyInfer(TyVar(a_id)), _) => {
66 self.fields.instantiate(b, EqTo, a_id)?;
70 (_, &ty::TyInfer(TyVar(b_id))) => {
71 self.fields.instantiate(a, EqTo, b_id)?;
76 self.fields.infcx.super_combine_tys(self, a, b)?;
82 fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
83 debug!("{}.regions({:?}, {:?})",
87 let origin = Subtype(self.fields.trace.clone());
88 self.fields.infcx.region_vars.make_eqregion(origin, a, b);
92 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
93 -> RelateResult<'tcx, ty::Binder<T>>
96 self.fields.higher_ranked_sub(a, b)?;
97 self.fields.higher_ranked_sub(b, a)