2014-09-12 09:53:35 -05:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2015-06-02 18:14:45 -05:00
|
|
|
use middle::subst::Substs;
|
2014-11-25 15:59:02 -06:00
|
|
|
use middle::infer::InferCtxt;
|
2015-06-23 13:50:50 -05:00
|
|
|
use middle::ty::{self, Ty, ToPredicate, ToPolyTraitRef};
|
2014-09-12 09:53:35 -05:00
|
|
|
use std::fmt;
|
|
|
|
use syntax::ast;
|
|
|
|
use syntax::codemap::Span;
|
2014-11-08 05:59:10 -06:00
|
|
|
use util::common::ErrorReported;
|
2015-02-05 10:48:20 -06:00
|
|
|
use util::nodemap::FnvHashSet;
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
use super::{Obligation, ObligationCause, PredicateObligation,
|
2015-02-07 07:24:34 -06:00
|
|
|
VtableImpl, VtableParam, VtableImplData, VtableDefaultImplData};
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-02-05 14:50:34 -06:00
|
|
|
struct PredicateSet<'a,'tcx:'a> {
|
|
|
|
tcx: &'a ty::ctxt<'tcx>,
|
|
|
|
set: FnvHashSet<ty::Predicate<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a,'tcx> PredicateSet<'a,'tcx> {
|
|
|
|
fn new(tcx: &'a ty::ctxt<'tcx>) -> PredicateSet<'a,'tcx> {
|
|
|
|
PredicateSet { tcx: tcx, set: FnvHashSet() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert(&mut self, pred: &ty::Predicate<'tcx>) -> bool {
|
|
|
|
// We have to be careful here because we want
|
|
|
|
//
|
|
|
|
// for<'a> Foo<&'a int>
|
|
|
|
//
|
|
|
|
// and
|
|
|
|
//
|
|
|
|
// for<'b> Foo<&'b int>
|
|
|
|
//
|
|
|
|
// to be considered equivalent. So normalize all late-bound
|
|
|
|
// regions before we throw things into the underlying set.
|
|
|
|
let normalized_pred = match *pred {
|
|
|
|
ty::Predicate::Trait(ref data) =>
|
|
|
|
ty::Predicate::Trait(ty::anonymize_late_bound_regions(self.tcx, data)),
|
|
|
|
|
|
|
|
ty::Predicate::Equate(ref data) =>
|
|
|
|
ty::Predicate::Equate(ty::anonymize_late_bound_regions(self.tcx, data)),
|
|
|
|
|
|
|
|
ty::Predicate::RegionOutlives(ref data) =>
|
|
|
|
ty::Predicate::RegionOutlives(ty::anonymize_late_bound_regions(self.tcx, data)),
|
|
|
|
|
|
|
|
ty::Predicate::TypeOutlives(ref data) =>
|
|
|
|
ty::Predicate::TypeOutlives(ty::anonymize_late_bound_regions(self.tcx, data)),
|
|
|
|
|
|
|
|
ty::Predicate::Projection(ref data) =>
|
|
|
|
ty::Predicate::Projection(ty::anonymize_late_bound_regions(self.tcx, data)),
|
|
|
|
};
|
|
|
|
self.set.insert(normalized_pred)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-12 09:53:35 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-12-11 03:35:51 -06:00
|
|
|
// `Elaboration` iterator
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-12-11 03:35:51 -06:00
|
|
|
/// "Elaboration" is the process of identifying all the predicates that
|
|
|
|
/// are implied by a source predicate. Currently this basically means
|
|
|
|
/// walking the "supertraits" and other similar assumptions. For
|
|
|
|
/// example, if we know that `T : Ord`, the elaborator would deduce
|
|
|
|
/// that `T : PartialOrd` holds as well. Similarly, if we have `trait
|
|
|
|
/// Foo : 'static`, and we know that `T : Foo`, then we know that `T :
|
|
|
|
/// 'static`.
|
2014-12-07 10:10:48 -06:00
|
|
|
pub struct Elaborator<'cx, 'tcx:'cx> {
|
2014-09-12 09:53:35 -05:00
|
|
|
tcx: &'cx ty::ctxt<'tcx>,
|
2015-02-23 14:00:49 -06:00
|
|
|
stack: Vec<ty::Predicate<'tcx>>,
|
2015-02-05 14:50:34 -06:00
|
|
|
visited: PredicateSet<'cx,'tcx>,
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
pub fn elaborate_trait_ref<'cx, 'tcx>(
|
|
|
|
tcx: &'cx ty::ctxt<'tcx>,
|
2014-12-17 13:16:28 -06:00
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>)
|
2014-12-07 10:10:48 -06:00
|
|
|
-> Elaborator<'cx, 'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
2015-06-23 13:50:50 -05:00
|
|
|
elaborate_predicates(tcx, vec![trait_ref.to_predicate()])
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
pub fn elaborate_trait_refs<'cx, 'tcx>(
|
|
|
|
tcx: &'cx ty::ctxt<'tcx>,
|
2014-12-17 13:16:28 -06:00
|
|
|
trait_refs: &[ty::PolyTraitRef<'tcx>])
|
2014-12-07 10:10:48 -06:00
|
|
|
-> Elaborator<'cx, 'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
2014-12-07 10:10:48 -06:00
|
|
|
let predicates = trait_refs.iter()
|
2015-06-23 13:50:50 -05:00
|
|
|
.map(|trait_ref| trait_ref.to_predicate())
|
2014-12-07 10:10:48 -06:00
|
|
|
.collect();
|
|
|
|
elaborate_predicates(tcx, predicates)
|
|
|
|
}
|
2014-09-18 10:08:04 -05:00
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
pub fn elaborate_predicates<'cx, 'tcx>(
|
|
|
|
tcx: &'cx ty::ctxt<'tcx>,
|
2015-02-05 10:48:20 -06:00
|
|
|
mut predicates: Vec<ty::Predicate<'tcx>>)
|
2014-12-07 10:10:48 -06:00
|
|
|
-> Elaborator<'cx, 'tcx>
|
|
|
|
{
|
2015-02-05 14:50:34 -06:00
|
|
|
let mut visited = PredicateSet::new(tcx);
|
|
|
|
predicates.retain(|pred| visited.insert(pred));
|
2015-02-23 14:00:49 -06:00
|
|
|
Elaborator { tcx: tcx, stack: predicates, visited: visited }
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
impl<'cx, 'tcx> Elaborator<'cx, 'tcx> {
|
2015-03-05 04:46:12 -06:00
|
|
|
pub fn filter_to_traits(self) -> FilterToTraits<Elaborator<'cx, 'tcx>> {
|
|
|
|
FilterToTraits::new(self)
|
2014-12-27 03:22:29 -06:00
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
fn push(&mut self, predicate: &ty::Predicate<'tcx>) {
|
|
|
|
match *predicate {
|
2014-12-17 13:16:28 -06:00
|
|
|
ty::Predicate::Trait(ref data) => {
|
2015-02-24 08:24:42 -06:00
|
|
|
// Predicates declared on the trait.
|
|
|
|
let predicates = ty::lookup_super_predicates(self.tcx, data.def_id());
|
|
|
|
|
|
|
|
let mut predicates: Vec<_> =
|
|
|
|
predicates.predicates
|
|
|
|
.iter()
|
|
|
|
.map(|p| p.subst_supertrait(self.tcx, &data.to_poly_trait_ref()))
|
|
|
|
.collect();
|
|
|
|
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("super_predicates: data={:?} predicates={:?}",
|
|
|
|
data, predicates);
|
2014-12-07 10:10:48 -06:00
|
|
|
|
|
|
|
// Only keep those bounds that we haven't already
|
|
|
|
// seen. This is necessary to prevent infinite
|
|
|
|
// recursion in some cases. One common case is when
|
2015-01-04 22:20:17 -06:00
|
|
|
// people define `trait Sized: Sized { }` rather than `trait
|
|
|
|
// Sized { }`.
|
2015-02-05 14:50:34 -06:00
|
|
|
predicates.retain(|r| self.visited.insert(r));
|
2014-12-07 10:10:48 -06:00
|
|
|
|
2015-06-10 11:22:20 -05:00
|
|
|
self.stack.extend(predicates);
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
|
|
|
ty::Predicate::Equate(..) => {
|
2014-12-17 13:16:28 -06:00
|
|
|
// Currently, we do not "elaborate" predicates like
|
|
|
|
// `X == Y`, though conceivably we might. For example,
|
|
|
|
// `&X == &Y` implies that `X == Y`.
|
|
|
|
}
|
|
|
|
ty::Predicate::Projection(..) => {
|
|
|
|
// Nothing to elaborate in a projection predicate.
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
|
|
|
ty::Predicate::RegionOutlives(..) |
|
|
|
|
ty::Predicate::TypeOutlives(..) => {
|
|
|
|
// Currently, we do not "elaborate" predicates like
|
|
|
|
// `'a : 'b` or `T : 'a`. We could conceivably do
|
|
|
|
// more here. For example,
|
|
|
|
//
|
|
|
|
// &'a int : 'b
|
|
|
|
//
|
|
|
|
// implies that
|
|
|
|
//
|
|
|
|
// 'a : 'b
|
|
|
|
//
|
|
|
|
// and we could get even more if we took WF
|
|
|
|
// constraints into account. For example,
|
|
|
|
//
|
|
|
|
// &'a &'b int : 'c
|
|
|
|
//
|
|
|
|
// implies that
|
|
|
|
//
|
|
|
|
// 'b : 'a
|
|
|
|
// 'a : 'c
|
2014-12-06 00:30:41 -06:00
|
|
|
}
|
2014-09-18 10:08:04 -05:00
|
|
|
}
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-01 22:26:38 -06:00
|
|
|
impl<'cx, 'tcx> Iterator for Elaborator<'cx, 'tcx> {
|
|
|
|
type Item = ty::Predicate<'tcx>;
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
fn next(&mut self) -> Option<ty::Predicate<'tcx>> {
|
2015-02-23 14:00:49 -06:00
|
|
|
// Extract next item from top-most stack frame, if any.
|
|
|
|
let next_predicate = match self.stack.pop() {
|
|
|
|
Some(predicate) => predicate,
|
|
|
|
None => {
|
|
|
|
// No more stack frames. Done.
|
|
|
|
return None;
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
2015-02-23 14:00:49 -06:00
|
|
|
};
|
|
|
|
self.push(&next_predicate);
|
|
|
|
return Some(next_predicate);
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 10:10:48 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Supertrait iterator
|
2014-12-11 03:35:51 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2014-12-07 10:10:48 -06:00
|
|
|
|
2015-03-05 04:46:12 -06:00
|
|
|
pub type Supertraits<'cx, 'tcx> = FilterToTraits<Elaborator<'cx, 'tcx>>;
|
2014-12-07 10:10:48 -06:00
|
|
|
|
|
|
|
pub fn supertraits<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
|
2014-12-17 13:16:28 -06:00
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>)
|
2014-12-07 10:10:48 -06:00
|
|
|
-> Supertraits<'cx, 'tcx>
|
|
|
|
{
|
2014-12-27 03:22:29 -06:00
|
|
|
elaborate_trait_ref(tcx, trait_ref).filter_to_traits()
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transitive_bounds<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
|
2014-12-17 13:16:28 -06:00
|
|
|
bounds: &[ty::PolyTraitRef<'tcx>])
|
2014-12-07 10:10:48 -06:00
|
|
|
-> Supertraits<'cx, 'tcx>
|
|
|
|
{
|
2014-12-27 03:22:29 -06:00
|
|
|
elaborate_trait_refs(tcx, bounds).filter_to_traits()
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
|
|
|
|
2015-03-26 14:51:11 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Iterator over def-ids of supertraits
|
|
|
|
|
|
|
|
pub struct SupertraitDefIds<'cx, 'tcx:'cx> {
|
|
|
|
tcx: &'cx ty::ctxt<'tcx>,
|
|
|
|
stack: Vec<ast::DefId>,
|
|
|
|
visited: FnvHashSet<ast::DefId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn supertrait_def_ids<'cx, 'tcx>(tcx: &'cx ty::ctxt<'tcx>,
|
|
|
|
trait_def_id: ast::DefId)
|
|
|
|
-> SupertraitDefIds<'cx, 'tcx>
|
|
|
|
{
|
|
|
|
SupertraitDefIds {
|
|
|
|
tcx: tcx,
|
|
|
|
stack: vec![trait_def_id],
|
|
|
|
visited: Some(trait_def_id).into_iter().collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'cx, 'tcx> Iterator for SupertraitDefIds<'cx, 'tcx> {
|
|
|
|
type Item = ast::DefId;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<ast::DefId> {
|
|
|
|
let def_id = match self.stack.pop() {
|
|
|
|
Some(def_id) => def_id,
|
|
|
|
None => { return None; }
|
|
|
|
};
|
|
|
|
|
|
|
|
let predicates = ty::lookup_super_predicates(self.tcx, def_id);
|
|
|
|
let visited = &mut self.visited;
|
|
|
|
self.stack.extend(
|
|
|
|
predicates.predicates
|
|
|
|
.iter()
|
|
|
|
.filter_map(|p| p.to_opt_poly_trait_ref())
|
|
|
|
.map(|t| t.def_id())
|
|
|
|
.filter(|&super_def_id| visited.insert(super_def_id)));
|
|
|
|
Some(def_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 14:30:14 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Other
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// A filter around an iterator of predicates that makes it yield up
|
|
|
|
/// just trait references.
|
2015-03-05 04:46:12 -06:00
|
|
|
pub struct FilterToTraits<I> {
|
2015-02-23 14:30:14 -06:00
|
|
|
base_iterator: I
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:46:12 -06:00
|
|
|
impl<I> FilterToTraits<I> {
|
|
|
|
fn new(base: I) -> FilterToTraits<I> {
|
|
|
|
FilterToTraits { base_iterator: base }
|
2015-02-23 14:30:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 04:46:12 -06:00
|
|
|
impl<'tcx,I:Iterator<Item=ty::Predicate<'tcx>>> Iterator for FilterToTraits<I> {
|
2015-01-01 22:26:38 -06:00
|
|
|
type Item = ty::PolyTraitRef<'tcx>;
|
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
|
2014-12-07 10:10:48 -06:00
|
|
|
loop {
|
2015-02-23 14:30:14 -06:00
|
|
|
match self.base_iterator.next() {
|
2014-12-07 10:10:48 -06:00
|
|
|
None => {
|
|
|
|
return None;
|
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
Some(ty::Predicate::Trait(data)) => {
|
|
|
|
return Some(data.to_poly_trait_ref());
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
2014-12-17 13:16:28 -06:00
|
|
|
Some(_) => {
|
2014-12-07 10:10:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-11 03:35:51 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Other
|
2014-12-07 10:10:48 -06:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-09-12 09:53:35 -05:00
|
|
|
// determine the `self` type, using fresh variables for all variables
|
2014-11-28 22:09:12 -06:00
|
|
|
// declared on the impl declaration e.g., `impl<A,B> for Box<[(A,B)]>`
|
2014-09-12 09:53:35 -05:00
|
|
|
// would return ($0, $1) where $0 and $1 are freshly instantiated type
|
|
|
|
// variables.
|
2015-03-30 16:46:34 -05:00
|
|
|
pub fn fresh_type_vars_for_impl<'a, 'tcx>(infcx: &InferCtxt<'a, 'tcx>,
|
|
|
|
span: Span,
|
|
|
|
impl_def_id: ast::DefId)
|
|
|
|
-> Substs<'tcx>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
|
|
|
let tcx = infcx.tcx;
|
|
|
|
let impl_generics = ty::lookup_item_type(tcx, impl_def_id).generics;
|
2014-12-29 15:32:12 -06:00
|
|
|
infcx.fresh_substs_for_generics(span, &impl_generics)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
/// See `super::obligations_for_generics`
|
2015-06-16 17:39:20 -05:00
|
|
|
pub fn predicates_for_generics<'tcx>(cause: ObligationCause<'tcx>,
|
2015-03-25 19:06:52 -05:00
|
|
|
recursion_depth: usize,
|
2015-02-11 09:28:52 -06:00
|
|
|
generic_bounds: &ty::InstantiatedPredicates<'tcx>)
|
2015-06-02 18:14:45 -05:00
|
|
|
-> Vec<PredicateObligation<'tcx>>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
2015-06-18 12:25:05 -05:00
|
|
|
debug!("predicates_for_generics(generic_bounds={:?})",
|
|
|
|
generic_bounds);
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-06-02 18:14:45 -05:00
|
|
|
generic_bounds.predicates.iter().map(|predicate| {
|
2014-12-06 10:39:25 -06:00
|
|
|
Obligation { cause: cause.clone(),
|
2014-12-07 10:10:48 -06:00
|
|
|
recursion_depth: recursion_depth,
|
2014-12-17 15:00:34 -06:00
|
|
|
predicate: predicate.clone() }
|
2015-06-02 18:14:45 -05:00
|
|
|
}).collect()
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
|
2014-12-17 13:16:28 -06:00
|
|
|
pub fn trait_ref_for_builtin_bound<'tcx>(
|
2014-09-29 14:11:30 -05:00
|
|
|
tcx: &ty::ctxt<'tcx>,
|
2014-09-12 09:53:35 -05:00
|
|
|
builtin_bound: ty::BuiltinBound,
|
2014-09-29 14:11:30 -05:00
|
|
|
param_ty: Ty<'tcx>)
|
2015-04-21 10:59:58 -05:00
|
|
|
-> Result<ty::TraitRef<'tcx>, ErrorReported>
|
2014-09-12 09:53:35 -05:00
|
|
|
{
|
|
|
|
match tcx.lang_items.from_builtin_kind(builtin_bound) {
|
|
|
|
Ok(def_id) => {
|
2015-04-21 10:59:58 -05:00
|
|
|
Ok(ty::TraitRef {
|
2014-09-18 10:08:04 -05:00
|
|
|
def_id: def_id,
|
2014-12-03 18:01:29 -06:00
|
|
|
substs: tcx.mk_substs(Substs::empty().with_self_ty(param_ty))
|
2015-04-21 10:59:58 -05:00
|
|
|
})
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2015-02-01 20:53:25 -06:00
|
|
|
tcx.sess.err(&e);
|
2014-12-06 00:30:41 -06:00
|
|
|
Err(ErrorReported)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-07 07:46:08 -06:00
|
|
|
|
2015-02-02 05:14:01 -06:00
|
|
|
pub fn predicate_for_trait_ref<'tcx>(
|
2014-09-29 14:11:30 -05:00
|
|
|
cause: ObligationCause<'tcx>,
|
2015-04-21 10:59:58 -05:00
|
|
|
trait_ref: ty::TraitRef<'tcx>,
|
2015-03-25 19:06:52 -05:00
|
|
|
recursion_depth: usize)
|
2015-04-14 18:57:29 -05:00
|
|
|
-> PredicateObligation<'tcx>
|
2014-09-18 10:08:04 -05:00
|
|
|
{
|
2015-04-14 18:57:29 -05:00
|
|
|
Obligation {
|
2014-12-06 00:30:41 -06:00
|
|
|
cause: cause,
|
|
|
|
recursion_depth: recursion_depth,
|
2015-06-23 13:50:50 -05:00
|
|
|
predicate: trait_ref.to_predicate(),
|
2015-04-14 18:57:29 -05:00
|
|
|
}
|
2014-09-18 10:08:04 -05:00
|
|
|
}
|
|
|
|
|
2015-02-26 18:13:31 -06:00
|
|
|
pub fn predicate_for_trait_def<'tcx>(
|
2015-02-02 05:14:01 -06:00
|
|
|
tcx: &ty::ctxt<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
trait_def_id: ast::DefId,
|
2015-03-25 19:06:52 -05:00
|
|
|
recursion_depth: usize,
|
2015-04-14 18:57:29 -05:00
|
|
|
param_ty: Ty<'tcx>,
|
|
|
|
ty_params: Vec<Ty<'tcx>>)
|
|
|
|
-> PredicateObligation<'tcx>
|
2015-02-02 05:14:01 -06:00
|
|
|
{
|
2015-04-21 10:59:58 -05:00
|
|
|
let trait_ref = ty::TraitRef {
|
2015-02-02 05:14:01 -06:00
|
|
|
def_id: trait_def_id,
|
2015-04-14 18:57:29 -05:00
|
|
|
substs: tcx.mk_substs(Substs::new_trait(ty_params, vec![], param_ty))
|
2015-05-11 21:41:08 -05:00
|
|
|
};
|
2015-02-07 07:46:08 -06:00
|
|
|
predicate_for_trait_ref(cause, trait_ref, recursion_depth)
|
2015-02-02 05:14:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn predicate_for_builtin_bound<'tcx>(
|
|
|
|
tcx: &ty::ctxt<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
|
|
|
builtin_bound: ty::BuiltinBound,
|
2015-03-25 19:06:52 -05:00
|
|
|
recursion_depth: usize,
|
2015-02-02 05:14:01 -06:00
|
|
|
param_ty: Ty<'tcx>)
|
|
|
|
-> Result<PredicateObligation<'tcx>, ErrorReported>
|
|
|
|
{
|
|
|
|
let trait_ref = try!(trait_ref_for_builtin_bound(tcx, builtin_bound, param_ty));
|
2015-04-14 18:57:29 -05:00
|
|
|
Ok(predicate_for_trait_ref(cause, trait_ref, recursion_depth))
|
2015-02-02 05:14:01 -06:00
|
|
|
}
|
|
|
|
|
2014-12-20 08:15:52 -06:00
|
|
|
/// Cast a trait reference into a reference to one of its super
|
|
|
|
/// traits; returns `None` if `target_trait_def_id` is not a
|
|
|
|
/// supertrait.
|
|
|
|
pub fn upcast<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
source_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
target_trait_def_id: ast::DefId)
|
2015-03-03 07:01:13 -06:00
|
|
|
-> Vec<ty::PolyTraitRef<'tcx>>
|
2014-12-20 08:15:52 -06:00
|
|
|
{
|
|
|
|
if source_trait_ref.def_id() == target_trait_def_id {
|
2015-03-03 07:01:13 -06:00
|
|
|
return vec![source_trait_ref]; // shorcut the most common case
|
2014-12-20 08:15:52 -06:00
|
|
|
}
|
|
|
|
|
2015-03-03 07:01:13 -06:00
|
|
|
supertraits(tcx, source_trait_ref)
|
|
|
|
.filter(|r| r.def_id() == target_trait_def_id)
|
|
|
|
.collect()
|
2014-12-20 08:15:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Given an object of type `object_trait_ref`, returns the index of
|
|
|
|
/// the method `n_method` found in the trait `trait_def_id` (which
|
|
|
|
/// should be a supertrait of `object_trait_ref`) within the vtable
|
|
|
|
/// for `object_trait_ref`.
|
|
|
|
pub fn get_vtable_index_of_object_method<'tcx>(tcx: &ty::ctxt<'tcx>,
|
|
|
|
object_trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
trait_def_id: ast::DefId,
|
2015-03-25 19:06:52 -05:00
|
|
|
method_offset_in_trait: usize) -> usize {
|
2014-12-20 08:15:52 -06:00
|
|
|
// We need to figure the "real index" of the method in a
|
|
|
|
// listing of all the methods of an object. We do this by
|
|
|
|
// iterating down the supertraits of the object's trait until
|
|
|
|
// we find the trait the method came from, counting up the
|
|
|
|
// methods from them.
|
|
|
|
let mut method_count = 0;
|
2015-01-11 14:18:06 -06:00
|
|
|
|
|
|
|
for bound_ref in transitive_bounds(tcx, &[object_trait_ref]) {
|
2014-12-20 08:15:52 -06:00
|
|
|
if bound_ref.def_id() == trait_def_id {
|
2015-01-11 14:18:06 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
let trait_items = ty::trait_items(tcx, bound_ref.def_id());
|
2015-06-11 07:56:07 -05:00
|
|
|
for trait_item in trait_items.iter() {
|
2015-01-11 14:18:06 -06:00
|
|
|
match *trait_item {
|
|
|
|
ty::MethodTraitItem(_) => method_count += 1,
|
2015-03-14 13:05:00 -05:00
|
|
|
_ => {}
|
2014-12-20 08:15:52 -06:00
|
|
|
}
|
|
|
|
}
|
2015-01-11 14:18:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// count number of methods preceding the one we are selecting and
|
|
|
|
// add them to the total offset; skip over associated types.
|
|
|
|
let trait_items = ty::trait_items(tcx, trait_def_id);
|
|
|
|
for trait_item in trait_items.iter().take(method_offset_in_trait) {
|
|
|
|
match *trait_item {
|
|
|
|
ty::MethodTraitItem(_) => method_count += 1,
|
2015-03-14 13:05:00 -05:00
|
|
|
_ => {}
|
2015-01-11 14:18:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// the item at the offset we were given really ought to be a method
|
|
|
|
assert!(match trait_items[method_offset_in_trait] {
|
|
|
|
ty::MethodTraitItem(_) => true,
|
2015-03-14 13:05:00 -05:00
|
|
|
_ => false
|
2014-12-20 08:15:52 -06:00
|
|
|
});
|
2015-01-11 14:18:06 -06:00
|
|
|
|
|
|
|
method_count
|
2014-12-20 08:15:52 -06:00
|
|
|
}
|
|
|
|
|
2015-01-11 14:18:06 -06:00
|
|
|
pub enum TupleArgumentsFlag { Yes, No }
|
|
|
|
|
|
|
|
pub fn closure_trait_ref_and_return_type<'tcx>(
|
|
|
|
tcx: &ty::ctxt<'tcx>,
|
2015-01-10 10:54:15 -06:00
|
|
|
fn_trait_def_id: ast::DefId,
|
|
|
|
self_ty: Ty<'tcx>,
|
2015-01-11 14:18:06 -06:00
|
|
|
sig: &ty::PolyFnSig<'tcx>,
|
|
|
|
tuple_arguments: TupleArgumentsFlag)
|
2015-04-21 10:59:58 -05:00
|
|
|
-> ty::Binder<(ty::TraitRef<'tcx>, Ty<'tcx>)>
|
2015-01-10 10:54:15 -06:00
|
|
|
{
|
2015-01-11 14:18:06 -06:00
|
|
|
let arguments_tuple = match tuple_arguments {
|
|
|
|
TupleArgumentsFlag::No => sig.0.inputs[0],
|
|
|
|
TupleArgumentsFlag::Yes => ty::mk_tup(tcx, sig.0.inputs.to_vec()),
|
|
|
|
};
|
|
|
|
let trait_substs = Substs::new_trait(vec![arguments_tuple], vec![], self_ty);
|
2015-04-21 10:59:58 -05:00
|
|
|
let trait_ref = ty::TraitRef {
|
2015-01-10 10:54:15 -06:00
|
|
|
def_id: fn_trait_def_id,
|
|
|
|
substs: tcx.mk_substs(trait_substs),
|
2015-04-21 10:59:58 -05:00
|
|
|
};
|
2015-04-06 08:17:35 -05:00
|
|
|
ty::Binder((trait_ref, sig.0.output.unwrap_or(ty::mk_nil(tcx))))
|
2015-01-10 10:54:15 -06:00
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx,O:fmt::Debug> fmt::Debug for super::Obligation<'tcx, O> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "Obligation(predicate={:?},depth={})",
|
|
|
|
self.predicate,
|
|
|
|
self.recursion_depth)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, N:fmt::Debug> fmt::Debug for super::Vtable<'tcx, N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-09-12 09:53:35 -05:00
|
|
|
match *self {
|
|
|
|
super::VtableImpl(ref v) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "{:?}", v),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2015-02-07 07:24:34 -06:00
|
|
|
super::VtableDefaultImpl(ref t) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "{:?}", t),
|
2015-01-24 07:17:24 -06:00
|
|
|
|
2015-06-09 16:09:37 -05:00
|
|
|
super::VtableClosure(ref d) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "{:?}", d),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-12-01 08:23:40 -06:00
|
|
|
super::VtableFnPointer(ref d) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "VtableFnPointer({:?})", d),
|
2014-12-01 08:23:40 -06:00
|
|
|
|
2014-12-23 04:26:34 -06:00
|
|
|
super::VtableObject(ref d) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "VtableObject({:?})", d),
|
2014-12-23 04:26:34 -06:00
|
|
|
|
2015-01-08 20:41:42 -06:00
|
|
|
super::VtableParam(ref n) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "VtableParam({:?})", n),
|
2014-09-12 09:53:35 -05:00
|
|
|
|
2014-10-09 16:19:50 -05:00
|
|
|
super::VtableBuiltin(ref d) =>
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "{:?}", d)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, N:fmt::Debug> fmt::Debug for super::VtableImplData<'tcx, N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "VtableImpl(impl_def_id={:?}, substs={:?}, nested={:?})",
|
|
|
|
self.impl_def_id,
|
|
|
|
self.substs,
|
|
|
|
self.nested)
|
2015-02-02 05:14:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, N:fmt::Debug> fmt::Debug for super::VtableClosureData<'tcx, N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "VtableClosure(closure_def_id={:?}, substs={:?}, nested={:?})",
|
|
|
|
self.closure_def_id,
|
|
|
|
self.substs,
|
|
|
|
self.nested)
|
2014-10-09 16:19:50 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, N:fmt::Debug> fmt::Debug for super::VtableBuiltinData<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "VtableBuiltin(nested={:?})", self.nested)
|
2014-12-23 04:26:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx, N:fmt::Debug> fmt::Debug for super::VtableDefaultImplData<N> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "VtableDefaultImplData(trait_def_id={:?}, nested={:?})",
|
|
|
|
self.trait_def_id,
|
|
|
|
self.nested)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx> fmt::Debug for super::VtableObjectData<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "VtableObject(object_ty={:?})", self.object_ty)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 00:51:23 -05:00
|
|
|
impl<'tcx> fmt::Debug for super::FulfillmentError<'tcx> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "FulfillmentError({:?},{:?})",
|
|
|
|
self.obligation,
|
|
|
|
self.code)
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<'tcx> fmt::Debug for super::FulfillmentErrorCode<'tcx> {
|
2014-09-12 09:53:35 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
2014-12-20 02:09:35 -06:00
|
|
|
super::CodeSelectionError(ref e) => write!(f, "{:?}", e),
|
|
|
|
super::CodeProjectionError(ref e) => write!(f, "{:?}", e),
|
2014-09-11 00:07:49 -05:00
|
|
|
super::CodeAmbiguity => write!(f, "Ambiguity")
|
2014-09-12 09:53:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<'tcx> fmt::Debug for super::MismatchedProjectionTypes<'tcx> {
|
2014-12-30 07:59:33 -06:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-06-18 00:51:23 -05:00
|
|
|
write!(f, "MismatchedProjectionTypes({:?})", self.err)
|
2014-12-30 07:59:33 -06:00
|
|
|
}
|
|
|
|
}
|