Skip to content

Commit d0c877b

Browse files
Revert PR "Rework some predicates_of/{Generic,Instantiated}Predicates code"
Revert "Remove bound_{explicit,}_item_bounds" This reverts commit 90df86f. Revert "drive-by: assert when iterating through InstantiatedPredicates" This reverts commit e1533a2. Revert "Make InstantiatedPredicates impl IntoIterator" This reverts commit 9b28edb. Revert "instantiate_own doesn't need to return a pair of vectors" This reverts commit 91fd862.
1 parent 4817259 commit d0c877b

File tree

22 files changed

+156
-155
lines changed

22 files changed

+156
-155
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -673,34 +673,40 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
673673
let tcx = self.infcx.tcx;
674674

675675
// Find out if the predicates show that the type is a Fn or FnMut
676-
let find_fn_kind_from_did = |(pred, _): (ty::Predicate<'tcx>, _)| {
677-
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred.kind().skip_binder()
678-
&& pred.self_ty() == ty
679-
{
680-
if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
681-
return Some(hir::Mutability::Not);
682-
} else if Some(pred.def_id()) == tcx.lang_items().fn_mut_trait() {
683-
return Some(hir::Mutability::Mut);
676+
let find_fn_kind_from_did = |predicates: ty::EarlyBinder<
677+
&[(ty::Predicate<'tcx>, Span)],
678+
>,
679+
substs| {
680+
predicates.0.iter().find_map(|(pred, _)| {
681+
let pred = if let Some(substs) = substs {
682+
predicates.rebind(*pred).subst(tcx, substs).kind().skip_binder()
683+
} else {
684+
pred.kind().skip_binder()
685+
};
686+
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = pred && pred.self_ty() == ty {
687+
if Some(pred.def_id()) == tcx.lang_items().fn_trait() {
688+
return Some(hir::Mutability::Not);
689+
} else if Some(pred.def_id()) == tcx.lang_items().fn_mut_trait() {
690+
return Some(hir::Mutability::Mut);
691+
}
684692
}
685-
}
686-
None
693+
None
694+
})
687695
};
688696

689697
// If the type is opaque/param/closure, and it is Fn or FnMut, let's suggest (mutably)
690698
// borrowing the type, since `&mut F: FnMut` iff `F: FnMut` and similarly for `Fn`.
691699
// These types seem reasonably opaque enough that they could be substituted with their
692700
// borrowed variants in a function body when we see a move error.
693-
let borrow_level = match *ty.kind() {
694-
ty::Param(_) => tcx
695-
.explicit_predicates_of(self.mir_def_id().to_def_id())
696-
.predicates
697-
.iter()
698-
.copied()
699-
.find_map(find_fn_kind_from_did),
700-
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => tcx
701-
.bound_explicit_item_bounds(def_id)
702-
.subst_iter_copied(tcx, substs)
703-
.find_map(find_fn_kind_from_did),
701+
let borrow_level = match ty.kind() {
702+
ty::Param(_) => find_fn_kind_from_did(
703+
tcx.bound_explicit_predicates_of(self.mir_def_id().to_def_id())
704+
.map_bound(|p| p.predicates),
705+
None,
706+
),
707+
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => {
708+
find_fn_kind_from_did(tcx.bound_explicit_item_bounds(*def_id), Some(*substs))
709+
}
704710
ty::Closure(_, substs) => match substs.as_closure().kind() {
705711
ty::ClosureKind::Fn => Some(hir::Mutability::Not),
706712
ty::ClosureKind::FnMut => Some(hir::Mutability::Mut),

compiler/rustc_borrowck/src/type_check/canonical.rs

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

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,9 @@ fn compare_method_predicate_entailment<'tcx>(
209209
//
210210
// We then register the obligations from the impl_m and check to see
211211
// if all constraints hold.
212-
hybrid_preds.predicates.extend(
213-
trait_m_predicates
214-
.instantiate_own(tcx, trait_to_placeholder_substs)
215-
.map(|(predicate, _)| predicate),
216-
);
212+
hybrid_preds
213+
.predicates
214+
.extend(trait_m_predicates.instantiate_own(tcx, trait_to_placeholder_substs).predicates);
217215

218216
// Construct trait parameter environment and then shift it into the placeholder viewpoint.
219217
// The key step here is to update the caller_bounds's predicates to be
@@ -232,7 +230,7 @@ fn compare_method_predicate_entailment<'tcx>(
232230
debug!("compare_impl_method: caller_bounds={:?}", param_env.caller_bounds());
233231

234232
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_placeholder_substs);
235-
for (predicate, span) in impl_m_own_bounds {
233+
for (predicate, span) in iter::zip(impl_m_own_bounds.predicates, impl_m_own_bounds.spans) {
236234
let normalize_cause = traits::ObligationCause::misc(span, impl_m_hir_id);
237235
let predicate = ocx.normalize(&normalize_cause, param_env, predicate);
238236

@@ -1830,7 +1828,8 @@ fn compare_type_predicate_entailment<'tcx>(
18301828
check_region_bounds_on_impl_item(tcx, impl_ty, trait_ty, false)?;
18311829

18321830
let impl_ty_own_bounds = impl_ty_predicates.instantiate_own(tcx, impl_substs);
1833-
if impl_ty_own_bounds.len() == 0 {
1831+
1832+
if impl_ty_own_bounds.is_empty() {
18341833
// Nothing to check.
18351834
return Ok(());
18361835
}
@@ -1845,11 +1844,9 @@ fn compare_type_predicate_entailment<'tcx>(
18451844
// associated type in the trait are assumed.
18461845
let impl_predicates = tcx.predicates_of(impl_ty_predicates.parent.unwrap());
18471846
let mut hybrid_preds = impl_predicates.instantiate_identity(tcx);
1848-
hybrid_preds.predicates.extend(
1849-
trait_ty_predicates
1850-
.instantiate_own(tcx, trait_to_impl_substs)
1851-
.map(|(predicate, _)| predicate),
1852-
);
1847+
hybrid_preds
1848+
.predicates
1849+
.extend(trait_ty_predicates.instantiate_own(tcx, trait_to_impl_substs).predicates);
18531850

18541851
debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds);
18551852

@@ -1865,7 +1862,9 @@ fn compare_type_predicate_entailment<'tcx>(
18651862

18661863
debug!("compare_type_predicate_entailment: caller_bounds={:?}", param_env.caller_bounds());
18671864

1868-
for (predicate, span) in impl_ty_own_bounds {
1865+
assert_eq!(impl_ty_own_bounds.predicates.len(), impl_ty_own_bounds.spans.len());
1866+
for (span, predicate) in std::iter::zip(impl_ty_own_bounds.spans, impl_ty_own_bounds.predicates)
1867+
{
18691868
let cause = ObligationCause::misc(span, impl_ty_hir_id);
18701869
let predicate = ocx.normalize(&cause, param_env, predicate);
18711870

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use rustc_trait_selection::traits::{
3232
};
3333

3434
use std::cell::LazyCell;
35+
use std::iter;
3536
use std::ops::{ControlFlow, Deref};
3637

3738
pub(super) struct WfCheckingCtxt<'a, 'tcx> {
@@ -1309,7 +1310,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
13091310
let infcx = wfcx.infcx;
13101311
let tcx = wfcx.tcx();
13111312

1312-
let predicates = tcx.predicates_of(def_id.to_def_id());
1313+
let predicates = tcx.bound_predicates_of(def_id.to_def_id());
13131314
let generics = tcx.generics_of(def_id);
13141315

13151316
let is_our_default = |def: &ty::GenericParamDef| match def.kind {
@@ -1410,6 +1411,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14101411

14111412
// Now we build the substituted predicates.
14121413
let default_obligations = predicates
1414+
.0
14131415
.predicates
14141416
.iter()
14151417
.flat_map(|&(pred, sp)| {
@@ -1440,13 +1442,13 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14401442
}
14411443
let mut param_count = CountParams::default();
14421444
let has_region = pred.visit_with(&mut param_count).is_break();
1443-
let substituted_pred = ty::EarlyBinder(pred).subst(tcx, substs);
1445+
let substituted_pred = predicates.rebind(pred).subst(tcx, substs);
14441446
// Don't check non-defaulted params, dependent defaults (including lifetimes)
14451447
// or preds with multiple params.
14461448
if substituted_pred.has_non_region_param() || param_count.params.len() > 1 || has_region
14471449
{
14481450
None
1449-
} else if predicates.predicates.iter().any(|&(p, _)| p == substituted_pred) {
1451+
} else if predicates.0.predicates.iter().any(|&(p, _)| p == substituted_pred) {
14501452
// Avoid duplication of predicates that contain no parameters, for example.
14511453
None
14521454
} else {
@@ -1472,21 +1474,22 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14721474
traits::Obligation::new(tcx, cause, wfcx.param_env, pred)
14731475
});
14741476

1475-
let predicates = predicates.instantiate_identity(tcx);
1477+
let predicates = predicates.0.instantiate_identity(tcx);
14761478

14771479
let predicates = wfcx.normalize(span, None, predicates);
14781480

14791481
debug!(?predicates.predicates);
14801482
assert_eq!(predicates.predicates.len(), predicates.spans.len());
1481-
let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| {
1482-
traits::wf::predicate_obligations(
1483-
infcx,
1484-
wfcx.param_env.without_const(),
1485-
wfcx.body_id,
1486-
p,
1487-
sp,
1488-
)
1489-
});
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+
});
14901493

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

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,14 @@ 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 predicates {
378+
for (predicate, predicate_span) in
379+
predicates.predicates.iter().zip(&predicates.spans)
380+
{
379381
let obligation = Obligation::new(
380382
self.tcx,
381383
ObligationCause::dummy_with_span(callee_expr.span),
382384
self.param_env,
383-
predicate,
385+
*predicate,
384386
);
385387
let result = self.evaluate_obligation(&obligation);
386388
self.tcx
@@ -389,7 +391,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
389391
callee_expr.span,
390392
&format!("evaluate({:?}) = {:?}", predicate, result),
391393
)
392-
.span_label(predicate_span, "predicate")
394+
.span_label(*predicate_span, "predicate")
393395
.emit();
394396
}
395397
}

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2140,7 +2140,8 @@ 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 instantiated
2143+
for (predicate, span) in
2144+
std::iter::zip(instantiated.predicates, instantiated.spans)
21442145
{
21452146
if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder()
21462147
&& pred.self_ty().peel_refs() == callee_ty

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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;
2223
use std::ops::Deref;
2324

2425
struct ConfirmContext<'a, 'tcx> {
@@ -100,7 +101,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
100101
let filler_substs = rcvr_substs
101102
.extend_to(self.tcx, pick.item.def_id, |def, _| self.tcx.mk_param_from_def(def));
102103
let illegal_sized_bound = self.predicates_require_illegal_sized_bound(
103-
self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs),
104+
&self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs),
104105
);
105106

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

565566
fn predicates_require_illegal_sized_bound(
566567
&self,
567-
predicates: ty::InstantiatedPredicates<'tcx>,
568+
predicates: &ty::InstantiatedPredicates<'tcx>,
568569
) -> Option<Span> {
569570
let sized_def_id = self.tcx.lang_items().sized_trait()?;
570571

@@ -574,11 +575,10 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
574575
ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred))
575576
if trait_pred.def_id() == sized_def_id =>
576577
{
577-
let span = predicates
578-
.iter()
578+
let span = iter::zip(&predicates.predicates, &predicates.spans)
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_infer/src/infer/error_reporting/note.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
330330

331331
let Ok(trait_predicates) = self
332332
.tcx
333-
.explicit_predicates_of(trait_item_def_id)
334-
.instantiate_own(self.tcx, trait_item_substs)
333+
.bound_explicit_predicates_of(trait_item_def_id)
334+
.map_bound(|p| p.predicates)
335+
.subst_iter_copied(self.tcx, trait_item_substs)
335336
.map(|(pred, _)| {
336337
if pred.is_suggestable(self.tcx, false) {
337338
Ok(pred.to_string())

compiler/rustc_middle/src/ty/generics.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,15 @@ impl<'tcx> GenericPredicates<'tcx> {
341341
&self,
342342
tcx: TyCtxt<'tcx>,
343343
substs: SubstsRef<'tcx>,
344-
) -> impl Iterator<Item = (Predicate<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator
345-
{
346-
EarlyBinder(self.predicates).subst_iter_copied(tcx, substs)
344+
) -> InstantiatedPredicates<'tcx> {
345+
InstantiatedPredicates {
346+
predicates: self
347+
.predicates
348+
.iter()
349+
.map(|(p, _)| EarlyBinder(*p).subst(tcx, substs))
350+
.collect(),
351+
spans: self.predicates.iter().map(|(_, sp)| *sp).collect(),
352+
}
347353
}
348354

349355
#[instrument(level = "debug", skip(self, tcx))]

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,35 +1252,6 @@ 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-
debug_assert_eq!(self.predicates.len(), self.spans.len());
1268-
std::iter::zip(self.predicates, self.spans)
1269-
}
1270-
}
1271-
1272-
impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> {
1273-
type Item = (Predicate<'tcx>, Span);
1274-
1275-
type IntoIter = std::iter::Zip<
1276-
std::iter::Copied<std::slice::Iter<'a, Predicate<'tcx>>>,
1277-
std::iter::Copied<std::slice::Iter<'a, Span>>,
1278-
>;
1279-
1280-
fn into_iter(self) -> Self::IntoIter {
1281-
debug_assert_eq!(self.predicates.len(), self.spans.len());
1282-
std::iter::zip(self.predicates.iter().copied(), self.spans.iter().copied())
1283-
}
12841255
}
12851256

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

compiler/rustc_middle/src/ty/subst.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -639,13 +639,6 @@ where
639639
}
640640
}
641641

642-
impl<'tcx, I: IntoIterator> ExactSizeIterator for SubstIter<'_, 'tcx, I>
643-
where
644-
I::IntoIter: ExactSizeIterator,
645-
I::Item: TypeFoldable<'tcx>,
646-
{
647-
}
648-
649642
impl<'tcx, 's, I: IntoIterator> EarlyBinder<I>
650643
where
651644
I::Item: Deref,
@@ -693,14 +686,6 @@ where
693686
}
694687
}
695688

696-
impl<'tcx, I: IntoIterator> ExactSizeIterator for SubstIterCopied<'_, 'tcx, I>
697-
where
698-
I::IntoIter: ExactSizeIterator,
699-
I::Item: Deref,
700-
<I::Item as Deref>::Target: Copy + TypeFoldable<'tcx>,
701-
{
702-
}
703-
704689
pub struct EarlyBinderIter<T> {
705690
t: T,
706691
}

0 commit comments

Comments
 (0)