2012-12-03 16:48:01 -08:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 16:59:02 -05:00
|
|
|
use super::combine::*;
|
|
|
|
use super::equate::Equate;
|
|
|
|
use super::glb::Glb;
|
|
|
|
use super::higher_ranked::HigherRankedRelations;
|
|
|
|
use super::lattice::*;
|
|
|
|
use super::sub::Sub;
|
|
|
|
use super::{cres, InferCtxt};
|
|
|
|
use super::{TypeTrace, Subtype};
|
|
|
|
|
2013-05-10 15:57:27 -04:00
|
|
|
use middle::ty::{BuiltinBounds};
|
2015-01-03 22:42:21 -05:00
|
|
|
use middle::ty::{self, Ty};
|
2015-01-24 22:13:24 +02:00
|
|
|
use syntax::ast::{MutMutable, MutImmutable, Unsafety};
|
2014-06-21 03:39:03 -07:00
|
|
|
use util::ppaux::mt_to_string;
|
2014-06-20 06:35:06 -04:00
|
|
|
use util::ppaux::Repr;
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-07-22 07:15:43 -04:00
|
|
|
/// "Least upper bound" (common supertype)
|
2014-04-22 15:56:37 +03:00
|
|
|
pub struct Lub<'f, 'tcx: 'f> {
|
|
|
|
fields: CombineFields<'f, 'tcx>
|
2014-07-22 07:15:43 -04:00
|
|
|
}
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-07-21 15:27:59 +12:00
|
|
|
#[allow(non_snake_case)]
|
2014-04-22 15:56:37 +03:00
|
|
|
pub fn Lub<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Lub<'f, 'tcx> {
|
2014-07-22 07:15:43 -04:00
|
|
|
Lub { fields: cf }
|
2012-11-29 18:37:33 -08:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
impl<'f, 'tcx> Combine<'tcx> for Lub<'f, 'tcx> {
|
|
|
|
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx> { self.fields.infcx }
|
2014-05-25 03:17:19 -07:00
|
|
|
fn tag(&self) -> String { "lub".to_string() }
|
2014-07-22 07:15:43 -04:00
|
|
|
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
|
2014-09-29 22:11:30 +03:00
|
|
|
fn trace(&self) -> TypeTrace<'tcx> { self.fields.trace.clone() }
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
fn equate<'a>(&'a self) -> Equate<'a, 'tcx> { Equate(self.fields.clone()) }
|
|
|
|
fn sub<'a>(&'a self) -> Sub<'a, 'tcx> { Sub(self.fields.clone()) }
|
|
|
|
fn lub<'a>(&'a self) -> Lub<'a, 'tcx> { Lub(self.fields.clone()) }
|
|
|
|
fn glb<'a>(&'a self) -> Glb<'a, 'tcx> { Glb(self.fields.clone()) }
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
|
2014-11-15 17:09:51 -05:00
|
|
|
let tcx = self.tcx();
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2013-10-21 13:08:31 -07:00
|
|
|
debug!("{}.mts({}, {})",
|
2012-08-13 15:06:13 -07:00
|
|
|
self.tag(),
|
2014-06-21 03:39:03 -07:00
|
|
|
mt_to_string(tcx, a),
|
|
|
|
mt_to_string(tcx, b));
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2013-08-02 21:41:06 -07:00
|
|
|
if a.mutbl != b.mutbl {
|
|
|
|
return Err(ty::terr_mutability)
|
|
|
|
}
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2013-08-02 21:41:06 -07:00
|
|
|
let m = a.mutbl;
|
2012-08-13 15:06:13 -07:00
|
|
|
match m {
|
2014-07-22 07:46:36 -04:00
|
|
|
MutImmutable => {
|
|
|
|
let t = try!(self.tys(a.ty, b.ty));
|
|
|
|
Ok(ty::mt {ty: t, mutbl: m})
|
|
|
|
}
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-07-22 07:46:36 -04:00
|
|
|
MutMutable => {
|
|
|
|
let t = try!(self.equate().tys(a.ty, b.ty));
|
|
|
|
Ok(ty::mt {ty: t, mutbl: m})
|
|
|
|
}
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
|
2014-04-22 02:21:52 +03:00
|
|
|
self.glb().tys(a, b)
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
|
|
|
|
2014-12-09 10:36:46 -05:00
|
|
|
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
|
2012-08-13 15:06:13 -07:00
|
|
|
match (a, b) {
|
2014-12-09 10:36:46 -05:00
|
|
|
(Unsafety::Unsafe, _) | (_, Unsafety::Unsafe) => Ok(Unsafety::Unsafe),
|
|
|
|
(Unsafety::Normal, Unsafety::Normal) => Ok(Unsafety::Normal),
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 21:46:52 -04:00
|
|
|
fn builtin_bounds(&self,
|
|
|
|
a: ty::BuiltinBounds,
|
|
|
|
b: ty::BuiltinBounds)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> cres<'tcx, ty::BuiltinBounds> {
|
2013-05-10 15:57:27 -04:00
|
|
|
// More bounds is a subtype of fewer bounds, so
|
|
|
|
// the LUB (mutual supertype) is the intersection.
|
|
|
|
Ok(a.intersection(b))
|
|
|
|
}
|
|
|
|
|
2013-02-22 01:41:37 -05:00
|
|
|
fn contraregions(&self, a: ty::Region, b: ty::Region)
|
2014-09-29 22:11:30 +03:00
|
|
|
-> cres<'tcx, ty::Region> {
|
2014-04-22 02:21:52 +03:00
|
|
|
self.glb().regions(a, b)
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
|
2014-06-20 06:35:06 -04:00
|
|
|
debug!("{}.regions({}, {})",
|
2012-08-13 15:06:13 -07:00
|
|
|
self.tag(),
|
2014-11-15 17:09:51 -05:00
|
|
|
a.repr(self.tcx()),
|
|
|
|
b.repr(self.tcx()));
|
2012-08-13 15:06:13 -07:00
|
|
|
|
2014-11-15 17:09:51 -05:00
|
|
|
Ok(self.infcx().region_vars.lub_regions(Subtype(self.trace()), a, b))
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:11:30 +03:00
|
|
|
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
|
2013-02-22 01:41:37 -05:00
|
|
|
super_lattice_tys(self, a, b)
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|
2014-11-15 17:09:51 -05:00
|
|
|
|
2014-12-12 11:28:35 -05:00
|
|
|
fn binders<T>(&self, a: &ty::Binder<T>, b: &ty::Binder<T>) -> cres<'tcx, ty::Binder<T>>
|
|
|
|
where T : Combineable<'tcx>
|
|
|
|
{
|
2014-11-15 17:09:51 -05:00
|
|
|
self.higher_ranked_lub(a, b)
|
|
|
|
}
|
2012-08-13 15:06:13 -07:00
|
|
|
}
|