]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2012 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 | 11 | use super::combine::CombineFields; |
c34b1796 AL |
12 | use super::InferCtxt; |
13 | use super::lattice::{self, LatticeDir}; | |
85aaf69f | 14 | use super::Subtype; |
1a4d82fc | 15 | |
54a0048b SL |
16 | use ty::{self, Ty, TyCtxt}; |
17 | use ty::relate::{Relate, RelateResult, TypeRelation}; | |
1a4d82fc JJ |
18 | |
19 | /// "Greatest lower bound" (common subtype) | |
5bcae85e SL |
20 | pub struct Glb<'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> Glb<'combine, 'infcx, 'gcx, 'tcx> { |
26 | pub fn new(fields: &'combine mut CombineFields<'infcx, 'gcx, 'tcx>, a_is_expected: bool) | |
27 | -> Glb<'combine, 'infcx, 'gcx, 'tcx> | |
28 | { | |
29 | Glb { fields: fields, a_is_expected: a_is_expected } | |
54a0048b | 30 | } |
c34b1796 | 31 | } |
85aaf69f | 32 | |
5bcae85e SL |
33 | impl<'combine, 'infcx, 'gcx, 'tcx> TypeRelation<'infcx, 'gcx, 'tcx> |
34 | for Glb<'combine, 'infcx, 'gcx, 'tcx> | |
35 | { | |
c34b1796 | 36 | fn tag(&self) -> &'static str { "Glb" } |
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 | variance: ty::Variance, | |
44 | a: &T, | |
45 | b: &T) | |
46 | -> RelateResult<'tcx, T> | |
c34b1796 AL |
47 | { |
48 | match variance { | |
5bcae85e | 49 | ty::Invariant => self.fields.equate(self.a_is_expected).relate(a, b), |
c34b1796 | 50 | ty::Covariant => self.relate(a, b), |
5bcae85e SL |
51 | ty::Bivariant => self.fields.bivariate(self.a_is_expected).relate(a, b), |
52 | ty::Contravariant => self.fields.lub(self.a_is_expected).relate(a, b), | |
1a4d82fc JJ |
53 | } |
54 | } | |
55 | ||
c34b1796 AL |
56 | fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { |
57 | lattice::super_lattice_tys(self, a, b) | |
1a4d82fc JJ |
58 | } |
59 | ||
c34b1796 | 60 | fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> { |
62682a34 | 61 | debug!("{}.regions({:?}, {:?})", |
1a4d82fc | 62 | self.tag(), |
62682a34 SL |
63 | a, |
64 | b); | |
1a4d82fc | 65 | |
c34b1796 AL |
66 | let origin = Subtype(self.fields.trace.clone()); |
67 | Ok(self.fields.infcx.region_vars.glb_regions(origin, a, b)) | |
1a4d82fc JJ |
68 | } |
69 | ||
c34b1796 AL |
70 | fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>) |
71 | -> RelateResult<'tcx, ty::Binder<T>> | |
a7813a04 | 72 | where T: Relate<'tcx> |
c34b1796 | 73 | { |
5bcae85e | 74 | self.fields.higher_ranked_glb(a, b, self.a_is_expected) |
1a4d82fc | 75 | } |
c34b1796 | 76 | } |
1a4d82fc | 77 | |
5bcae85e SL |
78 | impl<'combine, 'infcx, 'gcx, 'tcx> LatticeDir<'infcx, 'gcx, 'tcx> |
79 | for Glb<'combine, 'infcx, 'gcx, 'tcx> | |
80 | { | |
81 | fn infcx(&self) -> &'infcx InferCtxt<'infcx, 'gcx, 'tcx> { | |
c34b1796 AL |
82 | self.fields.infcx |
83 | } | |
84 | ||
5bcae85e SL |
85 | fn relate_bound(&mut self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> { |
86 | let mut sub = self.fields.sub(self.a_is_expected); | |
54a0048b SL |
87 | sub.relate(&v, &a)?; |
88 | sub.relate(&v, &b)?; | |
c34b1796 | 89 | Ok(()) |
1a4d82fc JJ |
90 | } |
91 | } |