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.
|
|
|
|
|
2016-03-29 04:54:26 -05:00
|
|
|
use hir::def_id::DefId;
|
2016-08-26 17:13:48 -05:00
|
|
|
use ty::subst::{Kind, Substs};
|
2016-03-22 10:30:57 -05:00
|
|
|
use ty::{self, Ty, TyCtxt, TypeFoldable};
|
|
|
|
use ty::error::{ExpectedFound, TypeError};
|
2015-03-22 14:11:56 -05:00
|
|
|
use std::rc::Rc;
|
2016-11-28 21:25:33 -06:00
|
|
|
use std::iter;
|
2015-03-22 14:11:56 -05:00
|
|
|
use syntax::abi;
|
2016-03-29 00:50:44 -05:00
|
|
|
use hir as ast;
|
2016-11-28 21:25:33 -06:00
|
|
|
use rustc_data_structures::accumulate_vec::AccumulateVec;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
2015-09-06 13:51:58 -05:00
|
|
|
pub type RelateResult<'tcx, T> = Result<T, 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
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
pub trait TypeRelation<'a, 'gcx: 'a+'tcx, 'tcx: 'a> : Sized {
|
|
|
|
fn tcx(&self) -> TyCtxt<'a, 'gcx, 'tcx>;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
|
|
|
/// 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.
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate<T: Relate<'tcx>>(&mut self, a: &T, b: &T) -> RelateResult<'tcx, T> {
|
2015-03-22 14:11:56 -05:00
|
|
|
Relate::relate(self, a, b)
|
|
|
|
}
|
|
|
|
|
2017-05-11 09:54:19 -05:00
|
|
|
/// Relate the two substitutions for the given item. The default
|
|
|
|
/// is to look up the variance for the item and proceed
|
|
|
|
/// accordingly.
|
|
|
|
fn relate_item_substs(&mut self,
|
|
|
|
item_def_id: DefId,
|
|
|
|
a_subst: &'tcx Substs<'tcx>,
|
|
|
|
b_subst: &'tcx Substs<'tcx>)
|
|
|
|
-> RelateResult<'tcx, &'tcx Substs<'tcx>>
|
|
|
|
{
|
|
|
|
debug!("relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
|
|
|
|
item_def_id,
|
|
|
|
a_subst,
|
|
|
|
b_subst);
|
|
|
|
|
|
|
|
let opt_variances = self.tcx().variances_of(item_def_id);
|
|
|
|
relate_substs(self, Some(&opt_variances), a_subst, b_subst)
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:11:56 -05:00
|
|
|
/// Switch variance for the purpose of relating `a` and `b`.
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate_with_variance<T: Relate<'tcx>>(&mut self,
|
|
|
|
variance: ty::Variance,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
|
|
|
-> RelateResult<'tcx, T>;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
|
|
|
// 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>>;
|
|
|
|
|
2017-04-20 03:45:53 -05:00
|
|
|
fn regions(&mut self, a: ty::Region<'tcx>, b: ty::Region<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::Region<'tcx>>;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
|
|
|
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
|
|
|
|
-> RelateResult<'tcx, ty::Binder<T>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where T: Relate<'tcx>;
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
pub trait Relate<'tcx>: TypeFoldable<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R, a: &Self, b: &Self)
|
|
|
|
-> RelateResult<'tcx, Self>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a;
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Relate impls
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::TypeAndMut<'tcx>,
|
|
|
|
b: &ty::TypeAndMut<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::TypeAndMut<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
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 {
|
2016-02-09 10:44:47 -06:00
|
|
|
ast::Mutability::MutImmutable => ty::Covariant,
|
|
|
|
ast::Mutability::MutMutable => ty::Invariant,
|
2015-03-22 14:11:56 -05:00
|
|
|
};
|
2016-03-22 22:01:37 -05:00
|
|
|
let ty = 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
|
2016-08-26 17:13:48 -05:00
|
|
|
variances: Option<&Vec<ty::Variance>>,
|
2016-04-29 00:30:54 -05:00
|
|
|
a_subst: &'tcx Substs<'tcx>,
|
|
|
|
b_subst: &'tcx Substs<'tcx>)
|
|
|
|
-> RelateResult<'tcx, &'tcx Substs<'tcx>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2016-08-08 15:39:49 -05:00
|
|
|
let tcx = relation.tcx();
|
2016-08-16 22:32:00 -05:00
|
|
|
|
2017-02-15 23:46:23 -06:00
|
|
|
let params = a_subst.iter().zip(b_subst).enumerate().map(|(i, (a, b))| {
|
2016-08-26 17:13:48 -05:00
|
|
|
let variance = variances.map_or(ty::Invariant, |v| v[i]);
|
|
|
|
if let (Some(a_ty), Some(b_ty)) = (a.as_type(), b.as_type()) {
|
|
|
|
Ok(Kind::from(relation.relate_with_variance(variance, &a_ty, &b_ty)?))
|
|
|
|
} else if let (Some(a_r), Some(b_r)) = (a.as_region(), b.as_region()) {
|
|
|
|
Ok(Kind::from(relation.relate_with_variance(variance, &a_r, &b_r)?))
|
|
|
|
} else {
|
|
|
|
bug!()
|
|
|
|
}
|
|
|
|
});
|
2015-03-22 14:11:56 -05:00
|
|
|
|
2016-10-24 19:23:29 -05:00
|
|
|
Ok(tcx.mk_substs(params)?)
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::FnSig<'tcx>,
|
|
|
|
b: &ty::FnSig<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::FnSig<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2016-11-28 21:25:33 -06:00
|
|
|
if a.variadic != b.variadic {
|
2015-07-10 17:40:04 -05:00
|
|
|
return Err(TypeError::VariadicMismatch(
|
2016-11-28 21:25:33 -06:00
|
|
|
expected_found(relation, &a.variadic, &b.variadic)));
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
2017-02-13 02:51:06 -06:00
|
|
|
let unsafety = relation.relate(&a.unsafety, &b.unsafety)?;
|
|
|
|
let abi = relation.relate(&a.abi, &b.abi)?;
|
2015-03-22 14:11:56 -05:00
|
|
|
|
2016-11-28 20:35:38 -06:00
|
|
|
if a.inputs().len() != b.inputs().len() {
|
|
|
|
return Err(TypeError::ArgCount);
|
|
|
|
}
|
2015-03-22 14:11:56 -05:00
|
|
|
|
2016-11-28 21:25:33 -06:00
|
|
|
let inputs_and_output = a.inputs().iter().cloned()
|
|
|
|
.zip(b.inputs().iter().cloned())
|
|
|
|
.map(|x| (x, false))
|
|
|
|
.chain(iter::once(((a.output(), b.output()), true)))
|
|
|
|
.map(|((a, b), is_output)| {
|
|
|
|
if is_output {
|
|
|
|
relation.relate(&a, &b)
|
|
|
|
} else {
|
|
|
|
relation.relate_with_variance(ty::Contravariant, &a, &b)
|
|
|
|
}
|
|
|
|
}).collect::<Result<AccumulateVec<[_; 8]>, _>>()?;
|
|
|
|
Ok(ty::FnSig {
|
|
|
|
inputs_and_output: relation.tcx().intern_type_list(&inputs_and_output),
|
2017-02-13 02:51:06 -06:00
|
|
|
variadic: a.variadic,
|
2017-07-03 13:19:51 -05:00
|
|
|
unsafety,
|
|
|
|
abi,
|
2016-11-28 21:25:33 -06:00
|
|
|
})
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ast::Unsafety {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ast::Unsafety,
|
|
|
|
b: &ast::Unsafety)
|
|
|
|
-> RelateResult<'tcx, ast::Unsafety>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for abi::Abi {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &abi::Abi,
|
|
|
|
b: &abi::Abi)
|
|
|
|
-> RelateResult<'tcx, abi::Abi>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::ProjectionTy<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::ProjectionTy<'tcx>,
|
|
|
|
b: &ty::ProjectionTy<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ProjectionTy<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2017-07-11 09:33:09 -05:00
|
|
|
if a.item_def_id != b.item_def_id {
|
|
|
|
Err(TypeError::ProjectionMismatched(
|
|
|
|
expected_found(relation, &a.item_def_id, &b.item_def_id)))
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
2017-07-11 09:33:09 -05:00
|
|
|
let substs = relation.relate(&a.substs, &b.substs)?;
|
|
|
|
Ok(ty::ProjectionTy {
|
|
|
|
item_def_id: a.item_def_id,
|
|
|
|
substs: &substs,
|
|
|
|
})
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:52:57 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> {
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
2016-08-04 07:52:57 -05:00
|
|
|
a: &ty::ExistentialProjection<'tcx>,
|
|
|
|
b: &ty::ExistentialProjection<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ExistentialProjection<'tcx>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2017-07-11 09:33:09 -05:00
|
|
|
if a.item_def_id != b.item_def_id {
|
|
|
|
Err(TypeError::ProjectionMismatched(
|
|
|
|
expected_found(relation, &a.item_def_id, &b.item_def_id)))
|
2016-08-04 07:52:57 -05:00
|
|
|
} else {
|
|
|
|
let ty = relation.relate(&a.ty, &b.ty)?;
|
2017-07-11 09:33:09 -05:00
|
|
|
let substs = relation.relate(&a.substs, &b.substs)?;
|
2016-08-04 07:52:57 -05:00
|
|
|
Ok(ty::ExistentialProjection {
|
2017-07-11 09:33:09 -05:00
|
|
|
item_def_id: a.item_def_id,
|
|
|
|
substs: substs,
|
2017-07-03 13:19:51 -05:00
|
|
|
ty,
|
2016-08-04 07:52:57 -05:00
|
|
|
})
|
|
|
|
}
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:52:57 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for Vec<ty::PolyExistentialProjection<'tcx>> {
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
2016-08-04 07:52:57 -05:00
|
|
|
a: &Vec<ty::PolyExistentialProjection<'tcx>>,
|
|
|
|
b: &Vec<ty::PolyExistentialProjection<'tcx>>)
|
|
|
|
-> RelateResult<'tcx, Vec<ty::PolyExistentialProjection<'tcx>>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::TraitRef<'tcx>,
|
|
|
|
b: &ty::TraitRef<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::TraitRef<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
// 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 {
|
2017-06-02 14:05:41 -05:00
|
|
|
let substs = relate_substs(relation, None, a.substs, b.substs)?;
|
2016-04-29 00:30:54 -05:00
|
|
|
Ok(ty::TraitRef { def_id: a.def_id, substs: substs })
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-04 07:52:57 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::ExistentialTraitRef<'tcx>,
|
|
|
|
b: &ty::ExistentialTraitRef<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ExistentialTraitRef<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
|
|
|
{
|
|
|
|
// Different traits cannot be related
|
|
|
|
if a.def_id != b.def_id {
|
|
|
|
Err(TypeError::Traits(expected_found(relation, &a.def_id, &b.def_id)))
|
|
|
|
} else {
|
2017-06-02 14:05:41 -05:00
|
|
|
let substs = relate_substs(relation, None, a.substs, b.substs)?;
|
2016-08-04 07:52:57 -05:00
|
|
|
Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs: substs })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for Ty<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &Ty<'tcx>,
|
|
|
|
b: &Ty<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Ty<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
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.
|
2016-04-28 22:00:23 -05:00
|
|
|
pub fn super_relate_tys<'a, 'gcx, 'tcx, R>(relation: &mut R,
|
|
|
|
a: Ty<'tcx>,
|
|
|
|
b: Ty<'tcx>)
|
|
|
|
-> RelateResult<'tcx, Ty<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
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!
|
2016-03-24 19:14:29 -05:00
|
|
|
bug!("var types encountered in super_relate_tys")
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2015-06-11 18:21:46 -05:00
|
|
|
(&ty::TyError, _) | (_, &ty::TyError) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
Ok(tcx.types.err)
|
|
|
|
}
|
|
|
|
|
2016-08-02 02:56:20 -05:00
|
|
|
(&ty::TyNever, _) |
|
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))
|
2016-08-16 22:32:00 -05:00
|
|
|
if a_p.idx == b_p.idx =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
Ok(a)
|
|
|
|
}
|
|
|
|
|
2016-09-05 17:26:02 -05:00
|
|
|
(&ty::TyAdt(a_def, a_substs), &ty::TyAdt(b_def, b_substs))
|
2015-07-20 14:13:36 -05:00
|
|
|
if a_def == b_def =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2017-05-11 09:54:19 -05:00
|
|
|
let substs = relation.relate_item_substs(a_def.did, a_substs, b_substs)?;
|
2016-09-05 17:26:02 -05:00
|
|
|
Ok(tcx.mk_adt(a_def, substs))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-11-16 10:21:49 -06:00
|
|
|
(&ty::TyDynamic(ref a_obj, ref a_region), &ty::TyDynamic(ref b_obj, ref b_region)) => {
|
|
|
|
let region_bound = relation.with_cause(Cause::ExistentialRegionBound,
|
|
|
|
|relation| {
|
|
|
|
relation.relate_with_variance(
|
|
|
|
ty::Contravariant,
|
|
|
|
a_region,
|
|
|
|
b_region)
|
|
|
|
})?;
|
|
|
|
Ok(tcx.mk_dynamic(relation.relate(a_obj, b_obj)?, region_bound))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-04-29 00:30:54 -05:00
|
|
|
(&ty::TyClosure(a_id, a_substs),
|
|
|
|
&ty::TyClosure(b_id, b_substs))
|
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.
|
2016-04-29 00:30:54 -05:00
|
|
|
let substs = relation.relate(&a_substs, &b_substs)?;
|
2015-07-16 08:46:35 -05:00
|
|
|
Ok(tcx.mk_closure_from_closure_substs(a_id, substs))
|
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
|
|
|
{
|
2016-03-22 22:01:37 -05:00
|
|
|
let mt = 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
|
|
|
{
|
2016-08-25 15:58:52 -05:00
|
|
|
let r = relation.relate_with_variance(ty::Contravariant, &a_r, &b_r)?;
|
2016-03-22 22:01:37 -05:00
|
|
|
let mt = relation.relate(a_mt, b_mt)?;
|
2016-08-25 15:58:52 -05:00
|
|
|
Ok(tcx.mk_ref(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
|
|
|
{
|
2016-03-22 22:01:37 -05:00
|
|
|
let t = relation.relate(&a_t, &b_t)?;
|
2015-03-22 14:11:56 -05:00
|
|
|
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
|
|
|
{
|
2016-03-22 22:01:37 -05:00
|
|
|
let t = 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
|
|
|
}
|
|
|
|
|
2017-01-11 01:58:37 -06:00
|
|
|
(&ty::TyTuple(as_, a_defaulted), &ty::TyTuple(bs, b_defaulted)) =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
if as_.len() == bs.len() {
|
2017-01-11 01:58:37 -06:00
|
|
|
let defaulted = a_defaulted || b_defaulted;
|
|
|
|
Ok(tcx.mk_tup(as_.iter().zip(bs).map(|(a, b)| relation.relate(a, b)), defaulted)?)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-13 09:11:52 -05:00
|
|
|
(&ty::TyFnDef(a_def_id, a_substs), &ty::TyFnDef(b_def_id, b_substs))
|
2015-06-13 15:15:03 -05:00
|
|
|
if a_def_id == b_def_id =>
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
2017-06-02 14:05:41 -05:00
|
|
|
let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?;
|
2017-05-13 09:11:52 -05:00
|
|
|
Ok(tcx.mk_fn_def(a_def_id, substs))
|
2015-06-13 15:15:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
(&ty::TyFnPtr(a_fty), &ty::TyFnPtr(b_fty)) =>
|
|
|
|
{
|
2016-04-29 00:30:54 -05:00
|
|
|
let fty = relation.relate(&a_fty, &b_fty)?;
|
2015-06-13 15:15:03 -05:00
|
|
|
Ok(tcx.mk_fn_ptr(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
|
|
|
{
|
2016-03-22 22:01:37 -05:00
|
|
|
let projection_ty = relation.relate(a_data, b_data)?;
|
2017-07-11 09:33:09 -05:00
|
|
|
Ok(tcx.mk_projection(projection_ty.item_def_id, projection_ty.substs))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
|
2016-07-22 10:56:22 -05:00
|
|
|
(&ty::TyAnon(a_def_id, a_substs), &ty::TyAnon(b_def_id, b_substs))
|
|
|
|
if a_def_id == b_def_id =>
|
|
|
|
{
|
|
|
|
let substs = relate_substs(relation, None, a_substs, b_substs)?;
|
|
|
|
Ok(tcx.mk_anon(a_def_id, substs))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 10:21:49 -06:00
|
|
|
impl<'tcx> Relate<'tcx> for &'tcx ty::Slice<ty::ExistentialPredicate<'tcx>> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &Self,
|
|
|
|
b: &Self)
|
|
|
|
-> RelateResult<'tcx, Self>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a {
|
|
|
|
|
|
|
|
if a.len() != b.len() {
|
|
|
|
return Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)));
|
|
|
|
}
|
|
|
|
|
|
|
|
let tcx = relation.tcx();
|
|
|
|
let v = a.iter().zip(b.iter()).map(|(ep_a, ep_b)| {
|
|
|
|
use ty::ExistentialPredicate::*;
|
|
|
|
match (*ep_a, *ep_b) {
|
|
|
|
(Trait(ref a), Trait(ref b)) => Ok(Trait(relation.relate(a, b)?)),
|
|
|
|
(Projection(ref a), Projection(ref b)) => Ok(Projection(relation.relate(a, b)?)),
|
|
|
|
(AutoTrait(ref a), AutoTrait(ref b)) if a == b => Ok(AutoTrait(*a)),
|
|
|
|
_ => Err(TypeError::ExistentialMismatch(expected_found(relation, a, b)))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Ok(tcx.mk_existential_predicates(v)?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::ClosureSubsts<'tcx>,
|
|
|
|
b: &ty::ClosureSubsts<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::ClosureSubsts<'tcx>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-07-16 08:46:35 -05:00
|
|
|
{
|
2016-11-03 15:19:33 -05:00
|
|
|
let substs = relate_substs(relation, None, a.substs, b.substs)?;
|
|
|
|
Ok(ty::ClosureSubsts { substs: substs })
|
2015-07-16 08:46:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 00:30:54 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for &'tcx Substs<'tcx> {
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
2016-04-29 00:30:54 -05:00
|
|
|
a: &&'tcx Substs<'tcx>,
|
|
|
|
b: &&'tcx Substs<'tcx>)
|
|
|
|
-> RelateResult<'tcx, &'tcx Substs<'tcx>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2016-03-28 23:56:19 -05:00
|
|
|
{
|
|
|
|
relate_substs(relation, None, a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 03:45:53 -05:00
|
|
|
impl<'tcx> Relate<'tcx> for ty::Region<'tcx> {
|
2016-04-28 22:00:23 -05:00
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
2017-04-20 03:45:53 -05:00
|
|
|
a: &ty::Region<'tcx>,
|
|
|
|
b: &ty::Region<'tcx>)
|
|
|
|
-> RelateResult<'tcx, ty::Region<'tcx>>
|
2016-04-28 22:00:23 -05:00
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
relation.regions(*a, *b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for ty::Binder<T> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &ty::Binder<T>,
|
|
|
|
b: &ty::Binder<T>)
|
|
|
|
-> RelateResult<'tcx, ty::Binder<T>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
relation.binders(a, b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Rc<T> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &Rc<T>,
|
|
|
|
b: &Rc<T>)
|
|
|
|
-> RelateResult<'tcx, Rc<T>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let a: &T = a;
|
|
|
|
let b: &T = b;
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(Rc::new(relation.relate(a, b)?))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
impl<'tcx, T: Relate<'tcx>> Relate<'tcx> for Box<T> {
|
|
|
|
fn relate<'a, 'gcx, R>(relation: &mut R,
|
|
|
|
a: &Box<T>,
|
|
|
|
b: &Box<T>)
|
|
|
|
-> RelateResult<'tcx, Box<T>>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
let a: &T = a;
|
|
|
|
let b: &T = b;
|
2016-03-22 22:01:37 -05:00
|
|
|
Ok(Box::new(relation.relate(a, b)?))
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Error handling
|
|
|
|
|
2016-04-28 22:00:23 -05:00
|
|
|
pub fn expected_found<'a, 'gcx, 'tcx, R, T>(relation: &mut R,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
|
|
|
-> ExpectedFound<T>
|
|
|
|
where R: TypeRelation<'a, 'gcx, 'tcx>, T: Clone, 'gcx: 'a+'tcx, 'tcx: 'a
|
2015-03-22 14:11:56 -05:00
|
|
|
{
|
|
|
|
expected_found_bool(relation.a_is_expected(), a, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expected_found_bool<T>(a_is_expected: bool,
|
|
|
|
a: &T,
|
|
|
|
b: &T)
|
2015-09-06 13:51:58 -05:00
|
|
|
-> 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-09-06 13:51:58 -05:00
|
|
|
ExpectedFound {expected: a, found: b}
|
2015-03-22 14:11:56 -05:00
|
|
|
} else {
|
2015-09-06 13:51:58 -05:00
|
|
|
ExpectedFound {expected: b, found: a}
|
2015-03-22 14:11:56 -05:00
|
|
|
}
|
|
|
|
}
|