Skip to content

Commit bae6454

Browse files
committed
Only create the opaque collector once and visit it many times
1 parent c8979e5 commit bae6454

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,8 @@ struct OpaqueTypeCollector<'tcx> {
2222
}
2323

2424
impl<'tcx> OpaqueTypeCollector<'tcx> {
25-
fn collect(
26-
tcx: TyCtxt<'tcx>,
27-
item: LocalDefId,
28-
val: ty::Binder<'tcx, impl TypeVisitable<TyCtxt<'tcx>>>,
29-
) -> Vec<LocalDefId> {
30-
let mut collector = Self { tcx, opaques: Vec::new(), item, seen: Default::default() };
31-
val.skip_binder().visit_with(&mut collector);
32-
collector.opaques
25+
fn new(tcx: TyCtxt<'tcx>, item: LocalDefId) -> Self {
26+
Self { tcx, opaques: Vec::new(), item, seen: Default::default() }
3327
}
3428

3529
fn span(&self) -> Span {
@@ -166,21 +160,17 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
166160
match kind {
167161
// We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds`
168162
DefKind::Fn | DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
169-
let defined_opaques = match kind {
170-
DefKind::Fn => {
171-
OpaqueTypeCollector::collect(tcx, item, tcx.fn_sig(item).subst_identity())
163+
let mut collector = OpaqueTypeCollector::new(tcx, item);
164+
match kind {
165+
DefKind::AssocFn | DefKind::Fn => {
166+
tcx.fn_sig(item).subst_identity().visit_with(&mut collector);
172167
}
173-
DefKind::AssocFn => {
174-
OpaqueTypeCollector::collect(tcx, item, tcx.fn_sig(item).subst_identity())
168+
DefKind::AssocTy | DefKind::AssocConst => {
169+
tcx.type_of(item).subst_identity().visit_with(&mut collector);
175170
}
176-
DefKind::AssocTy | DefKind::AssocConst => OpaqueTypeCollector::collect(
177-
tcx,
178-
item,
179-
ty::Binder::dummy(tcx.type_of(item).subst_identity()),
180-
),
181171
_ => unreachable!(),
182-
};
183-
tcx.arena.alloc_from_iter(defined_opaques)
172+
}
173+
tcx.arena.alloc_from_iter(collector.opaques)
184174
}
185175
DefKind::Mod
186176
| DefKind::Struct

0 commit comments

Comments
 (0)