]> git.proxmox.com Git - rustc.git/blame - src/librustc/middle/infer/equate.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / librustc / middle / infer / equate.rs
CommitLineData
1a4d82fc
JJ
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.
4//
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.
10
c34b1796
AL
11use super::combine::{self, CombineFields};
12use super::higher_ranked::HigherRankedRelations;
13use super::{Subtype};
14use super::type_variable::{EqTo};
15
1a4d82fc
JJ
16use middle::ty::{self, Ty};
17use middle::ty::TyVar;
c34b1796 18use middle::ty_relate::{Relate, RelateResult, TypeRelation};
1a4d82fc 19
c34b1796
AL
20pub struct Equate<'a, 'tcx: 'a> {
21 fields: CombineFields<'a, 'tcx>
1a4d82fc
JJ
22}
23
c34b1796
AL
24impl<'a, 'tcx> Equate<'a, 'tcx> {
25 pub fn new(fields: CombineFields<'a, 'tcx>) -> Equate<'a, 'tcx> {
26 Equate { fields: fields }
1a4d82fc 27 }
c34b1796 28}
1a4d82fc 29
c34b1796
AL
30impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> {
31 fn tag(&self) -> &'static str { "Equate" }
1a4d82fc 32
c34b1796 33 fn tcx(&self) -> &'a ty::ctxt<'tcx> { self.fields.tcx() }
1a4d82fc 34
c34b1796 35 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
1a4d82fc 36
62682a34
SL
37 fn will_change(&mut self, a: bool, b: bool) -> bool {
38 // if either side changed from what it was, that could cause equality to fail
39 a || b
40 }
41
c34b1796
AL
42 fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
43 _: ty::Variance,
44 a: &T,
45 b: &T)
46 -> RelateResult<'tcx, T>
1a4d82fc 47 {
c34b1796 48 self.relate(a, b)
1a4d82fc
JJ
49 }
50
c34b1796 51 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
62682a34
SL
52 debug!("{}.tys({:?}, {:?})", self.tag(),
53 a, b);
1a4d82fc
JJ
54 if a == b { return Ok(a); }
55
56 let infcx = self.fields.infcx;
57 let a = infcx.type_variables.borrow().replace_if_possible(a);
58 let b = infcx.type_variables.borrow().replace_if_possible(b);
59 match (&a.sty, &b.sty) {
62682a34 60 (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => {
1a4d82fc
JJ
61 infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id);
62 Ok(a)
63 }
64
62682a34 65 (&ty::TyInfer(TyVar(a_id)), _) => {
1a4d82fc
JJ
66 try!(self.fields.instantiate(b, EqTo, a_id));
67 Ok(a)
68 }
69
62682a34 70 (_, &ty::TyInfer(TyVar(b_id))) => {
1a4d82fc
JJ
71 try!(self.fields.instantiate(a, EqTo, b_id));
72 Ok(a)
73 }
74
75 _ => {
c34b1796 76 combine::super_combine_tys(self.fields.infcx, self, a, b)
1a4d82fc
JJ
77 }
78 }
79 }
80
c34b1796 81 fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
62682a34 82 debug!("{}.regions({:?}, {:?})",
c34b1796 83 self.tag(),
62682a34
SL
84 a,
85 b);
c34b1796
AL
86 let origin = Subtype(self.fields.trace.clone());
87 self.fields.infcx.region_vars.make_eqregion(origin, a, b);
88 Ok(a)
89 }
90
91 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
92 -> RelateResult<'tcx, ty::Binder<T>>
93 where T: Relate<'a, 'tcx>
1a4d82fc 94 {
c34b1796
AL
95 try!(self.fields.higher_ranked_sub(a, b));
96 self.fields.higher_ranked_sub(b, a)
1a4d82fc
JJ
97 }
98}