2015-03-22 14:11:56 -05:00
|
|
|
// Copyright 2012-2013 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.
|
|
|
|
|
|
|
|
//! Generalized type relating mechanism. A type relation R relates a
|
|
|
|
//! pair of values (A, B). A and B are usually types or regions but
|
|
|
|
//! can be other things. Examples of type relations are subtyping,
|
|
|
|
//! type equality, etc.
|
|
|
|
|
|
|
|
use middle::subst::{ErasedRegions, NonerasedRegions, ParamSpace, Substs};
|
2015-07-10 17:40:04 -05:00
|
|
|
use middle::ty::{self, Ty, TypeError};
|
2015-03-22 14:11:56 -05:00
|
|
|
use middle::ty_fold::TypeFoldable;
|
|
|
|
use std::rc::Rc;
|
|
|
|
use syntax::abi;
|
|
|
|
use syntax::ast;
|
|
|
|
|
2015-07-08 14:27:32 -05:00
|
|
|
pub type RelateResult<'tcx, T> = Result<T, ty::TypeError<'tcx>>;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
2015-06-17 08:11:34 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Cause {
|
2015-07-14 18:36:15 -05:00
|
|
|
ExistentialRegionBound, // relating an existential region bound
|
2015-06-17 08:11:34 -05:00
|
|
|
}
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
pub trait TypeRelation<'a,'tcx> : Sized {
|
|
|
|
fn tcx(&self) -> &'a ty::ctxt<'tcx>;
|
|
|
|
|
|
|
|
/// Returns a static string we can use for printouts.
|
|
|
|
fn tag(&self) -> &'static str;
|
|
|
|
|
|
|
|
/// Returns true if the value `a` is the "expected" type in the
|
|
|
|
/// relation. Just affects error messages.
|
|
|
|
fn a_is_expected(&self) -> bool;
|
|
|
|
|
2015-06-17 08:11:34 -05:00
|
|
|
fn with_cause<F,R>(&mut self, _cause: Cause, f: F) -> R
|
|
|
|
where F: FnOnce(&mut Self) -> R
|
|
|
|
{
|
|
|
|
f(self)
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
/// Generic relation routine suitable for most anything.
|
|
|
|
fn relate<T:Relate<'a,'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
|
|
|
|
Relate::relate(self, a, b)
|
|
|
|
}
|
|
|
|
|
2015-07-16 04:32:45 -05:00
|
|
|
/// Relete elements of two slices pairwise.
|
|
|
|
fn relate_zip<T:Relate<'a,'tcx>>(&mut self, a: &[T], b: &[T]) -> RelateResult<'tcx, Vec<T>> {
|
|
|
|
assert_eq!(a.len(), b.len());
|
|
|
|
a.iter().zip(b).map(|(a, b)| self.relate(a, b)).collect()
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
/// Switch variance for the purpose of relating `a` and `b`.
|
|
|
|
fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
|
|
|
|
variance: ty::Variance,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
|
|
|
-> RelateResult<'tcx, T>;
|
|
|
|
|
|
|
|
// Overrideable relations. You shouldn't typically call these
|
|
|
|
// directly, instead call `relate()`, which in turn calls
|
|
|
|
// these. This is both more uniform but also allows us to add
|
|
|
|
// additional hooks for other types in the future if needed
|
|
|
|
// without making older code, which called `relate`, obsolete.
|
|
|
|
|
|
|
|
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Ty<'tcx>>;
|
|
|
|
|
|
|
|
fn regions(&mut self, a: ty::Region, b: ty::Region)
|
|
|
|
-> RelateResult<'tcx, ty::Region>;
|
|
|
|
|
|
|
|
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
|
|
|
|
-> RelateResult<'tcx, ty::Binder<T>>
|
|
|
|
where T: Relate<'a,'tcx>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Relate<'a,'tcx>: TypeFoldable<'tcx> {
|
|
|
|
fn relate<R:TypeRelation<'a,'tcx>>(relation: &mut R,
|
|
|
|
a: &Self,
|
|
|
|
b: &Self)
|
|
|
|
-> RelateResult<'tcx, Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Relate impls
|
|
|
|
|
2015-07-10 20:27:06 -05:00
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::TypeAndMut<'tcx> {
|
2015-03-22 14:11:56 -05:00
|
|
|
fn relate<R>(relation: &mut R,
|
2015-07-10 20:27:06 -05:00
|
|
|
a: &ty::TypeAndMut<'tcx>,
|
|
|
|
b: &ty::TypeAndMut<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
|
2015-03-22 14:11:56 -05:00
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("{}.mts({:?}, {:?})",
|
2015-03-22 14:11:56 -05:00
|
|
|
relation.tag(),
|
2015-06-18 12:25:05 -05:00
|
|
|
a,
|
|
|
|
b);
|
2015-03-22 14:11:56 -05:00
|
|
|
if a.mutbl != b.mutbl {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::Mutability)
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
|
|
|
let mutbl = a.mutbl;
|
|
|
|
let variance = match mutbl {
|
|
|
|
ast::MutImmutable => ty::Covariant,
|
|
|
|
ast::MutMutable => ty::Invariant,
|
|
|
|
};
|
|
|
|
let ty = try!(relation.relate_with_variance(variance, &a.ty, &b.ty));
|
2015-07-10 20:27:06 -05:00
|
|
|
Ok(ty::TypeAndMut {ty: ty, mutbl: mutbl})
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// substitutions are not themselves relatable without more context,
|
|
|
|
// but they is an important subroutine for things that ARE relatable,
|
|
|
|
// like traits etc.
|
|
|
|
fn relate_item_substs<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
item_def_id: ast::DefId,
|
|
|
|
a_subst: &Substs<'tcx>,
|
|
|
|
b_subst: &Substs<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Substs<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("substs: item_def_id={:?} a_subst={:?} b_subst={:?}",
|
|
|
|
item_def_id,
|
|
|
|
a_subst,
|
|
|
|
b_subst);
|
2015-03-22 14:11:56 -05:00
|
|
|
|
|
|
|
let variances;
|
|
|
|
let opt_variances = if relation.tcx().variance_computed.get() {
|
2015-06-25 15:42:17 -05:00
|
|
|
variances = relation.tcx().item_variances(item_def_id);
|
2015-03-22 14:11:56 -05:00
|
|
|
Some(&*variances)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
relate_substs(relation, opt_variances, a_subst, b_subst)
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:24:44 -05:00
|
|
|
fn relate_substs<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
variances: Option<&ty::ItemVariances>,
|
|
|
|
a_subst: &Substs<'tcx>,
|
|
|
|
b_subst: &Substs<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Substs<'tcx>>
|
2015-03-22 14:11:56 -05:00
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let mut substs = Substs::empty();
|
|
|
|
|
|
|
|
for &space in &ParamSpace::all() {
|
|
|
|
let a_tps = a_subst.types.get_slice(space);
|
|
|
|
let b_tps = b_subst.types.get_slice(space);
|
|
|
|
let t_variances = variances.map(|v| v.types.get_slice(space));
|
|
|
|
let tps = try!(relate_type_params(relation, t_variances, a_tps, b_tps));
|
|
|
|
substs.types.replace(space, tps);
|
|
|
|
}
|
|
|
|
|
|
|
|
match (&a_subst.regions, &b_subst.regions) {
|
|
|
|
(&ErasedRegions, _) | (_, &ErasedRegions) => {
|
|
|
|
substs.regions = ErasedRegions;
|
|
|
|
}
|
|
|
|
|
|
|
|
(&NonerasedRegions(ref a), &NonerasedRegions(ref b)) => {
|
|
|
|
for &space in &ParamSpace::all() {
|
|
|
|
let a_regions = a.get_slice(space);
|
|
|
|
let b_regions = b.get_slice(space);
|
|
|
|
let r_variances = variances.map(|v| v.regions.get_slice(space));
|
|
|
|
let regions = try!(relate_region_params(relation,
|
|
|
|
r_variances,
|
|
|
|
a_regions,
|
|
|
|
b_regions));
|
|
|
|
substs.mut_regions().replace(space, regions);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(substs)
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:24:44 -05:00
|
|
|
fn relate_type_params<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
variances: Option<&[ty::Variance]>,
|
|
|
|
a_tys: &[Ty<'tcx>],
|
|
|
|
b_tys: &[Ty<'tcx>])
|
|
|
|
-> RelateResult<'tcx, Vec<Ty<'tcx>>>
|
2015-03-22 14:11:56 -05:00
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a_tys.len() != b_tys.len() {
|
2015-07-10 17:40:04 -05:00
|
|
|
return Err(TypeError::TyParamSize(expected_found(relation,
|
2015-03-22 14:11:56 -05:00
|
|
|
&a_tys.len(),
|
|
|
|
&b_tys.len())));
|
|
|
|
}
|
|
|
|
|
|
|
|
(0 .. a_tys.len())
|
|
|
|
.map(|i| {
|
|
|
|
let a_ty = a_tys[i];
|
|
|
|
let b_ty = b_tys[i];
|
|
|
|
let v = variances.map_or(ty::Invariant, |v| v[i]);
|
|
|
|
relation.relate_with_variance(v, &a_ty, &b_ty)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn relate_region_params<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
variances: Option<&[ty::Variance]>,
|
|
|
|
a_rs: &[ty::Region],
|
|
|
|
b_rs: &[ty::Region])
|
|
|
|
-> RelateResult<'tcx, Vec<ty::Region>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let num_region_params = a_rs.len();
|
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("relate_region_params(a_rs={:?}, \
|
|
|
|
b_rs={:?}, variances={:?})",
|
|
|
|
a_rs,
|
|
|
|
b_rs,
|
|
|
|
variances);
|
2015-03-22 14:11:56 -05:00
|
|
|
|
|
|
|
assert_eq!(num_region_params,
|
|
|
|
variances.map_or(num_region_params,
|
|
|
|
|v| v.len()));
|
|
|
|
|
|
|
|
assert_eq!(num_region_params, b_rs.len());
|
|
|
|
|
|
|
|
(0..a_rs.len())
|
|
|
|
.map(|i| {
|
|
|
|
let a_r = a_rs[i];
|
|
|
|
let b_r = b_rs[i];
|
|
|
|
let variance = variances.map_or(ty::Invariant, |v| v[i]);
|
|
|
|
relation.relate_with_variance(variance, &a_r, &b_r)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::BareFnTy<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::BareFnTy<'tcx>,
|
|
|
|
b: &ty::BareFnTy<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::BareFnTy<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let unsafety = try!(relation.relate(&a.unsafety, &b.unsafety));
|
|
|
|
let abi = try!(relation.relate(&a.abi, &b.abi));
|
|
|
|
let sig = try!(relation.relate(&a.sig, &b.sig));
|
|
|
|
Ok(ty::BareFnTy {unsafety: unsafety,
|
|
|
|
abi: abi,
|
|
|
|
sig: sig})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::FnSig<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::FnSig<'tcx>,
|
|
|
|
b: &ty::FnSig<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::FnSig<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a.variadic != b.variadic {
|
2015-07-10 17:40:04 -05:00
|
|
|
return Err(TypeError::VariadicMismatch(
|
2015-03-22 14:11:56 -05:00
|
|
|
expected_found(relation, &a.variadic, &b.variadic)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let inputs = try!(relate_arg_vecs(relation,
|
|
|
|
&a.inputs,
|
|
|
|
&b.inputs));
|
|
|
|
|
|
|
|
let output = try!(match (a.output, b.output) {
|
|
|
|
(ty::FnConverging(a_ty), ty::FnConverging(b_ty)) =>
|
|
|
|
Ok(ty::FnConverging(try!(relation.relate(&a_ty, &b_ty)))),
|
|
|
|
(ty::FnDiverging, ty::FnDiverging) =>
|
|
|
|
Ok(ty::FnDiverging),
|
|
|
|
(a, b) =>
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::ConvergenceMismatch(
|
2015-03-22 14:11:56 -05:00
|
|
|
expected_found(relation, &(a != ty::FnDiverging), &(b != ty::FnDiverging)))),
|
|
|
|
});
|
|
|
|
|
|
|
|
return Ok(ty::FnSig {inputs: inputs,
|
|
|
|
output: output,
|
|
|
|
variadic: a.variadic});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 12:24:44 -05:00
|
|
|
fn relate_arg_vecs<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
a_args: &[Ty<'tcx>],
|
|
|
|
b_args: &[Ty<'tcx>])
|
|
|
|
-> RelateResult<'tcx, Vec<Ty<'tcx>>>
|
2015-03-22 14:11:56 -05:00
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a_args.len() != b_args.len() {
|
2015-07-10 17:40:04 -05:00
|
|
|
return Err(TypeError::ArgCount);
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-10 11:22:20 -05:00
|
|
|
a_args.iter().zip(b_args)
|
2015-03-22 14:11:56 -05:00
|
|
|
.map(|(a, b)| relation.relate_with_variance(ty::Contravariant, a, b))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ast::Unsafety {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ast::Unsafety,
|
|
|
|
b: &ast::Unsafety)
|
|
|
|
-> RelateResult<'tcx, ast::Unsafety>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a != b {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::UnsafetyMismatch(expected_found(relation, a, b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
|
|
|
Ok(*a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for abi::Abi {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &abi::Abi,
|
|
|
|
b: &abi::Abi)
|
|
|
|
-> RelateResult<'tcx, abi::Abi>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a == b {
|
|
|
|
Ok(*a)
|
|
|
|
} else {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::AbiMismatch(expected_found(relation, a, b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::ProjectionTy<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::ProjectionTy<'tcx>,
|
|
|
|
b: &ty::ProjectionTy<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
if a.item_name != b.item_name {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::ProjectionNameMismatched(
|
2015-03-22 14:11:56 -05:00
|
|
|
expected_found(relation, &a.item_name, &b.item_name)))
|
|
|
|
} else {
|
2015-04-21 10:59:58 -05:00
|
|
|
let trait_ref = try!(relation.relate(&a.trait_ref, &b.trait_ref));
|
|
|
|
Ok(ty::ProjectionTy { trait_ref: trait_ref, item_name: a.item_name })
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::ProjectionPredicate<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::ProjectionPredicate<'tcx>,
|
|
|
|
b: &ty::ProjectionPredicate<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ProjectionPredicate<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let projection_ty = try!(relation.relate(&a.projection_ty, &b.projection_ty));
|
|
|
|
let ty = try!(relation.relate(&a.ty, &b.ty));
|
|
|
|
Ok(ty::ProjectionPredicate { projection_ty: projection_ty, ty: ty })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for Vec<ty::PolyProjectionPredicate<'tcx>> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &Vec<ty::PolyProjectionPredicate<'tcx>>,
|
|
|
|
b: &Vec<ty::PolyProjectionPredicate<'tcx>>)
|
|
|
|
-> RelateResult<'tcx, Vec<ty::PolyProjectionPredicate<'tcx>>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
// To be compatible, `a` and `b` must be for precisely the
|
|
|
|
// same set of traits and item names. We always require that
|
|
|
|
// projection bounds lists are sorted by trait-def-id and item-name,
|
|
|
|
// so we can just iterate through the lists pairwise, so long as they are the
|
|
|
|
// same length.
|
|
|
|
if a.len() != b.len() {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::ProjectionBoundsLength(expected_found(relation, &a.len(), &b.len())))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
2015-06-10 11:22:20 -05:00
|
|
|
a.iter().zip(b)
|
2015-03-22 14:11:56 -05:00
|
|
|
.map(|(a, b)| relation.relate(a, b))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::ExistentialBounds<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::ExistentialBounds<'tcx>,
|
|
|
|
b: &ty::ExistentialBounds<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ExistentialBounds<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
2015-06-17 08:11:34 -05:00
|
|
|
let r =
|
|
|
|
try!(relation.with_cause(
|
2015-07-14 18:36:15 -05:00
|
|
|
Cause::ExistentialRegionBound,
|
2015-06-17 08:11:34 -05:00
|
|
|
|relation| relation.relate_with_variance(ty::Contravariant,
|
|
|
|
&a.region_bound,
|
|
|
|
&b.region_bound)));
|
2015-03-22 14:11:56 -05:00
|
|
|
let nb = try!(relation.relate(&a.builtin_bounds, &b.builtin_bounds));
|
|
|
|
let pb = try!(relation.relate(&a.projection_bounds, &b.projection_bounds));
|
|
|
|
Ok(ty::ExistentialBounds { region_bound: r,
|
|
|
|
builtin_bounds: nb,
|
2015-07-14 18:36:15 -05:00
|
|
|
projection_bounds: pb })
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::BuiltinBounds {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::BuiltinBounds,
|
|
|
|
b: &ty::BuiltinBounds)
|
|
|
|
-> RelateResult<'tcx, ty::BuiltinBounds>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
// Two sets of builtin bounds are only relatable if they are
|
|
|
|
// precisely the same (but see the coercion code).
|
|
|
|
if a != b {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::BuiltinBoundsMismatch(expected_found(relation, a, b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
|
|
|
Ok(*a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::TraitRef<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::TraitRef<'tcx>,
|
|
|
|
b: &ty::TraitRef<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::TraitRef<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
// Different traits cannot be related
|
|
|
|
if a.def_id != b.def_id {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
|
|
|
let substs = try!(relate_item_substs(relation, a.def_id, a.substs, b.substs));
|
|
|
|
Ok(ty::TraitRef { def_id: a.def_id, substs: relation.tcx().mk_substs(substs) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for Ty<'tcx> {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &Ty<'tcx>,
|
|
|
|
b: &Ty<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Ty<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
relation.tys(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The main "type relation" routine. Note that this does not handle
|
|
|
|
/// inference artifacts, so you should filter those out before calling
|
|
|
|
/// it.
|
|
|
|
pub fn super_relate_tys<'a,'tcx:'a,R>(relation: &mut R,
|
|
|
|
a: Ty<'tcx>,
|
|
|
|
b: Ty<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Ty<'tcx>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let tcx = relation.tcx();
|
|
|
|
let a_sty = &a.sty;
|
|
|
|
let b_sty = &b.sty;
|
|
|
|
debug!("super_tys: a_sty={:?} b_sty={:?}", a_sty, b_sty);
|
|
|
|
match (a_sty, b_sty) {
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyInfer(_), _) |
|
|
|
|
(_, &ty::TyInfer(_)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
// The caller should handle these cases!
|
|
|
|
tcx.sess.bug("var types encountered in super_relate_tys")
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyError, _) | (_, &ty::TyError) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
Ok(tcx.types.err)
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyChar, _) |
|
|
|
|
(&ty::TyBool, _) |
|
|
|
|
(&ty::TyInt(_), _) |
|
|
|
|
(&ty::TyUint(_), _) |
|
|
|
|
(&ty::TyFloat(_), _) |
|
|
|
|
(&ty::TyStr, _)
|
2015-03-22 14:11:56 -05:00
|
|
|
if a == b =>
|
|
|
|
{
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyParam(ref a_p), &ty::TyParam(ref b_p))
|
2015-03-22 14:11:56 -05:00
|
|
|
if a_p.idx == b_p.idx && a_p.space == b_p.space =>
|
|
|
|
{
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyEnum(a_id, a_substs), &ty::TyEnum(b_id, b_substs))
|
2015-03-22 14:11:56 -05:00
|
|
|
if a_id == b_id =>
|
|
|
|
{
|
|
|
|
let substs = try!(relate_item_substs(relation, a_id, a_substs, b_substs));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_enum(a_id, tcx.mk_substs(substs)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyTrait(ref a_), &ty::TyTrait(ref b_)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let principal = try!(relation.relate(&a_.principal, &b_.principal));
|
|
|
|
let bounds = try!(relation.relate(&a_.bounds, &b_.bounds));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_trait(principal, bounds))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyStruct(a_id, a_substs), &ty::TyStruct(b_id, b_substs))
|
2015-03-22 14:11:56 -05:00
|
|
|
if a_id == b_id =>
|
|
|
|
{
|
|
|
|
let substs = try!(relate_item_substs(relation, a_id, a_substs, b_substs));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_struct(a_id, tcx.mk_substs(substs)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-07-16 04:32:45 -05:00
|
|
|
(&ty::TyClosure(a_id, a_substs, ref a_tys),
|
|
|
|
&ty::TyClosure(b_id, b_substs, ref b_tys))
|
2015-03-22 14:11:56 -05:00
|
|
|
if a_id == b_id =>
|
|
|
|
{
|
2015-06-11 18:21:46 -05:00
|
|
|
// All TyClosure types with the same id represent
|
2015-03-22 14:11:56 -05:00
|
|
|
// the (anonymous) type of the same closure expression. So
|
|
|
|
// all of their regions should be equated.
|
|
|
|
let substs = try!(relate_substs(relation, None, a_substs, b_substs));
|
2015-07-16 04:32:45 -05:00
|
|
|
let tys = try!(relation.relate_zip(a_tys, b_tys));
|
|
|
|
Ok(tcx.mk_closure(a_id, tcx.mk_substs(substs), tys))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyBox(a_inner), &ty::TyBox(b_inner)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let typ = try!(relation.relate(&a_inner, &b_inner));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_box(typ))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyRawPtr(ref a_mt), &ty::TyRawPtr(ref b_mt)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let mt = try!(relation.relate(a_mt, b_mt));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_ptr(mt))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyRef(a_r, ref a_mt), &ty::TyRef(b_r, ref b_mt)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let r = try!(relation.relate_with_variance(ty::Contravariant, a_r, b_r));
|
|
|
|
let mt = try!(relation.relate(a_mt, b_mt));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_ref(tcx.mk_region(r), mt))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-12 18:50:13 -05:00
|
|
|
(&ty::TyArray(a_t, sz_a), &ty::TyArray(b_t, sz_b)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let t = try!(relation.relate(&a_t, &b_t));
|
|
|
|
if sz_a == sz_b {
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_array(t, sz_a))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::FixedArraySize(expected_found(relation, &sz_a, &sz_b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-12 18:50:13 -05:00
|
|
|
(&ty::TySlice(a_t), &ty::TySlice(b_t)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let t = try!(relation.relate(&a_t, &b_t));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_slice(t))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyTuple(ref as_), &ty::TyTuple(ref bs)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
if as_.len() == bs.len() {
|
2015-06-10 11:22:20 -05:00
|
|
|
let ts = try!(as_.iter().zip(bs)
|
2015-03-22 14:11:56 -05:00
|
|
|
.map(|(a, b)| relation.relate(a, b))
|
|
|
|
.collect::<Result<_, _>>());
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_tup(ts))
|
2015-03-24 18:54:09 -05:00
|
|
|
} else if !(as_.is_empty() || bs.is_empty()) {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::TupleSize(
|
2015-03-22 14:11:56 -05:00
|
|
|
expected_found(relation, &as_.len(), &bs.len())))
|
|
|
|
} else {
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::Sorts(expected_found(relation, &a, &b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyBareFn(a_opt_def_id, a_fty), &ty::TyBareFn(b_opt_def_id, b_fty))
|
2015-03-22 14:11:56 -05:00
|
|
|
if a_opt_def_id == b_opt_def_id =>
|
|
|
|
{
|
|
|
|
let fty = try!(relation.relate(a_fty, b_fty));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_fn(a_opt_def_id, tcx.mk_bare_fn(fty)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyProjection(ref a_data), &ty::TyProjection(ref b_data)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let projection_ty = try!(relation.relate(a_data, b_data));
|
2015-06-24 20:09:46 -05:00
|
|
|
Ok(tcx.mk_projection(projection_ty.trait_ref, projection_ty.item_name))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_ =>
|
|
|
|
{
|
2015-07-10 17:40:04 -05:00
|
|
|
Err(TypeError::Sorts(expected_found(relation, &a, &b)))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::Region {
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::Region,
|
|
|
|
b: &ty::Region)
|
|
|
|
-> RelateResult<'tcx, ty::Region>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
relation.regions(*a, *b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a,T> Relate<'a,'tcx> for ty::Binder<T>
|
|
|
|
where T: Relate<'a,'tcx>
|
|
|
|
{
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &ty::Binder<T>,
|
|
|
|
b: &ty::Binder<T>)
|
|
|
|
-> RelateResult<'tcx, ty::Binder<T>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
relation.binders(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a,T> Relate<'a,'tcx> for Rc<T>
|
|
|
|
where T: Relate<'a,'tcx>
|
|
|
|
{
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &Rc<T>,
|
|
|
|
b: &Rc<T>)
|
|
|
|
-> RelateResult<'tcx, Rc<T>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let a: &T = a;
|
|
|
|
let b: &T = b;
|
|
|
|
Ok(Rc::new(try!(relation.relate(a, b))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx:'a,T> Relate<'a,'tcx> for Box<T>
|
|
|
|
where T: Relate<'a,'tcx>
|
|
|
|
{
|
|
|
|
fn relate<R>(relation: &mut R,
|
|
|
|
a: &Box<T>,
|
|
|
|
b: &Box<T>)
|
|
|
|
-> RelateResult<'tcx, Box<T>>
|
|
|
|
where R: TypeRelation<'a,'tcx>
|
|
|
|
{
|
|
|
|
let a: &T = a;
|
|
|
|
let b: &T = b;
|
|
|
|
Ok(Box::new(try!(relation.relate(a, b))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Error handling
|
|
|
|
|
2015-04-02 12:24:44 -05:00
|
|
|
pub fn expected_found<'a,'tcx:'a,R,T>(relation: &mut R,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
2015-07-08 14:27:32 -05:00
|
|
|
-> ty::ExpectedFound<T>
|
2015-03-22 14:11:56 -05:00
|
|
|
where R: TypeRelation<'a,'tcx>, T: Clone
|
|
|
|
{
|
|
|
|
expected_found_bool(relation.a_is_expected(), a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expected_found_bool<T>(a_is_expected: bool,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
2015-07-08 14:27:32 -05:00
|
|
|
-> ty::ExpectedFound<T>
|
2015-03-22 14:11:56 -05:00
|
|
|
where T: Clone
|
|
|
|
{
|
|
|
|
let a = a.clone();
|
|
|
|
let b = b.clone();
|
|
|
|
if a_is_expected {
|
2015-07-08 14:27:32 -05:00
|
|
|
ty::ExpectedFound {expected: a, found: b}
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
2015-07-08 14:27:32 -05:00
|
|
|
ty::ExpectedFound {expected: b, found: a}
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|