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::higher_ranked::HigherRankedRelations;
17 use middle::ty::{BuiltinBounds};
18 use middle::ty::{self, Ty};
19 use syntax::ast::{MutImmutable, MutMutable, Unsafety};
20 use util::ppaux::mt_to_string;
21 use util::ppaux::Repr;
23 /// "Greatest lower bound" (common subtype)
24 pub struct Glb<'f, 'tcx: 'f> {
25 fields: CombineFields<'f, 'tcx>
28 #[allow(non_snake_case)]
29 pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
33 impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
34 fn tag(&self) -> String { "Glb".to_string() }
35 fn fields<'a>(&'a self) -> &'a CombineFields<'a, 'tcx> { &self.fields }
37 fn tys_with_variance(&self, v: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
38 -> cres<'tcx, Ty<'tcx>>
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.lub().tys(a, b),
48 fn regions_with_variance(&self, v: ty::Variance, a: ty::Region, b: ty::Region)
49 -> cres<'tcx, ty::Region>
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.lub().regions(a, b),
59 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
60 let tcx = self.fields.infcx.tcx;
62 debug!("{}.mts({}, {})",
65 mt_to_string(tcx, b));
67 match (a.mutbl, b.mutbl) {
68 // If one side or both is mut, then the GLB must use
69 // the precise type from the mut side.
70 (MutMutable, MutMutable) => {
71 let t = try!(self.equate().tys(a.ty, b.ty));
72 Ok(ty::mt {ty: t, mutbl: MutMutable})
75 // If one side or both is immutable, we can use the GLB of
76 // both sides but mutbl must be `MutImmutable`.
77 (MutImmutable, MutImmutable) => {
78 let t = try!(self.tys(a.ty, b.ty));
79 Ok(ty::mt {ty: t, mutbl: MutImmutable})
82 // There is no mutual subtype of these combinations.
83 (MutMutable, MutImmutable) |
84 (MutImmutable, MutMutable) => {
85 Err(ty::terr_mutability)
90 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
92 (Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
93 (Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
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 tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
116 super_lattice_tys(self, a, b)
119 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
120 where T : Combineable<'tcx>
122 self.higher_ranked_glb(a, b)