1
Fork 0

Auto merge of #106395 - compiler-errors:rework-predicates, r=eholk

Rework some `predicates_of`/`{Generic,Instantiated}Predicates` code

1. Make `instantiate_own` return an iterator, since it's a bit more efficient and easier to work with
2. Remove `bound_{explicit,}_predicates_of` -- these `bound_` methods in particular were a bit awkward to work with since `ty::GenericPredicates` *already* acts kinda like an `EarlyBinder` with its own `instantiate_*` methods, and had only a few call sites anyways.
3. Implement `IntoIterator` for `InstantiatedPredicates`, since it's *very* commonly being `zip`'d together.
This commit is contained in:
bors 2023-01-16 05:55:59 +00:00
commit d12412c90f
22 changed files with 155 additions and 156 deletions

View file

@ -82,9 +82,7 @@ pub fn recompute_applicable_impls<'tcx>(
let predicates =
tcx.predicates_of(obligation.cause.body_id.owner.to_def_id()).instantiate_identity(tcx);
for obligation in
elaborate_predicates_with_span(tcx, std::iter::zip(predicates.predicates, predicates.spans))
{
for obligation in elaborate_predicates_with_span(tcx, predicates.into_iter()) {
let kind = obligation.predicate.kind();
if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = kind.skip_binder()
&& param_env_candidate_may_apply(kind.rebind(trait_pred))

View file

@ -2070,7 +2070,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// Find another predicate whose self-type is equal to the expected self type,
// but whose substs don't match.
let other_pred = std::iter::zip(&predicates.predicates, &predicates.spans)
let other_pred = predicates.into_iter()
.enumerate()
.find(|(other_idx, (pred, _))| match pred.kind().skip_binder() {
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred))
@ -2095,7 +2095,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
// If we found one, then it's very likely the cause of the error.
if let Some((_, (_, other_pred_span))) = other_pred {
err.span_note(
*other_pred_span,
other_pred_span,
"closure inferred to have a different signature due to this bound",
);
}

View file

@ -115,14 +115,12 @@ pub fn predicates_for_generics<'tcx>(
param_env: ty::ParamEnv<'tcx>,
generic_bounds: ty::InstantiatedPredicates<'tcx>,
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
std::iter::zip(generic_bounds.predicates, generic_bounds.spans).enumerate().map(
move |(idx, (predicate, span))| Obligation {
cause: cause(idx, span),
recursion_depth: 0,
param_env,
predicate,
},
)
generic_bounds.into_iter().enumerate().map(move |(idx, (predicate, span))| Obligation {
cause: cause(idx, span),
recursion_depth: 0,
param_env,
predicate,
})
}
/// Determines whether the type `ty` is known to meet `bound` and

View file

@ -2259,25 +2259,23 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
tcx.predicates_of(impl_fn_def_id).instantiate(tcx, impl_fn_substs),
&mut obligations,
);
obligations.extend(std::iter::zip(predicates.predicates, predicates.spans).map(
|(pred, span)| {
Obligation::with_depth(
tcx,
ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,
if span.is_dummy() {
super::ItemObligation(impl_fn_def_id)
} else {
super::BindingObligation(impl_fn_def_id, span)
},
),
obligation.recursion_depth + 1,
obligation.param_env,
pred,
)
},
));
obligations.extend(predicates.into_iter().map(|(pred, span)| {
Obligation::with_depth(
tcx,
ObligationCause::new(
obligation.cause.span,
obligation.cause.body_id,
if span.is_dummy() {
super::ItemObligation(impl_fn_def_id)
} else {
super::BindingObligation(impl_fn_def_id, span)
},
),
obligation.recursion_depth + 1,
obligation.param_env,
pred,
)
}));
let ty = normalize_with_depth_to(
selcx,
@ -2303,10 +2301,10 @@ fn assoc_ty_own_obligations<'cx, 'tcx>(
nested: &mut Vec<PredicateObligation<'tcx>>,
) {
let tcx = selcx.tcx();
let own = tcx
let predicates = tcx
.predicates_of(obligation.predicate.def_id)
.instantiate_own(tcx, obligation.predicate.substs);
for (predicate, span) in std::iter::zip(own.predicates, own.spans) {
for (predicate, span) in predicates {
let normalized = normalize_with_depth_to(
selcx,
obligation.param_env,

View file

@ -185,9 +185,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
})?);
if let ty::Alias(ty::Projection, ..) = placeholder_self_ty.kind() {
let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs).predicates;
debug!(?predicates, "projection predicates");
for predicate in predicates {
let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs);
for (predicate, _) in predicates {
let normalized = normalize_with_depth_to(
self,
obligation.param_env,

View file

@ -2558,12 +2558,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// obligation will normalize to `<$0 as Iterator>::Item = $1` and
// `$1: Copy`, so we must ensure the obligations are emitted in
// that order.
let predicates = tcx.bound_predicates_of(def_id);
debug!(?predicates);
assert_eq!(predicates.0.parent, None);
let mut obligations = Vec::with_capacity(predicates.0.predicates.len());
for (predicate, span) in predicates.0.predicates {
let span = *span;
let predicates = tcx.predicates_of(def_id);
assert_eq!(predicates.parent, None);
let predicates = predicates.instantiate_own(tcx, substs);
let mut obligations = Vec::with_capacity(predicates.len());
for (predicate, span) in predicates {
let cause = cause.clone().derived_cause(parent_trait_pred, |derived| {
ImplDerivedObligation(Box::new(ImplDerivedObligationCause {
derived,
@ -2576,7 +2575,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
param_env,
cause.clone(),
recursion_depth,
predicates.rebind(*predicate).subst(tcx, substs),
predicate,
&mut obligations,
);
obligations.push(Obligation { cause, recursion_depth, param_env, predicate });

View file

@ -261,7 +261,10 @@ fn vtable_entries<'tcx>(
// Note that this method could then never be called, so we
// do not want to try and codegen it, in that case (see #23435).
let predicates = tcx.predicates_of(def_id).instantiate_own(tcx, substs);
if impossible_predicates(tcx, predicates.predicates) {
if impossible_predicates(
tcx,
predicates.map(|(predicate, _)| predicate).collect(),
) {
debug!("vtable_entries: predicates do not hold");
return VtblEntry::Vacant;
}

View file

@ -736,7 +736,7 @@ impl<'tcx> WfPredicates<'tcx> {
trace!("{:#?}", predicates);
debug_assert_eq!(predicates.predicates.len(), origins.len());
iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
iter::zip(predicates, origins.into_iter().rev())
.map(|((mut pred, span), origin_def_id)| {
let code = if span.is_dummy() {
traits::ItemObligation(origin_def_id)