Skip to content

Commit e54854f

Browse files
committed
add subst_identity_iter and subst_identity_iter_copied methods on EarlyBinder; use this to simplify some EarlyBinder noise around explicit_item_bounds calls
1 parent f3b279f commit e54854f

File tree

12 files changed

+44
-51
lines changed

12 files changed

+44
-51
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,10 @@ pub(super) fn check_opaque_for_inheriting_lifetimes(
318318
tcx,
319319
selftys: vec![],
320320
};
321-
let prohibit_opaque =
322-
tcx.explicit_item_bounds(def_id).transpose_iter().try_for_each(|bound| {
323-
let predicate = bound.map_bound(|&(predicate, _)| predicate).subst_identity();
324-
predicate.visit_with(&mut visitor)
325-
});
321+
let prohibit_opaque = tcx
322+
.explicit_item_bounds(def_id)
323+
.subst_identity_iter_copied()
324+
.try_for_each(|(predicate, _)| predicate.visit_with(&mut visitor));
326325

327326
if let Some(ty) = prohibit_opaque.break_value() {
328327
visitor.visit_item(&item);

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
361361
param_env,
362362
item_def_id,
363363
tcx.explicit_item_bounds(item_def_id)
364-
.transpose_iter()
365-
.map(|bound| bound.map_bound(|b| *b).subst_identity())
364+
.subst_identity_iter_copied()
366365
.collect::<Vec<_>>(),
367366
&FxIndexSet::default(),
368367
gat_def_id.def_id,
@@ -1128,8 +1127,7 @@ fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocIt
11281127
let bounds = wfcx.tcx().explicit_item_bounds(item.def_id);
11291128

11301129
debug!("check_associated_type_bounds: bounds={:?}", bounds);
1131-
let wf_obligations = bounds.transpose_iter().flat_map(|b| {
1132-
let (bound, bound_span) = b.map_bound(|b| *b).subst_identity();
1130+
let wf_obligations = bounds.subst_identity_iter_copied().flat_map(|(bound, bound_span)| {
11331131
let normalized_bound = wfcx.normalize(span, None, bound);
11341132
traits::wf::predicate_obligations(
11351133
wfcx.infcx,

compiler/rustc_hir_analysis/src/variance/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
153153
let mut collector =
154154
OpaqueTypeLifetimeCollector { tcx, root_def_id: item_def_id.to_def_id(), variances };
155155
let id_substs = ty::InternalSubsts::identity_for_item(tcx, item_def_id);
156-
for pred in tcx.explicit_item_bounds(item_def_id).transpose_iter() {
157-
let pred = pred.map_bound(|(pred, _)| *pred).subst(tcx, id_substs);
156+
for (pred, _) in tcx.explicit_item_bounds(item_def_id).subst_iter_copied(tcx, id_substs) {
158157
debug!(?pred);
159158

160159
// We only ignore opaque type substs if the opaque type is the outermost type.

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ fn check_must_not_suspend_ty<'tcx>(
571571
// FIXME: support adding the attribute to TAITs
572572
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
573573
let mut has_emitted = false;
574-
for bound in fcx.tcx.explicit_item_bounds(def).transpose_iter() {
575-
let predicate = bound.map_bound(|&(pred, _)| pred).subst_identity();
574+
for (predicate, _) in fcx.tcx.explicit_item_bounds(def).subst_identity_iter_copied() {
576575
// We only look at the `DefId`, so it is safe to skip the binder here.
577576
if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
578577
predicate.kind().skip_binder()

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
7474
// For every projection predicate in the opaque type's explicit bounds,
7575
// check that the type that we're assigning actually satisfies the bounds
7676
// of the associated type.
77-
for bound in cx.tcx.explicit_item_bounds(def_id).transpose_iter() {
78-
let (pred, pred_span) = bound.map_bound(|b| *b).subst_identity();
79-
77+
for (pred, pred_span) in cx.tcx.explicit_item_bounds(def_id).subst_identity_iter_copied() {
8078
// Liberate bound regions in the predicate since we
8179
// don't actually care about lifetimes in this check.
8280
let predicate = cx.tcx.liberate_late_bound_regions(def_id, pred.kind());

compiler/rustc_lint/src/unused.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -254,29 +254,23 @@ impl<'tcx> LateLintPass<'tcx> for UnusedResults {
254254
}
255255
ty::Adt(def, _) => is_def_must_use(cx, def.did(), span),
256256
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
257-
elaborate(
258-
cx.tcx,
259-
cx.tcx
260-
.explicit_item_bounds(def)
261-
.transpose_iter()
262-
.map(|bound| bound.map_bound(|b| *b).subst_identity()),
263-
)
264-
// We only care about self bounds for the impl-trait
265-
.filter_only_self()
266-
.find_map(|(pred, _span)| {
267-
// We only look at the `DefId`, so it is safe to skip the binder here.
268-
if let ty::PredicateKind::Clause(ty::Clause::Trait(
269-
ref poly_trait_predicate,
270-
)) = pred.kind().skip_binder()
271-
{
272-
let def_id = poly_trait_predicate.trait_ref.def_id;
273-
274-
is_def_must_use(cx, def_id, span)
275-
} else {
276-
None
277-
}
278-
})
279-
.map(|inner| MustUsePath::Opaque(Box::new(inner)))
257+
elaborate(cx.tcx, cx.tcx.explicit_item_bounds(def).subst_identity_iter_copied())
258+
// We only care about self bounds for the impl-trait
259+
.filter_only_self()
260+
.find_map(|(pred, _span)| {
261+
// We only look at the `DefId`, so it is safe to skip the binder here.
262+
if let ty::PredicateKind::Clause(ty::Clause::Trait(
263+
ref poly_trait_predicate,
264+
)) = pred.kind().skip_binder()
265+
{
266+
let def_id = poly_trait_predicate.trait_ref.def_id;
267+
268+
is_def_must_use(cx, def_id, span)
269+
} else {
270+
None
271+
}
272+
})
273+
.map(|inner| MustUsePath::Opaque(Box::new(inner)))
280274
}
281275
ty::Dynamic(binders, _, _) => binders.iter().find_map(|predicate| {
282276
if let ty::ExistentialPredicate::Trait(ref trait_ref) = predicate.skip_binder()

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ impl<'tcx> TyCtxt<'tcx> {
16111611
let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind() else { return false };
16121612
let future_trait = self.require_lang_item(LangItem::Future, None);
16131613

1614-
self.explicit_item_bounds(def_id).skip_binder().iter().any(|(predicate, _)| {
1614+
self.explicit_item_bounds(def_id).subst_identity_iter_copied().any(|(predicate, _)| {
16151615
let ty::PredicateKind::Clause(ty::Clause::Trait(trait_predicate)) = predicate.kind().skip_binder() else {
16161616
return false;
16171617
};

compiler/rustc_middle/src/ty/subst.rs

+12
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ where
625625
) -> SubstIter<'s, 'tcx, I> {
626626
SubstIter { it: self.0.into_iter(), tcx, substs }
627627
}
628+
629+
/// Similar to [`subst_identity`](EarlyBinder::subst_identity),
630+
/// but on an iterator of `TypeFoldable` values.
631+
pub fn subst_identity_iter(self) -> I::IntoIter {
632+
self.0.into_iter()
633+
}
628634
}
629635

630636
pub struct SubstIter<'s, 'tcx, I: IntoIterator> {
@@ -677,6 +683,12 @@ where
677683
) -> SubstIterCopied<'s, 'tcx, I> {
678684
SubstIterCopied { it: self.0.into_iter(), tcx, substs }
679685
}
686+
687+
/// Similar to [`subst_identity`](EarlyBinder::subst_identity),
688+
/// but on an iterator of values that deref to a `TypeFoldable`.
689+
pub fn subst_identity_iter_copied(self) -> impl Iterator<Item = <I::Item as Deref>::Target> {
690+
self.0.into_iter().map(|v| *v)
691+
}
680692
}
681693

682694
pub struct SubstIterCopied<'a, 'tcx, I: IntoIterator> {

compiler/rustc_mir_transform/src/generator.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1800,9 +1800,7 @@ fn check_must_not_suspend_ty<'tcx>(
18001800
// FIXME: support adding the attribute to TAITs
18011801
ty::Alias(ty::Opaque, ty::AliasTy { def_id: def, .. }) => {
18021802
let mut has_emitted = false;
1803-
for bound in tcx.explicit_item_bounds(def).transpose_iter() {
1804-
let predicate = bound.map_bound(|&(pred, _)| pred).subst_identity();
1805-
1803+
for (predicate, _) in tcx.explicit_item_bounds(def).subst_identity_iter_copied() {
18061804
// We only look at the `DefId`, so it is safe to skip the binder here.
18071805
if let ty::PredicateKind::Clause(ty::Clause::Trait(ref poly_trait_predicate)) =
18081806
predicate.kind().skip_binder()

compiler/rustc_trait_selection/src/traits/object_safety.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ fn bounds_reference_self(tcx: TyCtxt<'_>, trait_def_id: DefId) -> SmallVec<[Span
297297
tcx.associated_items(trait_def_id)
298298
.in_definition_order()
299299
.filter(|item| item.kind == ty::AssocKind::Type)
300-
.flat_map(|item| tcx.explicit_item_bounds(item.def_id).transpose_iter())
301-
.map(|bound| bound.map_bound(|b| *b).subst_identity())
300+
.flat_map(|item| tcx.explicit_item_bounds(item.def_id).subst_identity_iter_copied())
302301
.filter_map(|pred_span| predicate_references_self(tcx, pred_span))
303302
.collect()
304303
}

src/librustdoc/clean/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1315,10 +1315,8 @@ pub(crate) fn clean_middle_assoc_item<'tcx>(
13151315
}
13161316

13171317
if let ty::TraitContainer = assoc_item.container {
1318-
let bounds = tcx
1319-
.explicit_item_bounds(assoc_item.def_id)
1320-
.transpose_iter()
1321-
.map(|bound| bound.map_bound(|b| *b).subst_identity());
1318+
let bounds =
1319+
tcx.explicit_item_bounds(assoc_item.def_id).subst_identity_iter_copied();
13221320
let predicates = tcx.explicit_predicates_of(assoc_item.def_id).predicates;
13231321
let predicates =
13241322
tcx.arena.alloc_from_iter(bounds.chain(predicates.iter().copied()));

src/tools/clippy/clippy_utils/src/ty.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ pub fn contains_ty_adt_constructor_opaque<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'
9090
return false;
9191
}
9292

93-
for bound in cx.tcx.explicit_item_bounds(def_id).transpose_iter() {
94-
let (predicate, _span) = bound.map_bound(|b| *b).subst_identity();
93+
for (predicate, _span) in cx.tcx.explicit_item_bounds(def_id).subst_identity_iter_copied() {
9594
match predicate.kind().skip_binder() {
9695
// For `impl Trait<U>`, it will register a predicate of `T: Trait<U>`, so we go through
9796
// and check substituions to find `U`.

0 commit comments

Comments
 (0)