]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/infer/glb.rs
doc: remove incomplete sentence
[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::{mod, Ty};
22 use syntax::ast::{Many, Once, MutImmutable, MutMutable};
23 use syntax::ast::{Onceness, Unsafety};
24 use util::ppaux::mt_to_string;
25 use util::ppaux::Repr;
26
27 /// "Greatest lower bound" (common subtype)
28 pub struct Glb<'f, 'tcx: 'f> {
29     fields: CombineFields<'f, 'tcx>
30 }
31
32 #[allow(non_snake_case)]
33 pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
34     Glb { fields: cf }
35 }
36
37 impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
38     fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx> { self.fields.infcx }
39     fn tag(&self) -> String { "glb".to_string() }
40     fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
41     fn trace(&self) -> TypeTrace<'tcx> { self.fields.trace.clone() }
42
43     fn equate<'a>(&'a self) -> Equate<'a, 'tcx> { Equate(self.fields.clone()) }
44     fn sub<'a>(&'a self) -> Sub<'a, 'tcx> { Sub(self.fields.clone()) }
45     fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields.clone()) }
46     fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields.clone()) }
47
48     fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
49         let tcx = self.fields.infcx.tcx;
50
51         debug!("{}.mts({}, {})",
52                self.tag(),
53                mt_to_string(tcx, a),
54                mt_to_string(tcx, b));
55
56         match (a.mutbl, b.mutbl) {
57             // If one side or both is mut, then the GLB must use
58             // the precise type from the mut side.
59             (MutMutable, MutMutable) => {
60                 let t = try!(self.equate().tys(a.ty, b.ty));
61                 Ok(ty::mt {ty: t, mutbl: MutMutable})
62             }
63
64             // If one side or both is immutable, we can use the GLB of
65             // both sides but mutbl must be `MutImmutable`.
66             (MutImmutable, MutImmutable) => {
67                 let t = try!(self.tys(a.ty, b.ty));
68                 Ok(ty::mt {ty: t, mutbl: MutImmutable})
69             }
70
71             // There is no mutual subtype of these combinations.
72             (MutMutable, MutImmutable) |
73             (MutImmutable, MutMutable) => {
74                 Err(ty::terr_mutability)
75             }
76         }
77     }
78
79     fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
80         self.lub().tys(a, b)
81     }
82
83     fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
84         match (a, b) {
85           (Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
86           (Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
87         }
88     }
89
90     fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness> {
91         match (a, b) {
92             (Many, _) | (_, Many) => Ok(Many),
93             (Once, Once) => Ok(Once)
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 contraregions(&self, a: ty::Region, b: ty::Region)
116                     -> cres<'tcx, ty::Region> {
117         self.lub().regions(a, b)
118     }
119
120     fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
121         super_lattice_tys(self, a, b)
122     }
123
124     fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
125         where T : Combineable<'tcx>
126     {
127         self.higher_ranked_glb(a, b)
128     }
129 }