2012-12-03 18:48:01 -06: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 15:59:02 -06:00
|
|
|
use super::combine::*;
|
|
|
|
use super::lattice::*;
|
|
|
|
use super::higher_ranked::HigherRankedRelations;
|
2015-02-12 04:16:02 -06:00
|
|
|
use super::{cres};
|
|
|
|
use super::Subtype;
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-05-10 14:57:27 -05:00
|
|
|
use middle::ty::{BuiltinBounds};
|
2015-01-03 21:42:21 -06:00
|
|
|
use middle::ty::{self, Ty};
|
2015-01-24 14:13:24 -06:00
|
|
|
use syntax::ast::{MutImmutable, MutMutable, Unsafety};
|
2014-06-21 05:39:03 -05:00
|
|
|
use util::ppaux::mt_to_string;
|
2014-06-20 05:35:06 -05:00
|
|
|
use util::ppaux::Repr;
|
2012-12-13 15:05:22 -06:00
|
|
|
|
2014-07-22 06:15:43 -05:00
|
|
|
/// "Greatest lower bound" (common subtype)
|
2014-04-22 07:56:37 -05:00
|
|
|
pub struct Glb<'f, 'tcx: 'f> {
|
|
|
|
fields: CombineFields<'f, 'tcx>
|
2014-07-22 06:15:43 -05:00
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-07-20 22:27:59 -05:00
|
|
|
#[allow(non_snake_case)]
|
2014-04-22 07:56:37 -05:00
|
|
|
pub fn Glb<'f, 'tcx>(cf: CombineFields<'f, 'tcx>) -> Glb<'f, 'tcx> {
|
2014-07-22 06:15:43 -05:00
|
|
|
Glb { fields: cf }
|
2013-11-01 20:06:31 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
impl<'f, 'tcx> Combine<'tcx> for Glb<'f, 'tcx> {
|
2015-02-12 04:16:02 -06:00
|
|
|
fn tag(&self) -> String { "Glb".to_string() }
|
|
|
|
fn fields<'a>(&'a self) -> &'a CombineFields<'a, 'tcx> { &self.fields }
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2015-02-12 04:16:02 -06:00
|
|
|
fn tys_with_variance(&self, v: ty::Variance, a: Ty<'tcx>, b: Ty<'tcx>)
|
|
|
|
-> cres<'tcx, Ty<'tcx>>
|
|
|
|
{
|
|
|
|
match v {
|
|
|
|
ty::Invariant => self.equate().tys(a, b),
|
|
|
|
ty::Covariant => self.tys(a, b),
|
|
|
|
ty::Bivariant => self.bivariate().tys(a, b),
|
|
|
|
ty::Contravariant => self.lub().tys(a, b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn regions_with_variance(&self, v: ty::Variance, a: ty::Region, b: ty::Region)
|
|
|
|
-> cres<'tcx, ty::Region>
|
|
|
|
{
|
|
|
|
match v {
|
|
|
|
ty::Invariant => self.equate().regions(a, b),
|
|
|
|
ty::Covariant => self.regions(a, b),
|
|
|
|
ty::Bivariant => self.bivariate().regions(a, b),
|
|
|
|
ty::Contravariant => self.lub().regions(a, b),
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn mts(&self, a: &ty::mt<'tcx>, b: &ty::mt<'tcx>) -> cres<'tcx, ty::mt<'tcx>> {
|
2014-07-22 06:15:43 -05:00
|
|
|
let tcx = self.fields.infcx.tcx;
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2013-10-21 15:08:31 -05:00
|
|
|
debug!("{}.mts({}, {})",
|
2012-08-13 17:06:13 -05:00
|
|
|
self.tag(),
|
2014-06-21 05:39:03 -05:00
|
|
|
mt_to_string(tcx, a),
|
|
|
|
mt_to_string(tcx, b));
|
2012-08-13 17:06:13 -05:00
|
|
|
|
|
|
|
match (a.mutbl, b.mutbl) {
|
2014-07-22 06:46:36 -05:00
|
|
|
// If one side or both is mut, then the GLB must use
|
|
|
|
// the precise type from the mut side.
|
|
|
|
(MutMutable, MutMutable) => {
|
|
|
|
let t = try!(self.equate().tys(a.ty, b.ty));
|
|
|
|
Ok(ty::mt {ty: t, mutbl: MutMutable})
|
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-07-22 06:46:36 -05:00
|
|
|
// If one side or both is immutable, we can use the GLB of
|
|
|
|
// both sides but mutbl must be `MutImmutable`.
|
|
|
|
(MutImmutable, MutImmutable) => {
|
|
|
|
let t = try!(self.tys(a.ty, b.ty));
|
2013-09-01 20:45:37 -05:00
|
|
|
Ok(ty::mt {ty: t, mutbl: MutImmutable})
|
2014-07-22 06:46:36 -05:00
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-07-22 06:46:36 -05:00
|
|
|
// There is no mutual subtype of these combinations.
|
|
|
|
(MutMutable, MutImmutable) |
|
|
|
|
(MutImmutable, MutMutable) => {
|
|
|
|
Err(ty::terr_mutability)
|
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 09:36:46 -06:00
|
|
|
fn unsafeties(&self, a: Unsafety, b: Unsafety) -> cres<'tcx, Unsafety> {
|
2012-08-13 17:06:13 -05:00
|
|
|
match (a, b) {
|
2014-12-09 09:36:46 -06:00
|
|
|
(Unsafety::Normal, _) | (_, Unsafety::Normal) => Ok(Unsafety::Normal),
|
|
|
|
(Unsafety::Unsafe, Unsafety::Unsafe) => Ok(Unsafety::Unsafe)
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-27 20:46:52 -05:00
|
|
|
fn builtin_bounds(&self,
|
|
|
|
a: ty::BuiltinBounds,
|
|
|
|
b: ty::BuiltinBounds)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cres<'tcx, ty::BuiltinBounds> {
|
2013-05-10 14:57:27 -05:00
|
|
|
// More bounds is a subtype of fewer bounds, so
|
|
|
|
// the GLB (mutual subtype) is the union.
|
|
|
|
Ok(a.union(b))
|
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn regions(&self, a: ty::Region, b: ty::Region) -> cres<'tcx, ty::Region> {
|
2014-10-15 01:25:34 -05:00
|
|
|
debug!("{}.regions({}, {})",
|
2012-08-13 17:06:13 -05:00
|
|
|
self.tag(),
|
2014-07-22 06:15:43 -05:00
|
|
|
a.repr(self.fields.infcx.tcx),
|
|
|
|
b.repr(self.fields.infcx.tcx));
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-07-22 06:15:43 -05:00
|
|
|
Ok(self.fields.infcx.region_vars.glb_regions(Subtype(self.trace()), a, b))
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 14:11:30 -05:00
|
|
|
fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
|
2013-02-22 00:41:37 -06:00
|
|
|
super_lattice_tys(self, a, b)
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|
|
|
|
|
2014-12-12 10:28:35 -06: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 15:47:59 -06:00
|
|
|
self.higher_ranked_glb(a, b)
|
|
|
|
}
|
2012-08-13 17:06:13 -05:00
|
|
|
}
|