]> git.proxmox.com Git - rustc.git/blob - src/librustc/infer/glb.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / librustc / infer / glb.rs
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
11 use super::combine::CombineFields;
12 use super::higher_ranked::HigherRankedRelations;
13 use super::InferCtxt;
14 use super::lattice::{self, LatticeDir};
15 use super::Subtype;
16
17 use ty::{self, Ty, TyCtxt};
18 use ty::relate::{Relate, RelateResult, TypeRelation};
19 use traits::PredicateObligations;
20
21 /// "Greatest lower bound" (common subtype)
22 pub struct Glb<'a, 'tcx: 'a> {
23 fields: CombineFields<'a, 'tcx>
24 }
25
26 impl<'a, 'tcx> Glb<'a, 'tcx> {
27 pub fn new(fields: CombineFields<'a, 'tcx>) -> Glb<'a, 'tcx> {
28 Glb { fields: fields }
29 }
30
31 pub fn obligations(self) -> PredicateObligations<'tcx> {
32 self.fields.obligations
33 }
34 }
35
36 impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Glb<'a, 'tcx> {
37 fn tag(&self) -> &'static str { "Glb" }
38
39 fn tcx(&self) -> &'a TyCtxt<'tcx> { self.fields.tcx() }
40
41 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
42
43 fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
44 variance: ty::Variance,
45 a: &T,
46 b: &T)
47 -> RelateResult<'tcx, T>
48 {
49 match variance {
50 ty::Invariant => self.fields.equate().relate(a, b),
51 ty::Covariant => self.relate(a, b),
52 ty::Bivariant => self.fields.bivariate().relate(a, b),
53 ty::Contravariant => self.fields.lub().relate(a, b),
54 }
55 }
56
57 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
58 lattice::super_lattice_tys(self, a, b)
59 }
60
61 fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
62 debug!("{}.regions({:?}, {:?})",
63 self.tag(),
64 a,
65 b);
66
67 let origin = Subtype(self.fields.trace.clone());
68 Ok(self.fields.infcx.region_vars.glb_regions(origin, a, b))
69 }
70
71 fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
72 -> RelateResult<'tcx, ty::Binder<T>>
73 where T: Relate<'a, 'tcx>
74 {
75 self.fields.higher_ranked_glb(a, b)
76 }
77 }
78
79 impl<'a, 'tcx> LatticeDir<'a,'tcx> for Glb<'a, 'tcx> {
80 fn infcx(&self) -> &'a InferCtxt<'a,'tcx> {
81 self.fields.infcx
82 }
83
84 fn relate_bound(&self, v: Ty<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> {
85 let mut sub = self.fields.sub();
86 sub.relate(&v, &a)?;
87 sub.relate(&v, &b)?;
88 Ok(())
89 }
90 }