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