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::equate::Equate;
|
|
|
|
use super::higher_ranked::HigherRankedRelations;
|
|
|
|
use super::lub::Lub;
|
|
|
|
use super::sub::Sub;
|
|
|
|
use super::{cres, InferCtxt};
|
|
|
|
use super::{TypeTrace, 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};
|
2014-01-09 07:05:33 -06:00
|
|
|
use syntax::ast::{Many, Once, MutImmutable, MutMutable};
|
2014-12-09 09:36:46 -06:00
|
|
|
use syntax::ast::{Onceness, 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> {
|
|
|
|
fn infcx<'a>(&'a self) -> &'a InferCtxt<'a, 'tcx> { self.fields.infcx }
|
2014-05-25 05:17:19 -05:00
|
|
|
fn tag(&self) -> String { "glb".to_string() }
|
2014-07-22 06:15:43 -05:00
|
|
|
fn a_is_expected(&self) -> bool { self.fields.a_is_expected }
|
2014-09-29 14:11:30 -05:00
|
|
|
fn trace(&self) -> TypeTrace<'tcx> { self.fields.trace.clone() }
|
2012-08-13 17:06:13 -05:00
|
|
|
|
2014-04-22 07:56:37 -05: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 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-09-29 14:11:30 -05:00
|
|
|
fn contratys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> cres<'tcx, Ty<'tcx>> {
|
2014-04-21 18:21:52 -05:00
|
|
|
self.lub().tys(a, b)
|
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-09-29 14:11:30 -05:00
|
|
|
fn oncenesses(&self, a: Onceness, b: Onceness) -> cres<'tcx, Onceness> {
|
2012-11-02 15:33:51 -05:00
|
|
|
match (a, b) {
|
|
|
|
(Many, _) | (_, Many) => Ok(Many),
|
|
|
|
(Once, Once) => Ok(Once)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-22 00:41:37 -06:00
|
|
|
fn contraregions(&self, a: ty::Region, b: ty::Region)
|
2014-09-29 14:11:30 -05:00
|
|
|
-> cres<'tcx, ty::Region> {
|
2014-04-21 18:21:52 -05:00
|
|
|
self.lub().regions(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
|
|
|
}
|