Skip to content

Commit 9b28edb

Browse files
Make InstantiatedPredicates impl IntoIterator
1 parent 91fd862 commit 9b28edb

File tree

12 files changed

+74
-65
lines changed

12 files changed

+74
-65
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
107107
instantiated_predicates: ty::InstantiatedPredicates<'tcx>,
108108
locations: Locations,
109109
) {
110-
for (predicate, span) in instantiated_predicates
111-
.predicates
112-
.into_iter()
113-
.zip(instantiated_predicates.spans.into_iter())
114-
{
110+
for (predicate, span) in instantiated_predicates {
115111
debug!(?predicate);
116112
let category = ConstraintCategory::Predicate(span);
117113
let predicate = self.normalize_with_category(predicate, locations, category);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_trait_selection::traits::{
3232
};
3333

3434
use std::cell::LazyCell;
35-
use std::iter;
3635
use std::ops::{ControlFlow, Deref};
3736

3837
pub(super) struct WfCheckingCtxt<'a, 'tcx> {
@@ -1480,16 +1479,15 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14801479

14811480
debug!(?predicates.predicates);
14821481
assert_eq!(predicates.predicates.len(), predicates.spans.len());
1483-
let wf_obligations =
1484-
iter::zip(&predicates.predicates, &predicates.spans).flat_map(|(&p, &sp)| {
1485-
traits::wf::predicate_obligations(
1486-
infcx,
1487-
wfcx.param_env.without_const(),
1488-
wfcx.body_id,
1489-
p,
1490-
sp,
1491-
)
1492-
});
1482+
let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
1483+
traits::wf::predicate_obligations(
1484+
infcx,
1485+
wfcx.param_env.without_const(),
1486+
wfcx.body_id,
1487+
p,
1488+
sp,
1489+
)
1490+
});
14931491

14941492
let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect();
14951493
wfcx.register_obligations(obligations);

compiler/rustc_hir_typeck/src/callee.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
375375
if self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses) {
376376
let predicates = self.tcx.predicates_of(def_id);
377377
let predicates = predicates.instantiate(self.tcx, subst);
378-
for (predicate, predicate_span) in
379-
predicates.predicates.iter().zip(&predicates.spans)
380-
{
378+
for (predicate, predicate_span) in predicates {
381379
let obligation = Obligation::new(
382380
self.tcx,
383381
ObligationCause::dummy_with_span(callee_expr.span),
384382
self.param_env,
385-
*predicate,
383+
predicate,
386384
);
387385
let result = self.evaluate_obligation(&obligation);
388386
self.tcx
@@ -391,7 +389,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
391389
callee_expr.span,
392390
&format!("evaluate({:?}) = {:?}", predicate, result),
393391
)
394-
.span_label(*predicate_span, "predicate")
392+
.span_label(predicate_span, "predicate")
395393
.emit();
396394
}
397395
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2140,8 +2140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
21402140
// FIXME(compiler-errors): This could be problematic if something has two
21412141
// fn-like predicates with different args, but callable types really never
21422142
// do that, so it's OK.
2143-
for (predicate, span) in
2144-
std::iter::zip(instantiated.predicates, instantiated.spans)
2143+
for (predicate, span) in instantiated
21452144
{
21462145
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder()
21472146
&& pred.self_ty().peel_refs() == callee_ty

compiler/rustc_hir_typeck/src/method/confirm.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use rustc_middle::ty::{InternalSubsts, UserSubsts, UserType};
1919
use rustc_span::{Span, DUMMY_SP};
2020
use rustc_trait_selection::traits;
2121

22-
use std::iter;
2322
use std::ops::Deref;
2423

2524
struct ConfirmContext<'a, 'tcx> {
@@ -101,7 +100,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
101100
let filler_substs = rcvr_substs
102101
.extend_to(self.tcx, pick.item.def_id, |def, _| self.tcx.mk_param_from_def(def));
103102
let illegal_sized_bound = self.predicates_require_illegal_sized_bound(
104-
&self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs),
103+
self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs),
105104
);
106105

107106
// Unify the (adjusted) self type with what the method expects.
@@ -565,7 +564,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
565564

566565
fn predicates_require_illegal_sized_bound(
567566
&self,
568-
predicates: &ty::InstantiatedPredicates<'tcx>,
567+
predicates: ty::InstantiatedPredicates<'tcx>,
569568
) -> Option<Span> {
570569
let sized_def_id = self.tcx.lang_items().sized_trait()?;
571570

@@ -575,10 +574,11 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
575574
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred))
576575
if trait_pred.def_id() == sized_def_id =>
577576
{
578-
let span = iter::zip(&predicates.predicates, &predicates.spans)
577+
let span = predicates
578+
.iter()
579579
.find_map(
580580
|(p, span)| {
581-
if *p == obligation.predicate { Some(*span) } else { None }
581+
if p == obligation.predicate { Some(span) } else { None }
582582
},
583583
)
584584
.unwrap_or(rustc_span::DUMMY_SP);

compiler/rustc_middle/src/ty/mod.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,33 @@ impl<'tcx> InstantiatedPredicates<'tcx> {
12521252
pub fn is_empty(&self) -> bool {
12531253
self.predicates.is_empty()
12541254
}
1255+
1256+
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
1257+
(&self).into_iter()
1258+
}
1259+
}
1260+
1261+
impl<'tcx> IntoIterator for InstantiatedPredicates<'tcx> {
1262+
type Item = (Predicate<'tcx>, Span);
1263+
1264+
type IntoIter = std::iter::Zip<std::vec::IntoIter<Predicate<'tcx>>, std::vec::IntoIter<Span>>;
1265+
1266+
fn into_iter(self) -> Self::IntoIter {
1267+
std::iter::zip(self.predicates, self.spans)
1268+
}
1269+
}
1270+
1271+
impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
1272+
type Item = (Predicate<'tcx>, Span);
1273+
1274+
type IntoIter = std::iter::Zip<
1275+
std::iter::Copied<std::slice::Iter<'a, Predicate<'tcx>>>,
1276+
std::iter::Copied<std::slice::Iter<'a, Span>>,
1277+
>;
1278+
1279+
fn into_iter(self) -> Self::IntoIter {
1280+
std::iter::zip(self.predicates.iter().copied(), self.spans.iter().copied())
1281+
}
12551282
}
12561283

12571284
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable, Lift)]

compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ pub fn recompute_applicable_impls<'tcx>(
8282

8383
let predicates =
8484
tcx.predicates_of(obligation.cause.body_id.owner.to_def_id()).instantiate_identity(tcx);
85-
for obligation in
86-
elaborate_predicates_with_span(tcx, std::iter::zip(predicates.predicates, predicates.spans))
87-
{
85+
for obligation in elaborate_predicates_with_span(tcx, predicates.into_iter()) {
8886
let kind = obligation.predicate.kind();
8987
if let ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) = kind.skip_binder()
9088
&& param_env_candidate_may_apply(kind.rebind(trait_pred))

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
20702070

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

compiler/rustc_trait_selection/src/traits/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,12 @@ pub fn predicates_for_generics<'tcx>(
115115
param_env: ty::ParamEnv<'tcx>,
116116
generic_bounds: ty::InstantiatedPredicates<'tcx>,
117117
) -> impl Iterator<Item = PredicateObligation<'tcx>> {
118-
std::iter::zip(generic_bounds.predicates, generic_bounds.spans).enumerate().map(
119-
move |(idx, (predicate, span))| Obligation {
120-
cause: cause(idx, span),
121-
recursion_depth: 0,
122-
param_env,
123-
predicate,
124-
},
125-
)
118+
generic_bounds.into_iter().enumerate().map(move |(idx, (predicate, span))| Obligation {
119+
cause: cause(idx, span),
120+
recursion_depth: 0,
121+
param_env,
122+
predicate,
123+
})
126124
}
127125

128126
/// Determines whether the type `ty` is known to meet `bound` and

compiler/rustc_trait_selection/src/traits/project.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -2259,25 +2259,23 @@ fn confirm_impl_trait_in_trait_candidate<'tcx>(
22592259
tcx.predicates_of(impl_fn_def_id).instantiate(tcx, impl_fn_substs),
22602260
&mut obligations,
22612261
);
2262-
obligations.extend(std::iter::zip(predicates.predicates, predicates.spans).map(
2263-
|(pred, span)| {
2264-
Obligation::with_depth(
2265-
tcx,
2266-
ObligationCause::new(
2267-
obligation.cause.span,
2268-
obligation.cause.body_id,
2269-
if span.is_dummy() {
2270-
super::ItemObligation(impl_fn_def_id)
2271-
} else {
2272-
super::BindingObligation(impl_fn_def_id, span)
2273-
},
2274-
),
2275-
obligation.recursion_depth + 1,
2276-
obligation.param_env,
2277-
pred,
2278-
)
2279-
},
2280-
));
2262+
obligations.extend(predicates.into_iter().map(|(pred, span)| {
2263+
Obligation::with_depth(
2264+
tcx,
2265+
ObligationCause::new(
2266+
obligation.cause.span,
2267+
obligation.cause.body_id,
2268+
if span.is_dummy() {
2269+
super::ItemObligation(impl_fn_def_id)
2270+
} else {
2271+
super::BindingObligation(impl_fn_def_id, span)
2272+
},
2273+
),
2274+
obligation.recursion_depth + 1,
2275+
obligation.param_env,
2276+
pred,
2277+
)
2278+
}));
22812279

22822280
let ty = normalize_with_depth_to(
22832281
selcx,

compiler/rustc_trait_selection/src/traits/wf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ impl<'tcx> WfPredicates<'tcx> {
736736
trace!("{:#?}", predicates);
737737
debug_assert_eq!(predicates.predicates.len(), origins.len());
738738

739-
iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
739+
iter::zip(predicates, origins.into_iter().rev())
740740
.map(|((mut pred, span), origin_def_id)| {
741741
let code = if span.is_dummy() {
742742
traits::ItemObligation(origin_def_id)

compiler/rustc_traits/src/type_op.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_trait_selection::traits::query::type_op::subtype::Subtype;
1717
use rustc_trait_selection::traits::query::{Fallible, NoSolution};
1818
use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, ObligationCtxt};
1919
use std::fmt;
20-
use std::iter::zip;
2120

2221
pub(crate) fn provide(p: &mut Providers) {
2322
*p = Providers {
@@ -108,9 +107,7 @@ fn relate_mir_and_user_substs<'tcx>(
108107
let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs);
109108

110109
debug!(?instantiated_predicates);
111-
for (instantiated_predicate, predicate_span) in
112-
zip(instantiated_predicates.predicates, instantiated_predicates.spans)
113-
{
110+
for (instantiated_predicate, predicate_span) in instantiated_predicates {
114111
let span = if span == DUMMY_SP { predicate_span } else { span };
115112
let cause = ObligationCause::new(
116113
span,

0 commit comments

Comments
 (0)