]>
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 | ||
a7813a04 | 11 | use super::combine::CombineFields; |
c34b1796 AL |
12 | use super::{Subtype}; |
13 | use super::type_variable::{EqTo}; | |
14 | ||
54a0048b SL |
15 | use ty::{self, Ty, TyCtxt}; |
16 | use ty::TyVar; | |
17 | use ty::relate::{Relate, RelateResult, TypeRelation}; | |
1a4d82fc | 18 | |
b039eaaf | 19 | /// Ensures `a` is made equal to `b`. Returns `a` on success. |
5bcae85e SL |
20 | pub struct Equate<'combine, 'infcx: 'combine, 'gcx: 'infcx+'tcx, 'tcx: 'infcx> { |
21 | fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, | |
22 | a_is_expected: bool, | |
1a4d82fc JJ |
23 | } |
24 | ||
5bcae85e SL |
25 | impl<'combine, 'infcx, 'gcx, 'tcx> Equate<'combine, 'infcx, 'gcx, 'tcx> { |
26 | pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) | |
27 | -> Equate<'combine, 'infcx, 'gcx, 'tcx> | |
28 | { | |
29 | Equate { fields: fields, a_is_expected: a_is_expected } | |
54a0048b | 30 | } |
c34b1796 | 31 | } |
1a4d82fc | 32 | |
5bcae85e SL |
33 | impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> |
34 | for Equate<'combine, 'infcx, 'gcx, 'tcx> | |
35 | { | |
c34b1796 | 36 | fn tag(&self) -> &'static str { "Equate" } |
1a4d82fc | 37 | |
5bcae85e | 38 | fn tcx(&self) -> TyCtxt<'infcx, 'gcx, 'tcx> { self.fields.tcx() } |
1a4d82fc | 39 | |
5bcae85e | 40 | fn a_is_expected(&self) -> bool { self.a_is_expected } |
1a4d82fc | 41 | |
a7813a04 XL |
42 | fn relate_with_variance<T: Relate<'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; | |
54a0048b SL |
57 | let a = infcx.type_variables.borrow_mut().replace_if_possible(a); |
58 | let b = infcx.type_variables.borrow_mut().replace_if_possible(b); | |
1a4d82fc | 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)), _) => { |
5bcae85e | 66 | self.fields.instantiate(b, EqTo, a_id, self.a_is_expected)?; |
1a4d82fc JJ |
67 | Ok(a) |
68 | } | |
69 | ||
62682a34 | 70 | (_, &ty::TyInfer(TyVar(b_id))) => { |
5bcae85e | 71 | self.fields.instantiate(a, EqTo, b_id, self.a_is_expected)?; |
1a4d82fc JJ |
72 | Ok(a) |
73 | } | |
74 | ||
75 | _ => { | |
a7813a04 | 76 | self.fields.infcx.super_combine_tys(self, a, b)?; |
b039eaaf | 77 | Ok(a) |
1a4d82fc JJ |
78 | } |
79 | } | |
80 | } | |
81 | ||
9e0c209e SL |
82 | fn regions(&mut self, a: &'tcx ty::Region, b: &'tcx ty::Region) |
83 | -> RelateResult<'tcx, &'tcx ty::Region> { | |
62682a34 | 84 | debug!("{}.regions({:?}, {:?})", |
c34b1796 | 85 | self.tag(), |
62682a34 SL |
86 | a, |
87 | b); | |
c34b1796 AL |
88 | let origin = Subtype(self.fields.trace.clone()); |
89 | self.fields.infcx.region_vars.make_eqregion(origin, a, b); | |
90 | Ok(a) | |
91 | } | |
92 | ||
93 | fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>) | |
94 | -> RelateResult<'tcx, ty::Binder<T>> | |
a7813a04 | 95 | where T: Relate<'tcx> |
1a4d82fc | 96 | { |
5bcae85e SL |
97 | self.fields.higher_ranked_sub(a, b, self.a_is_expected)?; |
98 | self.fields.higher_ranked_sub(b, a, self.a_is_expected) | |
1a4d82fc JJ |
99 | } |
100 | } |