From: Michael Goulet Date: Tue, 3 Jan 2023 03:32:59 +0000 (+0000) Subject: Make InstantiatedPredicates impl IntoIterator X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=9b28edb6d7746f457e79f5ca2c4ade944f653113;p=rust.git Make InstantiatedPredicates impl IntoIterator --- diff --git a/compiler/rustc_borrowck/src/type_check/canonical.rs b/compiler/rustc_borrowck/src/type_check/canonical.rs index 02222c0a03c..11729e2c83f 100644 --- a/compiler/rustc_borrowck/src/type_check/canonical.rs +++ b/compiler/rustc_borrowck/src/type_check/canonical.rs @@ -107,11 +107,7 @@ pub(super) fn normalize_and_prove_instantiated_predicates( instantiated_predicates: ty::InstantiatedPredicates<'tcx>, locations: Locations, ) { - for (predicate, span) in instantiated_predicates - .predicates - .into_iter() - .zip(instantiated_predicates.spans.into_iter()) - { + for (predicate, span) in instantiated_predicates { debug!(?predicate); let category = ConstraintCategory::Predicate(span); let predicate = self.normalize_with_category(predicate, locations, category); diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index 912e0ec560b..9af325b77fc 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -32,7 +32,6 @@ }; use std::cell::LazyCell; -use std::iter; use std::ops::{ControlFlow, Deref}; pub(super) struct WfCheckingCtxt<'a, 'tcx> { @@ -1480,16 +1479,15 @@ fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow { debug!(?predicates.predicates); assert_eq!(predicates.predicates.len(), predicates.spans.len()); - let wf_obligations = - iter::zip(&predicates.predicates, &predicates.spans).flat_map(|(&p, &sp)| { - traits::wf::predicate_obligations( - infcx, - wfcx.param_env.without_const(), - wfcx.body_id, - p, - sp, - ) - }); + let wf_obligations = predicates.into_iter().flat_map(|(p, sp)| { + traits::wf::predicate_obligations( + infcx, + wfcx.param_env.without_const(), + wfcx.body_id, + p, + sp, + ) + }); let obligations: Vec<_> = wf_obligations.chain(default_obligations).collect(); wfcx.register_obligations(obligations); diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index f1a4f94cd01..b617821fbd6 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -375,14 +375,12 @@ fn confirm_builtin_call( if self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses) { let predicates = self.tcx.predicates_of(def_id); let predicates = predicates.instantiate(self.tcx, subst); - for (predicate, predicate_span) in - predicates.predicates.iter().zip(&predicates.spans) - { + for (predicate, predicate_span) in predicates { let obligation = Obligation::new( self.tcx, ObligationCause::dummy_with_span(callee_expr.span), self.param_env, - *predicate, + predicate, ); let result = self.evaluate_obligation(&obligation); self.tcx @@ -391,7 +389,7 @@ fn confirm_builtin_call( callee_expr.span, &format!("evaluate({:?}) = {:?}", predicate, result), ) - .span_label(*predicate_span, "predicate") + .span_label(predicate_span, "predicate") .emit(); } } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index b9e13fd2009..c9609e69439 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -2140,8 +2140,7 @@ fn label_fn_like( // FIXME(compiler-errors): This could be problematic if something has two // fn-like predicates with different args, but callable types really never // do that, so it's OK. - for (predicate, span) in - std::iter::zip(instantiated.predicates, instantiated.spans) + for (predicate, span) in instantiated { if let ty::PredicateKind::Clause(ty::Clause::Trait(pred)) = predicate.kind().skip_binder() && pred.self_ty().peel_refs() == callee_ty diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 4a33a791e1b..372ea30ebd0 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -19,7 +19,6 @@ use rustc_span::{Span, DUMMY_SP}; use rustc_trait_selection::traits; -use std::iter; use std::ops::Deref; struct ConfirmContext<'a, 'tcx> { @@ -101,7 +100,7 @@ fn confirm( let filler_substs = rcvr_substs .extend_to(self.tcx, pick.item.def_id, |def, _| self.tcx.mk_param_from_def(def)); let illegal_sized_bound = self.predicates_require_illegal_sized_bound( - &self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs), + self.tcx.predicates_of(pick.item.def_id).instantiate(self.tcx, filler_substs), ); // Unify the (adjusted) self type with what the method expects. @@ -565,7 +564,7 @@ fn add_obligations( fn predicates_require_illegal_sized_bound( &self, - predicates: &ty::InstantiatedPredicates<'tcx>, + predicates: ty::InstantiatedPredicates<'tcx>, ) -> Option { let sized_def_id = self.tcx.lang_items().sized_trait()?; @@ -575,10 +574,11 @@ fn predicates_require_illegal_sized_bound( ty::PredicateKind::Clause(ty::Clause::Trait(trait_pred)) if trait_pred.def_id() == sized_def_id => { - let span = iter::zip(&predicates.predicates, &predicates.spans) + let span = predicates + .iter() .find_map( |(p, span)| { - if *p == obligation.predicate { Some(*span) } else { None } + if p == obligation.predicate { Some(span) } else { None } }, ) .unwrap_or(rustc_span::DUMMY_SP); diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index e9bd0e9866f..655753a42ef 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -1252,6 +1252,33 @@ pub fn empty() -> InstantiatedPredicates<'tcx> { pub fn is_empty(&self) -> bool { self.predicates.is_empty() } + + pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter { + (&self).into_iter() + } +} + +impl<'tcx> IntoIterator for InstantiatedPredicates<'tcx> { + type Item = (Predicate<'tcx>, Span); + + type IntoIter = std::iter::Zip>, std::vec::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + std::iter::zip(self.predicates, self.spans) + } +} + +impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> { + type Item = (Predicate<'tcx>, Span); + + type IntoIter = std::iter::Zip< + std::iter::Copied>>, + std::iter::Copied>, + >; + + fn into_iter(self) -> Self::IntoIter { + std::iter::zip(self.predicates.iter().copied(), self.spans.iter().copied()) + } } #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable, TyEncodable, TyDecodable, Lift)] diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs index df57c0f60fa..0419bb3f724 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/ambiguity.rs @@ -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)) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 5211f873a1e..6e2341a823b 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -2070,7 +2070,7 @@ fn note_conflicting_closure_bounds( // 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 @@ fn note_conflicting_closure_bounds( // 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", ); } diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 13aa067844a..531aa23d6ea 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -115,14 +115,12 @@ pub fn predicates_for_generics<'tcx>( param_env: ty::ParamEnv<'tcx>, generic_bounds: ty::InstantiatedPredicates<'tcx>, ) -> impl Iterator> { - 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 diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 15320916fbe..9c655aff0ba 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -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, diff --git a/compiler/rustc_trait_selection/src/traits/wf.rs b/compiler/rustc_trait_selection/src/traits/wf.rs index fec4047ff49..2cebad64c43 100644 --- a/compiler/rustc_trait_selection/src/traits/wf.rs +++ b/compiler/rustc_trait_selection/src/traits/wf.rs @@ -736,7 +736,7 @@ fn nominal_obligations_inner( 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) diff --git a/compiler/rustc_traits/src/type_op.rs b/compiler/rustc_traits/src/type_op.rs index aa5c83ac2e6..f35c5e44882 100644 --- a/compiler/rustc_traits/src/type_op.rs +++ b/compiler/rustc_traits/src/type_op.rs @@ -17,7 +17,6 @@ use rustc_trait_selection::traits::query::{Fallible, NoSolution}; use rustc_trait_selection::traits::{Normalized, Obligation, ObligationCause, ObligationCtxt}; use std::fmt; -use std::iter::zip; pub(crate) fn provide(p: &mut Providers) { *p = Providers { @@ -108,9 +107,7 @@ fn relate_mir_and_user_substs<'tcx>( let instantiated_predicates = tcx.predicates_of(def_id).instantiate(tcx, substs); debug!(?instantiated_predicates); - for (instantiated_predicate, predicate_span) in - zip(instantiated_predicates.predicates, instantiated_predicates.spans) - { + for (instantiated_predicate, predicate_span) in instantiated_predicates { let span = if span == DUMMY_SP { predicate_span } else { span }; let cause = ObligationCause::new( span,