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.
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.
11 use super::combine::*;
12 use super::lattice::*;
13 use super::equate::Equate;
14 use super::higher_ranked::HigherRankedRelations;
17 use super::{cres, InferCtxt};
18 use super::{TypeTrace, Subtype};
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;
26 /// "Greatest lower bound" (common subtype)
27 pub struct Glb<'f, 'tcx: 'f> {
28 fields: CombineFields<'f, 'tcx>
31 #[allow(non_snake_case)]
32 pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
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() }
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()) }
47 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
48 let tcx = self.fields.infcx.tcx;
50 debug!("{}.mts({}, {})",
53 mt_to_string(tcx, b));
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})
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})
70 // There is no mutual subtype of these combinations.
71 (MutMutable, MutImmutable) |
72 (MutImmutable, MutMutable) => {
73 Err(ty::terr_mutability)
78 fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
82 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
84 (Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
85 (Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
89 fn builtin_bounds(&self,
92 -> cres<'tcx, ty::BuiltinBounds> {
93 // More bounds is a subtype of fewer bounds, so
94 // the GLB (mutual subtype) is the union.
98 fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
99 debug!("{}.regions({}, {})",
101 a.repr(self.fields.infcx.tcx),
102 b.repr(self.fields.infcx.tcx));
104 Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
107 fn contraregions(&self, a: ty::Region, b: ty::Region)
108 -> cres<'tcx, ty::Region> {
109 self.lub().regions(a, b)
112 fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
113 super_lattice_tys(self, a, b)
116 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
117 where T : Combineable<'tcx>
119 self.higher_ranked_glb(a, b)