]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/lub.rs
Auto merge of #22541 - Manishearth:rollup, r=Gankro
[rust.git] / src / librustc / middle / infer / lub.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::higher_ranked::HigherRankedRelations;
13 use super::lattice::*;
14 use super::{cres};
15 use super::{Subtype};
16
17 use middle::ty::{BuiltinBounds};
18 use middle::ty::{self, Ty};
19 use syntax::ast::{MutMutable, MutImmutable, Unsafety};
20 use util::ppaux::mt_to_string;
21 use util::ppaux::Repr;
22
23 /// "Least upper bound" (common supertype)
24 pub struct Lub<'f, 'tcx: 'f> {
25     fields: CombineFields<'f, 'tcx>
26 }
27
28 #[allow(non_snake_case)]
29 pub fn Lub<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Lub<'f, 'tcx> {
30     Lub { fields: cf }
31 }
32
33 impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
34     fn tag(&self) -> String { "Lub".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.glb().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.glb().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.tcx();
61
62         debug!("{}.mts({}, {})",
63                self.tag(),
64                mt_to_string(tcx, a),
65                mt_to_string(tcx, b));
66
67         if a.mutbl != b.mutbl {
68             return Err(ty::terr_mutability)
69         }
70
71         let m = a.mutbl;
72         match m {
73             MutImmutable => {
74                 let t = try!(self.tys(a.ty, b.ty));
75                 Ok(ty::mt {ty: t, mutbl: m})
76             }
77
78             MutMutable => {
79                 let t = try!(self.equate().tys(a.ty, b.ty));
80                 Ok(ty::mt {ty: t, mutbl: m})
81             }
82         }
83     }
84
85     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
86         match (a, b) {
87           (Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
88           (Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
89         }
90     }
91
92     fn builtin_bounds(&self,
93                       a: ty::BuiltinBounds,
94                       b: ty::BuiltinBounds)
95                       -> cres<'tcx, ty::BuiltinBounds> {
96         // More bounds is a subtype of fewer bounds, so
97         // the LUB (mutual supertype) is the intersection.
98         Ok(a.intersection(b))
99     }
100
101     fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
102         debug!("{}.regions({}, {})",
103                self.tag(),
104                a.repr(self.tcx()),
105                b.repr(self.tcx()));
106
107         Ok(self.infcx().region_vars.lub_regions(Subtype(self.trace()), a, b))
108     }
109
110     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
111         super_lattice_tys(self, a, b)
112     }
113
114     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
115         where T : Combineable<'tcx>
116     {
117         self.higher_ranked_lub(a, b)
118     }
119 }