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::higher_ranked::HigherRankedRelations;
13 use super::lattice::*;
17 use middle::ty::{BuiltinBounds};
18 use middle::ty::{self, Ty};
19 use syntax::ast::{MutMutable, MutImmutable, Unsafety};
20 use util::ppaux::mt_to_string;
21 use util::ppaux::Repr;
23 /// "Least upper bound" (common supertype)
24 pub struct Lub<'f, 'tcx: 'f> {
25 fields: CombineFields<'f, 'tcx>
28 #[allow(non_snake_case)]
29 pub fn Lub<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Lub<'f, 'tcx> {
33 impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
34 fn tag(&self) -> String { "Lub".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.glb().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.glb().regions(a, b),
59 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
62 debug!("{}.mts({}, {})",
65 mt_to_string(tcx, b));
67 if a.mutbl != b.mutbl {
68 return Err(ty::terr_mutability)
74 let t = try!(self.tys(a.ty, b.ty));
75 Ok(ty::mt {ty: t, mutbl: m})
79 let t = try!(self.equate().tys(a.ty, b.ty));
80 Ok(ty::mt {ty: t, mutbl: m})
85 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
87 (Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
88 (Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
92 fn builtin_bounds(&self,
95 -> cres<'tcx, ty::BuiltinBounds> {
96 // More bounds is a subtype of fewer bounds, so
97 // the LUB (mutual supertype) is the intersection.
101 fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
102 debug!("{}.regions({}, {})",
107 Ok(self.infcx().region_vars.lub_regions(Subtype(self.trace()), a, b))
110 fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
111 super_lattice_tys(self, a, b)
114 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
115 where T : Combineable<'tcx>
117 self.higher_ranked_lub(a, b)