2020-02-22 10:44:18 +00:00
|
|
|
use smallvec::smallvec;
|
|
|
|
|
2020-03-03 23:07:04 +00:00
|
|
|
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
|
2020-03-29 15:19:48 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::ty::outlives::Component;
|
2020-06-18 18:33:52 +00:00
|
|
|
use rustc_middle::ty::{self, ToPredicate, TyCtxt, WithConstness};
|
2020-02-22 10:44:18 +00:00
|
|
|
|
2020-03-31 23:50:15 +00:00
|
|
|
pub fn anonymize_predicate<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-05-23 09:09:32 +00:00
|
|
|
pred: ty::Predicate<'tcx>,
|
2020-03-31 23:50:15 +00:00
|
|
|
) -> ty::Predicate<'tcx> {
|
2020-06-24 16:06:04 +00:00
|
|
|
match pred.kind() {
|
2020-06-18 18:41:43 +00:00
|
|
|
ty::PredicateKind::ForAll(binder) => {
|
2020-06-19 22:21:59 +00:00
|
|
|
let new = ty::PredicateKind::ForAll(tcx.anonymize_late_bound_regions(binder));
|
2020-06-24 16:06:04 +00:00
|
|
|
tcx.reuse_or_mk_predicate(pred, new)
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateKind::Atom(_) => pred,
|
2020-06-19 22:21:59 +00:00
|
|
|
}
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PredicateSet<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
set: FxHashSet<ty::Predicate<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PredicateSet<'tcx> {
|
|
|
|
fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2020-03-21 12:53:34 +00:00
|
|
|
Self { tcx, set: Default::default() }
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 09:09:32 +00:00
|
|
|
fn insert(&mut self, pred: ty::Predicate<'tcx>) -> bool {
|
2020-02-22 10:44:18 +00:00
|
|
|
// We have to be careful here because we want
|
|
|
|
//
|
2020-06-06 10:05:37 +00:00
|
|
|
// for<'a> Foo<&'a i32>
|
2020-02-22 10:44:18 +00:00
|
|
|
//
|
|
|
|
// and
|
|
|
|
//
|
2020-06-06 10:05:37 +00:00
|
|
|
// for<'b> Foo<&'b i32>
|
2020-02-22 10:44:18 +00:00
|
|
|
//
|
|
|
|
// to be considered equivalent. So normalize all late-bound
|
|
|
|
// regions before we throw things into the underlying set.
|
|
|
|
self.set.insert(anonymize_predicate(self.tcx, pred))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:09:32 +00:00
|
|
|
impl Extend<ty::Predicate<'tcx>> for PredicateSet<'tcx> {
|
|
|
|
fn extend<I: IntoIterator<Item = ty::Predicate<'tcx>>>(&mut self, iter: I) {
|
2020-02-22 10:44:18 +00:00
|
|
|
for pred in iter {
|
2020-05-23 09:09:32 +00:00
|
|
|
self.insert(pred);
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-13 03:09:55 +00:00
|
|
|
|
|
|
|
fn extend_one(&mut self, pred: ty::Predicate<'tcx>) {
|
|
|
|
self.insert(pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_reserve(&mut self, additional: usize) {
|
|
|
|
Extend::<ty::Predicate<'tcx>>::extend_reserve(&mut self.set, additional);
|
|
|
|
}
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// `Elaboration` iterator
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// "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`.
|
|
|
|
pub struct Elaborator<'tcx> {
|
2020-03-03 23:07:04 +00:00
|
|
|
stack: Vec<PredicateObligation<'tcx>>,
|
2020-02-22 10:44:18 +00:00
|
|
|
visited: PredicateSet<'tcx>,
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:50:15 +00:00
|
|
|
pub fn elaborate_trait_ref<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> Elaborator<'tcx> {
|
2020-05-07 10:12:19 +00:00
|
|
|
elaborate_predicates(tcx, std::iter::once(trait_ref.without_const().to_predicate(tcx)))
|
2020-03-31 23:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn elaborate_trait_refs<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
|
|
|
|
) -> Elaborator<'tcx> {
|
2020-05-07 10:12:19 +00:00
|
|
|
let predicates = trait_refs.map(|trait_ref| trait_ref.without_const().to_predicate(tcx));
|
2020-03-31 23:50:15 +00:00
|
|
|
elaborate_predicates(tcx, predicates)
|
|
|
|
}
|
|
|
|
|
2020-02-22 10:44:18 +00:00
|
|
|
pub fn elaborate_predicates<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-18 01:31:25 +00:00
|
|
|
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
|
2020-02-22 10:44:18 +00:00
|
|
|
) -> Elaborator<'tcx> {
|
2020-06-27 20:36:35 +00:00
|
|
|
let obligations = predicates
|
|
|
|
.map(|predicate| {
|
|
|
|
predicate_obligation(predicate, ty::ParamEnv::empty(), ObligationCause::dummy())
|
|
|
|
})
|
|
|
|
.collect();
|
2020-03-03 23:07:04 +00:00
|
|
|
elaborate_obligations(tcx, obligations)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn elaborate_obligations<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
mut obligations: Vec<PredicateObligation<'tcx>>,
|
|
|
|
) -> Elaborator<'tcx> {
|
|
|
|
let mut visited = PredicateSet::new(tcx);
|
2020-05-23 09:09:32 +00:00
|
|
|
obligations.retain(|obligation| visited.insert(obligation.predicate));
|
2020-03-03 23:07:04 +00:00
|
|
|
Elaborator { stack: obligations, visited }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn predicate_obligation<'tcx>(
|
|
|
|
predicate: ty::Predicate<'tcx>,
|
2020-06-27 20:36:35 +00:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
cause: ObligationCause<'tcx>,
|
2020-03-03 23:07:04 +00:00
|
|
|
) -> PredicateObligation<'tcx> {
|
2020-06-27 20:36:35 +00:00
|
|
|
Obligation { cause, param_env, recursion_depth: 0, predicate }
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Elaborator<'tcx> {
|
2020-06-21 10:26:17 +00:00
|
|
|
pub fn filter_to_traits(self) -> FilterToTraits<Self> {
|
|
|
|
FilterToTraits::new(self)
|
2020-03-31 23:50:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-03 23:07:04 +00:00
|
|
|
fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) {
|
2020-02-22 10:44:18 +00:00
|
|
|
let tcx = self.visited.tcx;
|
2020-06-19 17:19:21 +00:00
|
|
|
|
2020-10-08 00:02:06 +00:00
|
|
|
let bound_predicate = obligation.predicate.bound_atom(tcx);
|
|
|
|
match bound_predicate.skip_binder() {
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::Trait(data, _) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Get predicates declared on the trait.
|
|
|
|
let predicates = tcx.super_predicates_of(data.def_id());
|
|
|
|
|
2020-06-27 20:36:35 +00:00
|
|
|
let obligations = predicates.predicates.iter().map(|&(pred, _)| {
|
2020-03-03 23:07:04 +00:00
|
|
|
predicate_obligation(
|
2020-10-08 00:02:06 +00:00
|
|
|
pred.subst_supertrait(tcx, &bound_predicate.map_bound(|_| data.trait_ref)),
|
2020-06-27 20:36:35 +00:00
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
2020-03-03 23:07:04 +00:00
|
|
|
)
|
|
|
|
});
|
2020-04-20 17:42:12 +00:00
|
|
|
debug!("super_predicates: data={:?}", data);
|
2020-02-22 10:44:18 +00: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 people define
|
|
|
|
// `trait Sized: Sized { }` rather than `trait Sized { }`.
|
|
|
|
let visited = &mut self.visited;
|
2020-05-23 09:09:32 +00:00
|
|
|
let obligations = obligations.filter(|o| visited.insert(o.predicate));
|
2020-02-22 10:44:18 +00:00
|
|
|
|
2020-03-03 23:07:04 +00:00
|
|
|
self.stack.extend(obligations);
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::WellFormed(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Currently, we do not elaborate WF predicates,
|
|
|
|
// although we easily could.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::ObjectSafe(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Currently, we do not elaborate object-safe
|
|
|
|
// predicates.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::Subtype(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Currently, we do not "elaborate" predicates like `X <: Y`,
|
|
|
|
// though conceivably we might.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::Projection(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Nothing to elaborate in a projection predicate.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::ClosureKind(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Nothing to elaborate when waiting for a closure's kind to be inferred.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::ConstEvaluatable(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Currently, we do not elaborate const-evaluatable
|
|
|
|
// predicates.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::ConstEquate(..) => {
|
2020-02-28 21:03:04 +00:00
|
|
|
// Currently, we do not elaborate const-equate
|
|
|
|
// predicates.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::RegionOutlives(..) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Nothing to elaborate from `'a: 'b`.
|
|
|
|
}
|
2020-07-08 22:35:55 +00:00
|
|
|
ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty_max, r_min)) => {
|
2020-02-22 10:44:18 +00:00
|
|
|
// We know that `T: 'a` for some type `T`. We can
|
|
|
|
// often elaborate this. For example, if we know that
|
|
|
|
// `[U]: 'a`, that implies that `U: 'a`. Similarly, if
|
|
|
|
// we know `&'a U: 'b`, then we know that `'a: 'b` and
|
|
|
|
// `U: 'b`.
|
|
|
|
//
|
|
|
|
// We can basically ignore bound regions here. So for
|
|
|
|
// example `for<'c> Foo<'a,'c>: 'b` can be elaborated to
|
|
|
|
// `'a: 'b`.
|
|
|
|
|
|
|
|
// Ignore `for<'a> T: 'a` -- we might in the future
|
|
|
|
// consider this as evidence that `T: 'static`, but
|
|
|
|
// I'm a bit wary of such constructions and so for now
|
|
|
|
// I want to be conservative. --nmatsakis
|
|
|
|
if r_min.is_late_bound() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let visited = &mut self.visited;
|
|
|
|
let mut components = smallvec![];
|
|
|
|
tcx.push_outlives_components(ty_max, &mut components);
|
|
|
|
self.stack.extend(
|
|
|
|
components
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|component| match component {
|
|
|
|
Component::Region(r) => {
|
|
|
|
if r.is_late_bound() {
|
|
|
|
None
|
|
|
|
} else {
|
2020-07-08 22:35:55 +00:00
|
|
|
Some(ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(
|
2020-06-17 09:30:18 +00:00
|
|
|
r, r_min,
|
2020-02-22 10:44:18 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Component::Param(p) => {
|
|
|
|
let ty = tcx.mk_ty_param(p.index, p.name);
|
2020-07-08 22:35:55 +00:00
|
|
|
Some(ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(
|
2020-06-17 09:30:18 +00:00
|
|
|
ty, r_min,
|
2020-02-22 10:44:18 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
|
|
|
|
Component::UnresolvedInferenceVariable(_) => None,
|
|
|
|
|
|
|
|
Component::Projection(_) | Component::EscapingProjection(_) => {
|
|
|
|
// We can probably do more here. This
|
|
|
|
// corresponds to a case like `<T as
|
|
|
|
// Foo<'a>>::U: 'b`.
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2020-05-11 20:06:41 +00:00
|
|
|
.map(|predicate_kind| predicate_kind.to_predicate(tcx))
|
2020-05-23 09:09:32 +00:00
|
|
|
.filter(|&predicate| visited.insert(predicate))
|
2020-06-27 20:36:35 +00:00
|
|
|
.map(|predicate| {
|
|
|
|
predicate_obligation(
|
|
|
|
predicate,
|
|
|
|
obligation.param_env,
|
|
|
|
obligation.cause.clone(),
|
|
|
|
)
|
|
|
|
}),
|
2020-02-22 10:44:18 +00:00
|
|
|
);
|
|
|
|
}
|
2020-09-01 15:58:34 +00:00
|
|
|
ty::PredicateAtom::TypeWellFormedFromEnv(..) => {
|
|
|
|
// Nothing to elaborate
|
|
|
|
}
|
2020-02-22 10:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Iterator for Elaborator<'tcx> {
|
2020-03-03 23:07:04 +00:00
|
|
|
type Item = PredicateObligation<'tcx>;
|
2020-02-22 10:44:18 +00:00
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
(self.stack.len(), None)
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:07:04 +00:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2020-02-22 10:44:18 +00:00
|
|
|
// Extract next item from top-most stack frame, if any.
|
2020-03-03 23:07:04 +00:00
|
|
|
if let Some(obligation) = self.stack.pop() {
|
|
|
|
self.elaborate(&obligation);
|
|
|
|
Some(obligation)
|
2020-02-22 10:44:18 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 23:50:15 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Supertrait iterator
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2020-06-21 10:26:17 +00:00
|
|
|
pub type Supertraits<'tcx> = FilterToTraits<Elaborator<'tcx>>;
|
2020-03-31 23:50:15 +00:00
|
|
|
|
|
|
|
pub fn supertraits<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
|
|
|
) -> Supertraits<'tcx> {
|
|
|
|
elaborate_trait_ref(tcx, trait_ref).filter_to_traits()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn transitive_bounds<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
|
|
|
|
) -> Supertraits<'tcx> {
|
|
|
|
elaborate_trait_refs(tcx, bounds).filter_to_traits()
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Other
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// A filter around an iterator of predicates that makes it yield up
|
|
|
|
/// just trait references.
|
2020-06-21 10:26:17 +00:00
|
|
|
pub struct FilterToTraits<I> {
|
2020-03-31 23:50:15 +00:00
|
|
|
base_iterator: I,
|
|
|
|
}
|
|
|
|
|
2020-06-21 10:26:17 +00:00
|
|
|
impl<I> FilterToTraits<I> {
|
|
|
|
fn new(base: I) -> FilterToTraits<I> {
|
|
|
|
FilterToTraits { base_iterator: base }
|
2020-03-31 23:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 10:26:17 +00:00
|
|
|
impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToTraits<I> {
|
2020-03-31 23:50:15 +00:00
|
|
|
type Item = ty::PolyTraitRef<'tcx>;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
|
2020-03-03 23:07:04 +00:00
|
|
|
while let Some(obligation) = self.base_iterator.next() {
|
2020-06-21 10:26:17 +00:00
|
|
|
if let Some(data) = obligation.predicate.to_opt_poly_trait_ref() {
|
2020-06-18 18:33:52 +00:00
|
|
|
return Some(data);
|
2020-03-31 23:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
let (_, upper) = self.base_iterator.size_hint();
|
|
|
|
(0, upper)
|
|
|
|
}
|
|
|
|
}
|