]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/glb.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / librustc / middle / 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::*;
12 use super::lattice::*;
13 use super::higher_ranked::HigherRankedRelations;
14 use super::{cres};
15 use super::Subtype;
16
17 use middle::ty::{BuiltinBounds};
18 use middle::ty::{self, Ty};
19 use syntax::ast::{MutImmutable, MutMutable, Unsafety};
20 use util::ppaux::mt_to_string;
21 use util::ppaux::Repr;
22
23 /// "Greatest lower bound" (common subtype)
24 pub struct Glb<'f, 'tcx: 'f> {
25     fields: CombineFields<'f, 'tcx>
26 }
27
28 #[allow(non_snake_case)]
29 pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
30     Glb { fields: cf }
31 }
32
33 impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
34     fn tag(&self) -> String { "Glb".to_string() }
35     fn fields<'a>(&'a self) -> &'a CombineFields<'a, 'tcx> { &self.fields }
36
37     fn tys_with_variance(&self, v: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
38                          -> cres<'tcx, Ty<'tcx>>
39     {
40         match v {
41             ty::Invariant => self.equate().tys(a, b),
42             ty::Covariant => self.tys(a, b),
43             ty::Bivariant => self.bivariate().tys(a, b),
44             ty::Contravariant => self.lub().tys(a, b),
45         }
46     }
47
48     fn regions_with_variance(&self, v: ty::Variance, a: ty::Region, b: ty::Region)
49                              -> cres<'tcx, ty::Region>
50     {
51         match v {
52             ty::Invariant => self.equate().regions(a, b),
53             ty::Covariant => self.regions(a, b),
54             ty::Bivariant => self.bivariate().regions(a, b),
55             ty::Contravariant => self.lub().regions(a, b),
56         }
57     }
58
59     fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
60         let tcx = self.fields.infcx.tcx;
61
62         debug!("{}.mts({}, {})",
63                self.tag(),
64                mt_to_string(tcx, a),
65                mt_to_string(tcx, b));
66
67         match (a.mutbl, b.mutbl) {
68             // If one side or both is mut, then the GLB must use
69             // the precise type from the mut side.
70             (MutMutable, MutMutable) => {
71                 let t = try!(self.equate().tys(a.ty, b.ty));
72                 Ok(ty::mt {ty: t, mutbl: MutMutable})
73             }
74
75             // If one side or both is immutable, we can use the GLB of
76             // both sides but mutbl must be `MutImmutable`.
77             (MutImmutable, MutImmutable) => {
78                 let t = try!(self.tys(a.ty, b.ty));
79                 Ok(ty::mt {ty: t, mutbl: MutImmutable})
80             }
81
82             // There is no mutual subtype of these combinations.
83             (MutMutable, MutImmutable) |
84             (MutImmutable, MutMutable) => {
85                 Err(ty::terr_mutability)
86             }
87         }
88     }
89
90     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
91         match (a, b) {
92           (Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
93           (Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
94         }
95     }
96
97     fn builtin_bounds(&self,
98                       a: ty::BuiltinBounds,
99                       b: ty::BuiltinBounds)
100                       -> cres<'tcx, ty::BuiltinBounds> {
101         // More bounds is a subtype of fewer bounds, so
102         // the GLB (mutual subtype) is the union.
103         Ok(a.union(b))
104     }
105
106     fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
107         debug!("{}.regions({}, {})",
108                self.tag(),
109                a.repr(self.fields.infcx.tcx),
110                b.repr(self.fields.infcx.tcx));
111
112         Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
113     }
114
115     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
116         super_lattice_tys(self, a, b)
117     }
118
119     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
120         where T : Combineable<'tcx>
121     {
122         self.higher_ranked_glb(a, b)
123     }
124 }