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::equate::Equate;
14 use super::higher_ranked::HigherRankedRelations;
15 use super::lattice::*;
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};
23 use syntax::ast::{Onceness, Unsafety};
24 use syntax::ast::{MutMutable, MutImmutable};
25 use util::ppaux::mt_to_string;
26 use util::ppaux::Repr;
28 /// "Least upper bound" (common supertype)
29 pub struct Lub<'f, 'tcx: 'f> {
30 fields: CombineFields<'f, 'tcx>
33 #[allow(non_snake_case)]
34 pub fn Lub<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Lub<'f, 'tcx> {
38 impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
39 fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx> { self.fields.infcx }
40 fn tag(&self) -> String { "lub".to_string() }
41 fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
42 fn trace(&self) -> TypeTrace<'tcx> { self.fields.trace.clone() }
44 fn equate<'a>(&'a self) -> Equate<'a, 'tcx> { Equate(self.fields.clone()) }
45 fn sub<'a>(&'a self) -> Sub<'a, 'tcx> { Sub(self.fields.clone()) }
46 fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields.clone()) }
47 fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields.clone()) }
49 fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
52 debug!("{}.mts({}, {})",
55 mt_to_string(tcx, b));
57 if a.mutbl != b.mutbl {
58 return Err(ty::terr_mutability)
64 let t = try!(self.tys(a.ty, b.ty));
65 Ok(ty::mt {ty: t, mutbl: m})
69 let t = try!(self.equate().tys(a.ty, b.ty));
70 Ok(ty::mt {ty: t, mutbl: m})
75 fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
79 fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
81 (Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
82 (Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
86 fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness> {
88 (Once, _) | (_, Once) => Ok(Once),
89 (Many, Many) => Ok(Many)
93 fn builtin_bounds(&self,
96 -> cres<'tcx, ty::BuiltinBounds> {
97 // More bounds is a subtype of fewer bounds, so
98 // the LUB (mutual supertype) is the intersection.
102 fn contraregions(&self, a: ty::Region, b: ty::Region)
103 -> cres<'tcx, ty::Region> {
104 self.glb().regions(a, b)
107 fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
108 debug!("{}.regions({}, {})",
113 Ok(self.infcx().region_vars.lub_regions(Subtype(self.trace()), a, b))
116 fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
117 super_lattice_tys(self, a, b)
120 fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
121 where T : Combineable<'tcx>
123 self.higher_ranked_lub(a, b)