2014-07-22 07:46:36 -04:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-03 22:42:21 -05:00
|
|
|
use middle::ty::{self, Ty};
|
2014-07-22 07:46:36 -04:00
|
|
|
use middle::ty::TyVar;
|
2014-11-25 16:59:02 -05:00
|
|
|
use middle::infer::combine::*;
|
2015-03-09 16:39:50 -04:00
|
|
|
use middle::infer::CombineResult;
|
2015-03-28 02:23:20 -07:00
|
|
|
use middle::infer::Subtype;
|
|
|
|
use middle::infer::type_variable::EqTo;
|
|
|
|
use util::ppaux::Repr;
|
2014-07-22 07:46:36 -04:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct Equate<'f, 'tcx: 'f> {
|
|
|
|
fields: CombineFields<'f, 'tcx>
|
2014-07-22 07:46:36 -04:00
|
|
|
}
|
|
|
|
|
2014-07-21 15:27:59 +12:00
|
|
|
#[allow(non_snake_case)]
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn Equate<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Equate<'f, 'tcx> {
|
2014-07-22 07:46:36 -04:00
|
|
|
Equate { fields: cf }
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'f, 'tcx> Combine<'tcx> for Equate<'f, 'tcx> {
|
2015-02-12 05:16:02 -05:00
|
|
|
fn tag(&self) -> String { "Equate".to_string() }
|
|
|
|
fn fields<'a>(&'a self) -> &'a CombineFields<'a, 'tcx> { &self.fields }
|
2014-07-22 07:46:36 -04:00
|
|
|
|
2015-02-12 05:16:02 -05:00
|
|
|
fn tys_with_variance(&self, _: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
|
2015-03-09 16:39:50 -04:00
|
|
|
-> CombineResult<'tcx, Ty<'tcx>>
|
2015-02-12 05:16:02 -05:00
|
|
|
{
|
|
|
|
// Once we're equating, it doesn't matter what the variance is.
|
2014-07-22 07:46:36 -04:00
|
|
|
self.tys(a, b)
|
|
|
|
}
|
|
|
|
|
2015-02-12 05:16:02 -05:00
|
|
|
fn regions_with_variance(&self, _: ty::Variance, a: ty::Region, b: ty::Region)
|
2015-03-09 16:39:50 -04:00
|
|
|
-> CombineResult<'tcx, ty::Region>
|
2015-02-12 05:16:02 -05:00
|
|
|
{
|
|
|
|
// Once we're equating, it doesn't matter what the variance is.
|
2014-07-22 07:46:36 -04:00
|
|
|
self.regions(a, b)
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:39:50 -04:00
|
|
|
fn regions(&self, a: ty::Region, b: ty::Region) -> CombineResult<'tcx, ty::Region> {
|
2014-07-22 07:46:36 -04:00
|
|
|
debug!("{}.regions({}, {})",
|
|
|
|
self.tag(),
|
|
|
|
a.repr(self.fields.infcx.tcx),
|
|
|
|
b.repr(self.fields.infcx.tcx));
|
|
|
|
self.infcx().region_vars.make_eqregion(Subtype(self.trace()), a, b);
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:39:50 -04:00
|
|
|
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CombineResult<'tcx, Ty<'tcx>> {
|
2014-07-22 07:46:36 -04:00
|
|
|
debug!("{}.tys({}, {})", self.tag(),
|
|
|
|
a.repr(self.fields.infcx.tcx), b.repr(self.fields.infcx.tcx));
|
|
|
|
if a == b { return Ok(a); }
|
|
|
|
|
|
|
|
let infcx = self.fields.infcx;
|
|
|
|
let a = infcx.type_variables.borrow().replace_if_possible(a);
|
|
|
|
let b = infcx.type_variables.borrow().replace_if_possible(b);
|
2014-10-31 10:51:16 +02:00
|
|
|
match (&a.sty, &b.sty) {
|
2014-07-22 07:46:36 -04:00
|
|
|
(&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
|
|
|
|
infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
(&ty::ty_infer(TyVar(a_id)), _) => {
|
|
|
|
try!(self.fields.instantiate(b, EqTo, a_id));
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
(_, &ty::ty_infer(TyVar(b_id))) => {
|
|
|
|
try!(self.fields.instantiate(a, EqTo, b_id));
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
super_tys(self, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-09 16:39:50 -04:00
|
|
|
fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> CombineResult<'tcx, ty::Binder<T>>
|
2014-12-12 11:28:35 -05:00
|
|
|
where T : Combineable<'tcx>
|
2014-12-11 13:37:37 -05:00
|
|
|
{
|
2014-12-12 11:28:35 -05:00
|
|
|
try!(self.sub().binders(a, b));
|
|
|
|
self.sub().binders(b, a)
|
2014-11-15 17:09:51 -05:00
|
|
|
}
|
2014-07-22 07:46:36 -04:00
|
|
|
}
|