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::{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;
27 /// "Greatest lower bound" (common subtype)
28 pub struct Glb<'f, 'tcx: 'f> {
29 fields: CombineFields<'f, 'tcx>
32 #[allow(non_snake_case)]
33 pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
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() }
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()) }
48 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
49 let tcx = self.fields.infcx.tcx;
51 debug!("{}.mts({}, {})",
54 mt_to_string(tcx, b));
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})
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})
71 // There is no mutual subtype of these combinations.
72 (MutMutable, MutImmutable) |
73 (MutImmutable, MutMutable) => {
74 Err(ty::terr_mutability)
79 fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
83 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
85 (Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
86 (Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
90 fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness> {
92 (Many, _) | (_, Many) => Ok(Many),
93 (Once, Once) => Ok(Once)
97 fn builtin_bounds(&self,
100 -> cres<'tcx, ty::BuiltinBounds> {
101 // More bounds is a subtype of fewer bounds, so
102 // the GLB (mutual subtype) is the union.
106 fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
107 debug!("{}.regions({}, {})",
109 a.repr(self.fields.infcx.tcx),
110 b.repr(self.fields.infcx.tcx));
112 Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
115 fn contraregions(&self, a: ty::Region, b: ty::Region)
116 -> cres<'tcx, ty::Region> {
117 self.lub().regions(a, b)
120 fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
121 super_lattice_tys(self, a, b)
124 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
125 where T : Combineable<'tcx>
127 self.higher_ranked_glb(a, b)