]>
Commit | Line | Data |
---|---|---|
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 |
11 | use super::combine::{self, CombineFields}; |
12 | use super::higher_ranked::HigherRankedRelations; | |
13 | use super::{Subtype}; | |
14 | use super::type_variable::{EqTo}; | |
15 | ||
1a4d82fc JJ |
16 | use middle::ty::{self, Ty}; |
17 | use middle::ty::TyVar; | |
e9174d1e | 18 | use middle::ty::relate::{Relate, RelateResult, TypeRelation}; |
1a4d82fc | 19 | |
b039eaaf | 20 | /// Ensures `a` is made equal to `b`. Returns `a` on success. |
c34b1796 AL |
21 | pub struct Equate<'a, 'tcx: 'a> { |
22 | fields: CombineFields<'a, 'tcx> | |
1a4d82fc JJ |
23 | } |
24 | ||
c34b1796 AL |
25 | impl<'a, 'tcx> Equate<'a, 'tcx> { |
26 | pub fn new(fields: CombineFields<'a, 'tcx>) -> Equate<'a, 'tcx> { | |
27 | Equate { fields: fields } | |
1a4d82fc | 28 | } |
c34b1796 | 29 | } |
1a4d82fc | 30 | |
c34b1796 AL |
31 | impl<'a, 'tcx> TypeRelation<'a,'tcx> for Equate<'a, 'tcx> { |
32 | fn tag(&self) -> &'static str { "Equate" } | |
1a4d82fc | 33 | |
c34b1796 | 34 | fn tcx(&self) -> &'a ty::ctxt<'tcx> { self.fields.tcx() } |
1a4d82fc | 35 | |
c34b1796 | 36 | fn a_is_expected(&self) -> bool { self.fields.a_is_expected } |
1a4d82fc | 37 | |
c34b1796 AL |
38 | fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self, |
39 | _: ty::Variance, | |
40 | a: &T, | |
41 | b: &T) | |
42 | -> RelateResult<'tcx, T> | |
1a4d82fc | 43 | { |
c34b1796 | 44 | self.relate(a, b) |
1a4d82fc JJ |
45 | } |
46 | ||
c34b1796 | 47 | fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { |
62682a34 SL |
48 | debug!("{}.tys({:?}, {:?})", self.tag(), |
49 | a, b); | |
1a4d82fc JJ |
50 | if a == b { return Ok(a); } |
51 | ||
52 | let infcx = self.fields.infcx; | |
53 | let a = infcx.type_variables.borrow().replace_if_possible(a); | |
54 | let b = infcx.type_variables.borrow().replace_if_possible(b); | |
55 | match (&a.sty, &b.sty) { | |
62682a34 | 56 | (&ty::TyInfer(TyVar(a_id)), &ty::TyInfer(TyVar(b_id))) => { |
1a4d82fc JJ |
57 | infcx.type_variables.borrow_mut().relate_vars(a_id, EqTo, b_id); |
58 | Ok(a) | |
59 | } | |
60 | ||
62682a34 | 61 | (&ty::TyInfer(TyVar(a_id)), _) => { |
1a4d82fc JJ |
62 | try!(self.fields.instantiate(b, EqTo, a_id)); |
63 | Ok(a) | |
64 | } | |
65 | ||
62682a34 | 66 | (_, &ty::TyInfer(TyVar(b_id))) => { |
1a4d82fc JJ |
67 | try!(self.fields.instantiate(a, EqTo, b_id)); |
68 | Ok(a) | |
69 | } | |
70 | ||
71 | _ => { | |
b039eaaf SL |
72 | try!(combine::super_combine_tys(self.fields.infcx, self, a, b)); |
73 | Ok(a) | |
1a4d82fc JJ |
74 | } |
75 | } | |
76 | } | |
77 | ||
c34b1796 | 78 | fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> { |
62682a34 | 79 | debug!("{}.regions({:?}, {:?})", |
c34b1796 | 80 | self.tag(), |
62682a34 SL |
81 | a, |
82 | b); | |
c34b1796 AL |
83 | let origin = Subtype(self.fields.trace.clone()); |
84 | self.fields.infcx.region_vars.make_eqregion(origin, a, b); | |
85 | Ok(a) | |
86 | } | |
87 | ||
88 | fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>) | |
89 | -> RelateResult<'tcx, ty::Binder<T>> | |
90 | where T: Relate<'a, 'tcx> | |
1a4d82fc | 91 | { |
c34b1796 AL |
92 | try!(self.fields.higher_ranked_sub(a, b)); |
93 | self.fields.higher_ranked_sub(b, a) | |
1a4d82fc JJ |
94 | } |
95 | } |