Only create the opaque collector once and visit it many times

This commit is contained in:
Oli Scherer 2023-06-15 14:10:42 +00:00
parent c8979e587b
commit bae645451e

View File

@ -22,14 +22,8 @@ struct OpaqueTypeCollector<'tcx> {
} }
impl<'tcx> OpaqueTypeCollector<'tcx> { impl<'tcx> OpaqueTypeCollector<'tcx> {
fn collect( fn new(tcx: TyCtxt<'tcx>, item: LocalDefId) -> Self {
tcx: TyCtxt<'tcx>, Self { tcx, opaques: Vec::new(), item, seen: Default::default() }
item: LocalDefId,
val: ty::Binder<'tcx, impl TypeVisitable<TyCtxt<'tcx>>>,
) -> Vec<LocalDefId> {
let mut collector = Self { tcx, opaques: Vec::new(), item, seen: Default::default() };
val.skip_binder().visit_with(&mut collector);
collector.opaques
} }
fn span(&self) -> Span { fn span(&self) -> Span {
@ -166,21 +160,17 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
match kind { match kind {
// We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds` // We're also doing this for `AssocTy` for the wf checks in `check_opaque_meets_bounds`
DefKind::Fn | DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => { DefKind::Fn | DefKind::AssocFn | DefKind::AssocTy | DefKind::AssocConst => {
let defined_opaques = match kind { let mut collector = OpaqueTypeCollector::new(tcx, item);
DefKind::Fn => { match kind {
OpaqueTypeCollector::collect(tcx, item, tcx.fn_sig(item).subst_identity()) DefKind::AssocFn | DefKind::Fn => {
tcx.fn_sig(item).subst_identity().visit_with(&mut collector);
} }
DefKind::AssocFn => { DefKind::AssocTy | DefKind::AssocConst => {
OpaqueTypeCollector::collect(tcx, item, tcx.fn_sig(item).subst_identity()) tcx.type_of(item).subst_identity().visit_with(&mut collector);
} }
DefKind::AssocTy | DefKind::AssocConst => OpaqueTypeCollector::collect(
tcx,
item,
ty::Binder::dummy(tcx.type_of(item).subst_identity()),
),
_ => unreachable!(), _ => unreachable!(),
}; }
tcx.arena.alloc_from_iter(defined_opaques) tcx.arena.alloc_from_iter(collector.opaques)
} }
DefKind::Mod DefKind::Mod
| DefKind::Struct | DefKind::Struct