1
Fork 0

Collect nested items immediately instead of collecting them into an intermediate Vec first

This commit is contained in:
Oli Scherer 2023-06-29 07:23:11 +00:00
parent b07d27c81e
commit 18f3d86588

View file

@ -103,31 +103,26 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
self.tcx.predicates_of(self.item).instantiate_identity(self.tcx).visit_with(self); self.tcx.predicates_of(self.item).instantiate_identity(self.tcx).visit_with(self);
// An item is allowed to constrain opaques declared within its own body (but not nested within // An item is allowed to constrain opaques declared within its own body (but not nested within
// nested functions). // nested functions).
for id in self.find_taits_declared_in_body() { self.collect_taits_declared_in_body();
self.opaques.extend(self.tcx.opaque_types_defined_by(id))
}
} }
#[instrument(level = "trace", skip(self), ret)] #[instrument(level = "trace", skip(self))]
fn find_taits_declared_in_body(&self) -> Vec<LocalDefId> { fn collect_taits_declared_in_body(&mut self) {
let body = self.tcx.hir().body(self.tcx.hir().body_owned_by(self.item)).value; let body = self.tcx.hir().body(self.tcx.hir().body_owned_by(self.item)).value;
struct TaitInBodyFinder<'tcx> { struct TaitInBodyFinder<'a, 'tcx> {
/// Ids of type aliases found in the body collector: &'a mut OpaqueTypeCollector<'tcx>,
type_aliases: Vec<LocalDefId>,
tcx: TyCtxt<'tcx>,
} }
impl<'v> intravisit::Visitor<'v> for TaitInBodyFinder<'_> { impl<'v> intravisit::Visitor<'v> for TaitInBodyFinder<'_, '_> {
#[instrument(level = "trace", skip(self))] #[instrument(level = "trace", skip(self))]
fn visit_nested_item(&mut self, id: rustc_hir::ItemId) { fn visit_nested_item(&mut self, id: rustc_hir::ItemId) {
let id = id.owner_id.def_id; let id = id.owner_id.def_id;
if let DefKind::TyAlias = self.tcx.def_kind(id) { if let DefKind::TyAlias = self.collector.tcx.def_kind(id) {
self.type_aliases.push(id); let items = self.collector.tcx.opaque_types_defined_by(id);
self.collector.opaques.extend(items);
} }
} }
} }
let mut visitor = TaitInBodyFinder { type_aliases: Default::default(), tcx: self.tcx }; TaitInBodyFinder { collector: self }.visit_expr(body);
visitor.visit_expr(body);
visitor.type_aliases
} }
} }