2020-02-22 11:44:18 +01:00
|
|
|
use smallvec::smallvec;
|
|
|
|
|
|
2021-10-08 22:55:06 -04:00
|
|
|
use crate::infer::outlives::components::{push_outlives_components, Component};
|
2023-01-12 21:32:06 +00:00
|
|
|
use crate::traits::{self, Obligation, ObligationCause, PredicateObligation};
|
2020-12-03 20:10:55 -03:00
|
|
|
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
|
2021-12-12 12:34:46 +08:00
|
|
|
use rustc_middle::ty::{self, ToPredicate, TyCtxt};
|
2020-12-03 20:10:55 -03:00
|
|
|
use rustc_span::symbol::Ident;
|
2021-11-13 23:56:22 +01:00
|
|
|
use rustc_span::Span;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2020-03-31 16:50:15 -07:00
|
|
|
pub fn anonymize_predicate<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-05-23 11:09:32 +02:00
|
|
|
pred: ty::Predicate<'tcx>,
|
2020-03-31 16:50:15 -07:00
|
|
|
) -> ty::Predicate<'tcx> {
|
2022-07-25 20:24:13 +02:00
|
|
|
let new = tcx.anonymize_bound_vars(pred.kind());
|
2020-12-23 16:36:23 -05:00
|
|
|
tcx.reuse_or_mk_predicate(pred, new)
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
pub struct PredicateSet<'tcx> {
|
2020-02-22 11:44:18 +01:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
set: FxHashSet<ty::Predicate<'tcx>>,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> PredicateSet<'tcx> {
|
2021-06-17 12:20:18 +08:00
|
|
|
pub fn new(tcx: TyCtxt<'tcx>) -> Self {
|
2020-03-21 13:53:34 +01:00
|
|
|
Self { tcx, set: Default::default() }
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-17 12:20:18 +08:00
|
|
|
pub fn insert(&mut self, pred: ty::Predicate<'tcx>) -> bool {
|
2020-02-22 11:44:18 +01:00
|
|
|
// We have to be careful here because we want
|
|
|
|
|
//
|
2020-06-06 12:05:37 +02:00
|
|
|
// for<'a> Foo<&'a i32>
|
2020-02-22 11:44:18 +01:00
|
|
|
//
|
|
|
|
|
// and
|
|
|
|
|
//
|
2020-06-06 12:05:37 +02:00
|
|
|
// for<'b> Foo<&'b i32>
|
2020-02-22 11:44:18 +01: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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> Extend<ty::Predicate<'tcx>> for PredicateSet<'tcx> {
|
2020-05-23 11:09:32 +02:00
|
|
|
fn extend<I: IntoIterator<Item = ty::Predicate<'tcx>>>(&mut self, iter: I) {
|
2020-02-22 11:44:18 +01:00
|
|
|
for pred in iter {
|
2020-05-23 11:09:32 +02:00
|
|
|
self.insert(pred);
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-12 20:09:55 -07: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 11:44:18 +01: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 15:07:04 -08:00
|
|
|
stack: Vec<PredicateObligation<'tcx>>,
|
2020-02-22 11:44:18 +01:00
|
|
|
visited: PredicateSet<'tcx>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 16:50:15 -07: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 16:50:15 -07: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 16:50:15 -07:00
|
|
|
elaborate_predicates(tcx, predicates)
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-22 11:44:18 +01:00
|
|
|
pub fn elaborate_predicates<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-17 18:31:25 -07:00
|
|
|
predicates: impl Iterator<Item = ty::Predicate<'tcx>>,
|
2020-02-22 11:44:18 +01:00
|
|
|
) -> Elaborator<'tcx> {
|
2020-06-27 21:36:35 +01:00
|
|
|
let obligations = predicates
|
|
|
|
|
.map(|predicate| {
|
|
|
|
|
predicate_obligation(predicate, ty::ParamEnv::empty(), ObligationCause::dummy())
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2020-03-03 15:07:04 -08:00
|
|
|
elaborate_obligations(tcx, obligations)
|
2021-11-13 23:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn elaborate_predicates_with_span<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
predicates: impl Iterator<Item = (ty::Predicate<'tcx>, Span)>,
|
|
|
|
|
) -> Elaborator<'tcx> {
|
|
|
|
|
let obligations = predicates
|
|
|
|
|
.map(|(predicate, span)| {
|
|
|
|
|
predicate_obligation(
|
|
|
|
|
predicate,
|
|
|
|
|
ty::ParamEnv::empty(),
|
|
|
|
|
ObligationCause::dummy_with_span(span),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
elaborate_obligations(tcx, obligations)
|
2020-03-03 15:07:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn elaborate_obligations<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
mut obligations: Vec<PredicateObligation<'tcx>>,
|
|
|
|
|
) -> Elaborator<'tcx> {
|
|
|
|
|
let mut visited = PredicateSet::new(tcx);
|
2020-05-23 11:09:32 +02:00
|
|
|
obligations.retain(|obligation| visited.insert(obligation.predicate));
|
2020-03-03 15:07:04 -08:00
|
|
|
Elaborator { stack: obligations, visited }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn predicate_obligation<'tcx>(
|
|
|
|
|
predicate: ty::Predicate<'tcx>,
|
2020-06-27 21:36:35 +01:00
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
|
cause: ObligationCause<'tcx>,
|
2020-03-03 15:07:04 -08:00
|
|
|
) -> PredicateObligation<'tcx> {
|
2020-06-27 21:36:35 +01:00
|
|
|
Obligation { cause, param_env, recursion_depth: 0, predicate }
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> Elaborator<'tcx> {
|
2020-06-21 12:26:17 +02:00
|
|
|
pub fn filter_to_traits(self) -> FilterToTraits<Self> {
|
|
|
|
|
FilterToTraits::new(self)
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:07:04 -08:00
|
|
|
fn elaborate(&mut self, obligation: &PredicateObligation<'tcx>) {
|
2020-02-22 11:44:18 +01:00
|
|
|
let tcx = self.visited.tcx;
|
2020-06-19 19:19:21 +02:00
|
|
|
|
2021-01-07 11:20:28 -05:00
|
|
|
let bound_predicate = obligation.predicate.kind();
|
2020-10-07 20:02:06 -04:00
|
|
|
match bound_predicate.skip_binder() {
|
2022-11-24 18:14:58 -03:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::Trait(data)) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Get predicates declared on the trait.
|
|
|
|
|
let predicates = tcx.super_predicates_of(data.def_id());
|
|
|
|
|
|
2023-01-03 19:54:11 -08:00
|
|
|
let obligations =
|
|
|
|
|
predicates.predicates.iter().enumerate().map(|(index, &(mut pred, span))| {
|
|
|
|
|
// when parent predicate is non-const, elaborate it to non-const predicates.
|
|
|
|
|
if data.constness == ty::BoundConstness::NotConst {
|
|
|
|
|
pred = pred.without_const(tcx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cause = obligation.cause.clone().derived_cause(
|
|
|
|
|
bound_predicate.rebind(data),
|
|
|
|
|
|derived| {
|
|
|
|
|
traits::ImplDerivedObligation(Box::new(
|
|
|
|
|
traits::ImplDerivedObligationCause {
|
|
|
|
|
derived,
|
2023-02-16 21:55:53 +00:00
|
|
|
impl_or_alias_def_id: data.def_id(),
|
2023-01-03 19:54:11 -08:00
|
|
|
impl_def_predicate_index: Some(index),
|
|
|
|
|
span,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
predicate_obligation(
|
|
|
|
|
pred.subst_supertrait(tcx, &bound_predicate.rebind(data.trait_ref)),
|
|
|
|
|
obligation.param_env,
|
|
|
|
|
cause,
|
|
|
|
|
)
|
|
|
|
|
});
|
2021-12-19 22:01:48 -05:00
|
|
|
debug!(?data, ?obligations, "super_predicates");
|
2020-02-22 11:44:18 +01: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 11:09:32 +02:00
|
|
|
let obligations = obligations.filter(|o| visited.insert(o.predicate));
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2020-03-03 15:07:04 -08:00
|
|
|
self.stack.extend(obligations);
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::WellFormed(..) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Currently, we do not elaborate WF predicates,
|
|
|
|
|
// although we easily could.
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ObjectSafe(..) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Currently, we do not elaborate object-safe
|
|
|
|
|
// predicates.
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::Subtype(..) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Currently, we do not "elaborate" predicates like `X <: Y`,
|
|
|
|
|
// though conceivably we might.
|
|
|
|
|
}
|
2020-11-21 07:06:16 -05:00
|
|
|
ty::PredicateKind::Coerce(..) => {
|
|
|
|
|
// Currently, we do not "elaborate" predicates like `X -> Y`,
|
|
|
|
|
// though conceivably we might.
|
|
|
|
|
}
|
2022-11-24 18:14:58 -03:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::Projection(..)) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Nothing to elaborate in a projection predicate.
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ClosureKind(..) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Nothing to elaborate when waiting for a closure's kind to be inferred.
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ConstEvaluatable(..) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Currently, we do not elaborate const-evaluatable
|
|
|
|
|
// predicates.
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::ConstEquate(..) => {
|
2020-02-29 10:03:04 +13:00
|
|
|
// Currently, we do not elaborate const-equate
|
|
|
|
|
// predicates.
|
|
|
|
|
}
|
2022-11-24 18:14:58 -03:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::RegionOutlives(..)) => {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Nothing to elaborate from `'a: 'b`.
|
|
|
|
|
}
|
2022-11-24 18:14:58 -03:00
|
|
|
ty::PredicateKind::Clause(ty::Clause::TypeOutlives(ty::OutlivesPredicate(
|
|
|
|
|
ty_max,
|
|
|
|
|
r_min,
|
|
|
|
|
))) => {
|
2020-02-22 11:44:18 +01: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![];
|
2021-10-08 22:55:06 -04:00
|
|
|
push_outlives_components(tcx, ty_max, &mut components);
|
2020-02-22 11:44:18 +01:00
|
|
|
self.stack.extend(
|
|
|
|
|
components
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|component| match component {
|
|
|
|
|
Component::Region(r) => {
|
|
|
|
|
if r.is_late_bound() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2022-11-24 18:14:58 -03:00
|
|
|
Some(ty::PredicateKind::Clause(ty::Clause::RegionOutlives(
|
|
|
|
|
ty::OutlivesPredicate(r, r_min),
|
2020-02-22 11:44:18 +01:00
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Component::Param(p) => {
|
|
|
|
|
let ty = tcx.mk_ty_param(p.index, p.name);
|
2022-11-24 18:14:58 -03:00
|
|
|
Some(ty::PredicateKind::Clause(ty::Clause::TypeOutlives(
|
|
|
|
|
ty::OutlivesPredicate(ty, r_min),
|
2020-02-22 11:44:18 +01:00
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Component::UnresolvedInferenceVariable(_) => None,
|
|
|
|
|
|
2023-01-15 19:38:31 +03:00
|
|
|
Component::Alias(alias_ty) => {
|
|
|
|
|
// We might end up here if we have `Foo<<Bar as Baz>::Assoc>: 'a`.
|
|
|
|
|
// With this, we can deduce that `<Bar as Baz>::Assoc: 'a`.
|
2022-11-24 18:14:58 -03:00
|
|
|
Some(ty::PredicateKind::Clause(ty::Clause::TypeOutlives(
|
2023-01-15 19:38:31 +03:00
|
|
|
ty::OutlivesPredicate(alias_ty.to_ty(tcx), r_min),
|
2022-01-09 23:53:57 -05:00
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-15 19:38:31 +03:00
|
|
|
Component::EscapingAlias(_) => {
|
2022-01-09 23:53:57 -05:00
|
|
|
// We might be able to do more here, but we don't
|
|
|
|
|
// want to deal with escaping vars right now.
|
2020-02-22 11:44:18 +01:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
2022-05-18 16:01:10 +00:00
|
|
|
.map(|predicate_kind| {
|
|
|
|
|
bound_predicate.rebind(predicate_kind).to_predicate(tcx)
|
|
|
|
|
})
|
2020-05-23 11:09:32 +02:00
|
|
|
.filter(|&predicate| visited.insert(predicate))
|
2020-06-27 21:36:35 +01:00
|
|
|
.map(|predicate| {
|
|
|
|
|
predicate_obligation(
|
|
|
|
|
predicate,
|
|
|
|
|
obligation.param_env,
|
|
|
|
|
obligation.cause.clone(),
|
|
|
|
|
)
|
|
|
|
|
}),
|
2020-02-22 11:44:18 +01:00
|
|
|
);
|
|
|
|
|
}
|
2021-01-07 11:20:28 -05:00
|
|
|
ty::PredicateKind::TypeWellFormedFromEnv(..) => {
|
2020-09-01 17:58:34 +02:00
|
|
|
// Nothing to elaborate
|
|
|
|
|
}
|
2022-11-02 15:10:05 +00:00
|
|
|
ty::PredicateKind::Ambiguous => {}
|
2023-02-10 13:43:29 +00:00
|
|
|
ty::PredicateKind::AliasEq(..) => {
|
|
|
|
|
// No
|
|
|
|
|
}
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-13 16:50:58 -05:00
|
|
|
impl<'tcx> Iterator for Elaborator<'tcx> {
|
2020-03-03 15:07:04 -08:00
|
|
|
type Item = PredicateObligation<'tcx>;
|
2020-02-22 11:44:18 +01:00
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
(self.stack.len(), None)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-03 15:07:04 -08:00
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
2020-02-22 11:44:18 +01:00
|
|
|
// Extract next item from top-most stack frame, if any.
|
2020-03-03 15:07:04 -08:00
|
|
|
if let Some(obligation) = self.stack.pop() {
|
|
|
|
|
self.elaborate(&obligation);
|
|
|
|
|
Some(obligation)
|
2020-02-22 11:44:18 +01:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-31 16:50:15 -07:00
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Supertrait iterator
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2020-06-21 12:26:17 +02:00
|
|
|
pub type Supertraits<'tcx> = FilterToTraits<Elaborator<'tcx>>;
|
2020-03-31 16:50:15 -07: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()
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-03 20:10:55 -03:00
|
|
|
/// A specialized variant of `elaborate_trait_refs` that only elaborates trait references that may
|
|
|
|
|
/// define the given associated type `assoc_name`. It uses the
|
|
|
|
|
/// `super_predicates_that_define_assoc_type` query to avoid enumerating super-predicates that
|
2022-11-16 20:34:16 +00:00
|
|
|
/// aren't related to `assoc_item`. This is used when resolving types like `Self::Item` or
|
2020-12-03 20:10:55 -03:00
|
|
|
/// `T::Item` and helps to avoid cycle errors (see e.g. #35237).
|
|
|
|
|
pub fn transitive_bounds_that_define_assoc_type<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
bounds: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
|
|
|
|
|
assoc_name: Ident,
|
2021-02-05 15:52:57 -03:00
|
|
|
) -> impl Iterator<Item = ty::PolyTraitRef<'tcx>> {
|
2020-12-03 20:10:55 -03:00
|
|
|
let mut stack: Vec<_> = bounds.collect();
|
2021-02-05 15:52:57 -03:00
|
|
|
let mut visited = FxIndexSet::default();
|
|
|
|
|
|
|
|
|
|
std::iter::from_fn(move || {
|
|
|
|
|
while let Some(trait_ref) = stack.pop() {
|
2022-07-25 20:24:13 +02:00
|
|
|
let anon_trait_ref = tcx.anonymize_bound_vars(trait_ref);
|
2021-02-05 15:52:57 -03:00
|
|
|
if visited.insert(anon_trait_ref) {
|
|
|
|
|
let super_predicates = tcx.super_predicates_that_define_assoc_type((
|
|
|
|
|
trait_ref.def_id(),
|
|
|
|
|
Some(assoc_name),
|
|
|
|
|
));
|
|
|
|
|
for (super_predicate, _) in super_predicates.predicates {
|
2021-04-28 18:10:44 -04:00
|
|
|
let subst_predicate = super_predicate.subst_supertrait(tcx, &trait_ref);
|
2021-12-12 12:34:46 +08:00
|
|
|
if let Some(binder) = subst_predicate.to_opt_poly_trait_pred() {
|
|
|
|
|
stack.push(binder.map_bound(|t| t.trait_ref));
|
2021-02-05 15:52:57 -03:00
|
|
|
}
|
2020-12-03 20:10:55 -03:00
|
|
|
}
|
2021-02-05 15:52:57 -03:00
|
|
|
|
|
|
|
|
return Some(trait_ref);
|
2020-12-03 20:10:55 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-05 15:52:57 -03:00
|
|
|
return None;
|
|
|
|
|
})
|
2020-12-03 20:10:55 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-31 16:50:15 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Other
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
/// A filter around an iterator of predicates that makes it yield up
|
|
|
|
|
/// just trait references.
|
2020-06-21 12:26:17 +02:00
|
|
|
pub struct FilterToTraits<I> {
|
2020-03-31 16:50:15 -07:00
|
|
|
base_iterator: I,
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 12:26:17 +02:00
|
|
|
impl<I> FilterToTraits<I> {
|
|
|
|
|
fn new(base: I) -> FilterToTraits<I> {
|
|
|
|
|
FilterToTraits { base_iterator: base }
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 12:26:17 +02:00
|
|
|
impl<'tcx, I: Iterator<Item = PredicateObligation<'tcx>>> Iterator for FilterToTraits<I> {
|
2020-03-31 16:50:15 -07:00
|
|
|
type Item = ty::PolyTraitRef<'tcx>;
|
|
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<ty::PolyTraitRef<'tcx>> {
|
2020-03-03 15:07:04 -08:00
|
|
|
while let Some(obligation) = self.base_iterator.next() {
|
2021-12-12 12:34:46 +08:00
|
|
|
if let Some(data) = obligation.predicate.to_opt_poly_trait_pred() {
|
|
|
|
|
return Some(data.map_bound(|t| t.trait_ref));
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
|
|
|
|
let (_, upper) = self.base_iterator.size_hint();
|
|
|
|
|
(0, upper)
|
|
|
|
|
}
|
|
|
|
|
}
|