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};
|
2024-05-09 20:05:59 -04:00
|
|
|
use crate::traits::{self, Obligation, ObligationCauseCode, PredicateObligation};
|
2023-10-18 03:04:00 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2023-07-05 20:13:26 +01:00
|
|
|
use rustc_middle::ty::{self, ToPredicate, Ty, 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
|
|
|
}
|
|
|
|
|
|
2023-07-19 09:44:40 +00:00
|
|
|
/// Adds a predicate to the set.
|
|
|
|
|
///
|
|
|
|
|
/// Returns whether the predicate was newly inserted. That is:
|
|
|
|
|
/// - If the set did not previously contain this predicate, `true` is returned.
|
|
|
|
|
/// - If the set already contained this predicate, `false` is returned,
|
|
|
|
|
/// and the set is not modified: original predicate is not replaced,
|
|
|
|
|
/// and the predicate passed as argument is dropped.
|
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`.
|
2023-04-06 23:11:19 +00:00
|
|
|
pub struct Elaborator<'tcx, O> {
|
|
|
|
|
stack: Vec<O>,
|
2020-02-22 11:44:18 +01:00
|
|
|
visited: PredicateSet<'tcx>,
|
2023-10-18 02:45:16 +00:00
|
|
|
mode: Filter,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum Filter {
|
|
|
|
|
All,
|
|
|
|
|
OnlySelf,
|
|
|
|
|
OnlySelfThatDefines(Ident),
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
/// Describes how to elaborate an obligation into a sub-obligation.
|
|
|
|
|
///
|
|
|
|
|
/// For [`Obligation`], a sub-obligation is combined with the current obligation's
|
|
|
|
|
/// param-env and cause code. For [`ty::Predicate`], none of this is needed, since
|
|
|
|
|
/// there is no param-env or cause code to copy over.
|
|
|
|
|
pub trait Elaboratable<'tcx> {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx>;
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
// Makes a new `Self` but with a different clause that comes from elaboration.
|
|
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self;
|
2023-04-06 23:11:19 +00:00
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
// Makes a new `Self` but with a different clause and a different cause
|
|
|
|
|
// code (if `Self` has one, such as [`PredicateObligation`]).
|
2023-04-06 23:11:19 +00:00
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-04-06 23:11:19 +00:00
|
|
|
span: Span,
|
|
|
|
|
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
index: usize,
|
|
|
|
|
) -> Self;
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
impl<'tcx> Elaboratable<'tcx> for PredicateObligation<'tcx> {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx> {
|
|
|
|
|
self.predicate
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
|
2023-04-06 23:11:19 +00:00
|
|
|
Obligation {
|
|
|
|
|
cause: self.cause.clone(),
|
|
|
|
|
param_env: self.param_env,
|
|
|
|
|
recursion_depth: 0,
|
2023-06-29 00:46:41 +00:00
|
|
|
predicate: clause.as_predicate(),
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-04-06 23:11:19 +00:00
|
|
|
span: Span,
|
|
|
|
|
parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
index: usize,
|
|
|
|
|
) -> Self {
|
|
|
|
|
let cause = self.cause.clone().derived_cause(parent_trait_pred, |derived| {
|
2024-05-09 20:05:59 -04:00
|
|
|
ObligationCauseCode::ImplDerivedObligation(Box::new(
|
|
|
|
|
traits::ImplDerivedObligationCause {
|
|
|
|
|
derived,
|
|
|
|
|
impl_or_alias_def_id: parent_trait_pred.def_id(),
|
|
|
|
|
impl_def_predicate_index: Some(index),
|
|
|
|
|
span,
|
|
|
|
|
},
|
|
|
|
|
))
|
2023-04-06 23:11:19 +00:00
|
|
|
});
|
2023-06-29 00:46:41 +00:00
|
|
|
Obligation {
|
|
|
|
|
cause,
|
|
|
|
|
param_env: self.param_env,
|
|
|
|
|
recursion_depth: 0,
|
|
|
|
|
predicate: clause.as_predicate(),
|
|
|
|
|
}
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
impl<'tcx> Elaboratable<'tcx> for ty::Predicate<'tcx> {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx> {
|
|
|
|
|
*self
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
|
|
|
|
|
clause.as_predicate()
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-04-06 23:11:19 +00:00
|
|
|
_span: Span,
|
|
|
|
|
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
_index: usize,
|
|
|
|
|
) -> Self {
|
2023-06-29 00:46:41 +00:00
|
|
|
clause.as_predicate()
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> Elaboratable<'tcx> for (ty::Predicate<'tcx>, Span) {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx> {
|
|
|
|
|
self.0
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
|
|
|
|
|
(clause.as_predicate(), self.1)
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-04-06 23:11:19 +00:00
|
|
|
_span: Span,
|
|
|
|
|
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
_index: usize,
|
|
|
|
|
) -> Self {
|
2023-06-29 00:46:41 +00:00
|
|
|
(clause.as_predicate(), self.1)
|
2023-04-06 23:11:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 20:46:46 +00:00
|
|
|
impl<'tcx> Elaboratable<'tcx> for (ty::Clause<'tcx>, Span) {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx> {
|
|
|
|
|
self.0.as_predicate()
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
|
|
|
|
|
(clause, self.1)
|
2023-06-19 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-06-19 20:46:46 +00:00
|
|
|
_span: Span,
|
|
|
|
|
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
_index: usize,
|
|
|
|
|
) -> Self {
|
2023-06-29 00:46:41 +00:00
|
|
|
(clause, self.1)
|
2023-06-19 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-16 06:27:41 +00:00
|
|
|
impl<'tcx> Elaboratable<'tcx> for ty::Clause<'tcx> {
|
|
|
|
|
fn predicate(&self) -> ty::Predicate<'tcx> {
|
|
|
|
|
self.as_predicate()
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-29 00:46:41 +00:00
|
|
|
fn child(&self, clause: ty::Clause<'tcx>) -> Self {
|
|
|
|
|
clause
|
2023-06-16 06:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn child_with_derived_cause(
|
|
|
|
|
&self,
|
2023-06-29 00:46:41 +00:00
|
|
|
clause: ty::Clause<'tcx>,
|
2023-06-16 06:27:41 +00:00
|
|
|
_span: Span,
|
|
|
|
|
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
|
|
|
|
|
_index: usize,
|
|
|
|
|
) -> Self {
|
2023-06-29 00:46:41 +00:00
|
|
|
clause
|
2023-06-16 06:27:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
pub fn elaborate<'tcx, O: Elaboratable<'tcx>>(
|
2020-03-03 15:07:04 -08:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-04-06 23:11:19 +00:00
|
|
|
obligations: impl IntoIterator<Item = O>,
|
|
|
|
|
) -> Elaborator<'tcx, O> {
|
2023-02-02 21:22:02 +00:00
|
|
|
let mut elaborator =
|
2023-10-18 02:45:16 +00:00
|
|
|
Elaborator { stack: Vec::new(), visited: PredicateSet::new(tcx), mode: Filter::All };
|
2023-02-24 11:22:24 +01:00
|
|
|
elaborator.extend_deduped(obligations);
|
|
|
|
|
elaborator
|
2020-03-03 15:07:04 -08:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
|
|
|
|
|
fn extend_deduped(&mut self, obligations: impl IntoIterator<Item = O>) {
|
2023-02-24 11:22:24 +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;
|
2023-04-06 23:11:19 +00:00
|
|
|
self.stack.extend(obligations.into_iter().filter(|o| self.visited.insert(o.predicate())));
|
2023-02-24 11:22:24 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-02 21:22:02 +00:00
|
|
|
/// Filter to only the supertraits of trait predicates, i.e. only the predicates
|
|
|
|
|
/// that have `Self` as their self type, instead of all implied predicates.
|
|
|
|
|
pub fn filter_only_self(mut self) -> Self {
|
2023-10-18 02:45:16 +00:00
|
|
|
self.mode = Filter::OnlySelf;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Filter to only the supertraits of trait predicates that define the assoc_ty.
|
|
|
|
|
pub fn filter_only_self_that_defines(mut self, assoc_ty: Ident) -> Self {
|
|
|
|
|
self.mode = Filter::OnlySelfThatDefines(assoc_ty);
|
2023-02-02 21:22:02 +00:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
fn elaborate(&mut self, elaboratable: &O) {
|
2020-02-22 11:44:18 +01:00
|
|
|
let tcx = self.visited.tcx;
|
2020-06-19 19:19:21 +02:00
|
|
|
|
2023-12-07 17:52:51 +01:00
|
|
|
// We only elaborate clauses.
|
|
|
|
|
let Some(clause) = elaboratable.predicate().as_clause() else {
|
|
|
|
|
return;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let bound_clause = clause.kind();
|
|
|
|
|
match bound_clause.skip_binder() {
|
|
|
|
|
ty::ClauseKind::Trait(data) => {
|
2023-04-25 06:37:24 +00:00
|
|
|
// Negative trait bounds do not imply any supertrait bounds
|
2024-03-21 15:46:40 -04:00
|
|
|
if data.polarity != ty::PredicatePolarity::Positive {
|
2023-04-25 06:37:24 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2023-02-02 21:22:02 +00:00
|
|
|
// Get predicates implied by the trait, or only super predicates if we only care about self predicates.
|
2023-10-18 02:45:16 +00:00
|
|
|
let predicates = match self.mode {
|
|
|
|
|
Filter::All => tcx.implied_predicates_of(data.def_id()),
|
|
|
|
|
Filter::OnlySelf => tcx.super_predicates_of(data.def_id()),
|
|
|
|
|
Filter::OnlySelfThatDefines(ident) => {
|
|
|
|
|
tcx.super_predicates_that_define_assoc_item((data.def_id(), ident))
|
|
|
|
|
}
|
2023-02-02 21:22:02 +00:00
|
|
|
};
|
2020-02-22 11:44:18 +01:00
|
|
|
|
2023-01-03 19:54:11 -08:00
|
|
|
let obligations =
|
2023-07-29 08:20:25 +00:00
|
|
|
predicates.predicates.iter().enumerate().map(|(index, &(clause, span))| {
|
2023-04-06 23:11:19 +00:00
|
|
|
elaboratable.child_with_derived_cause(
|
2024-02-12 15:39:32 +09:00
|
|
|
clause
|
|
|
|
|
.instantiate_supertrait(tcx, &bound_clause.rebind(data.trait_ref)),
|
2023-04-06 23:11:19 +00:00
|
|
|
span,
|
2023-12-07 17:52:51 +01:00
|
|
|
bound_clause.rebind(data),
|
2023-04-06 23:11:19 +00:00
|
|
|
index,
|
2023-01-03 19:54:11 -08:00
|
|
|
)
|
|
|
|
|
});
|
2021-12-19 22:01:48 -05:00
|
|
|
debug!(?data, ?obligations, "super_predicates");
|
2023-02-24 11:22:24 +01:00
|
|
|
self.extend_deduped(obligations);
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
2023-12-07 17:52:51 +01:00
|
|
|
ty::ClauseKind::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
|
2023-11-13 14:00:05 +00:00
|
|
|
if r_min.is_bound() {
|
2020-02-22 11:44:18 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut components = smallvec![];
|
2021-10-08 22:55:06 -04:00
|
|
|
push_outlives_components(tcx, ty_max, &mut components);
|
2023-02-24 11:22:24 +01:00
|
|
|
self.extend_deduped(
|
2020-02-22 11:44:18 +01:00
|
|
|
components
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter_map(|component| match component {
|
|
|
|
|
Component::Region(r) => {
|
2023-11-13 14:00:05 +00:00
|
|
|
if r.is_bound() {
|
2020-02-22 11:44:18 +01:00
|
|
|
None
|
|
|
|
|
} else {
|
2023-06-29 00:46:41 +00:00
|
|
|
Some(ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
|
|
|
|
|
r, r_min,
|
2020-02-22 11:44:18 +01:00
|
|
|
)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Component::Param(p) => {
|
2023-07-05 20:13:26 +01:00
|
|
|
let ty = Ty::new_param(tcx, p.index, p.name);
|
2023-06-29 00:46:41 +00:00
|
|
|
Some(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty, r_min)))
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
|
2023-11-17 03:00:38 +00:00
|
|
|
Component::Placeholder(p) => {
|
|
|
|
|
let ty = Ty::new_placeholder(tcx, p);
|
|
|
|
|
Some(ty::ClauseKind::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`.
|
2023-06-29 00:46:41 +00:00
|
|
|
Some(ty::ClauseKind::TypeOutlives(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
|
|
|
|
|
}
|
|
|
|
|
})
|
2023-06-29 00:46:41 +00:00
|
|
|
.map(|clause| {
|
2023-12-07 17:52:51 +01:00
|
|
|
elaboratable.child(bound_clause.rebind(clause).to_predicate(tcx))
|
2023-06-29 00:46:41 +00:00
|
|
|
}),
|
2020-02-22 11:44:18 +01:00
|
|
|
);
|
|
|
|
|
}
|
2023-12-07 17:52:51 +01:00
|
|
|
ty::ClauseKind::RegionOutlives(..) => {
|
|
|
|
|
// Nothing to elaborate from `'a: 'b`.
|
|
|
|
|
}
|
|
|
|
|
ty::ClauseKind::WellFormed(..) => {
|
|
|
|
|
// Currently, we do not elaborate WF predicates,
|
|
|
|
|
// although we easily could.
|
|
|
|
|
}
|
|
|
|
|
ty::ClauseKind::Projection(..) => {
|
|
|
|
|
// Nothing to elaborate in a projection predicate.
|
|
|
|
|
}
|
|
|
|
|
ty::ClauseKind::ConstEvaluatable(..) => {
|
|
|
|
|
// Currently, we do not elaborate const-evaluatable
|
|
|
|
|
// predicates.
|
2023-02-10 13:43:29 +00:00
|
|
|
}
|
2023-12-07 17:52:51 +01:00
|
|
|
ty::ClauseKind::ConstArgHasType(..) => {
|
2023-02-16 11:55:58 +00:00
|
|
|
// Nothing to elaborate
|
|
|
|
|
}
|
2020-02-22 11:44:18 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:11:19 +00:00
|
|
|
impl<'tcx, O: Elaboratable<'tcx>> Iterator for Elaborator<'tcx, O> {
|
|
|
|
|
type Item = O;
|
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
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
pub fn supertraits<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
trait_ref: ty::PolyTraitRef<'tcx>,
|
2023-10-18 03:04:00 +00:00
|
|
|
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
|
2023-02-02 21:22:02 +00:00
|
|
|
elaborate(tcx, [trait_ref.to_predicate(tcx)]).filter_only_self().filter_to_traits()
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn transitive_bounds<'tcx>(
|
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-03-26 20:33:54 +00:00
|
|
|
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
|
2023-10-18 02:45:16 +00:00
|
|
|
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
|
2023-02-02 21:22:02 +00:00
|
|
|
elaborate(tcx, trait_refs.map(|trait_ref| trait_ref.to_predicate(tcx)))
|
|
|
|
|
.filter_only_self()
|
|
|
|
|
.filter_to_traits()
|
2020-03-31 16:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
2023-04-06 23:24:03 +00:00
|
|
|
/// A specialized variant of `elaborate` that only elaborates trait references that may
|
2023-05-03 20:13:32 +00:00
|
|
|
/// define the given associated item with the name `assoc_name`. It uses the
|
|
|
|
|
/// `super_predicates_that_define_assoc_item` 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).
|
2023-05-03 20:13:32 +00:00
|
|
|
pub fn transitive_bounds_that_define_assoc_item<'tcx>(
|
2020-12-03 20:10:55 -03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-10-18 02:45:16 +00:00
|
|
|
trait_refs: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
|
2020-12-03 20:10:55 -03:00
|
|
|
assoc_name: Ident,
|
2023-10-18 02:45:16 +00:00
|
|
|
) -> FilterToTraits<Elaborator<'tcx, ty::Predicate<'tcx>>> {
|
|
|
|
|
elaborate(tcx, trait_refs.map(|trait_ref| trait_ref.to_predicate(tcx)))
|
|
|
|
|
.filter_only_self_that_defines(assoc_name)
|
|
|
|
|
.filter_to_traits()
|
2020-12-03 20:10:55 -03:00
|
|
|
}
|
|
|
|
|
|
2020-03-31 16:50:15 -07:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Other
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2023-02-02 21:22:02 +00:00
|
|
|
impl<'tcx> Elaborator<'tcx, ty::Predicate<'tcx>> {
|
|
|
|
|
fn filter_to_traits(self) -> FilterToTraits<Self> {
|
|
|
|
|
FilterToTraits { base_iterator: self }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-31 16:50:15 -07:00
|
|
|
/// 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,
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-26 20:33:54 +00:00
|
|
|
impl<'tcx, I: Iterator<Item = ty::Predicate<'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>> {
|
2023-03-26 20:33:54 +00:00
|
|
|
while let Some(pred) = self.base_iterator.next() {
|
|
|
|
|
if let Some(data) = pred.to_opt_poly_trait_pred() {
|
2021-12-12 12:34:46 +08:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|