Skip to content

Commit 52f833a

Browse files
committed
remove LifeSeeder
Signed-off-by: Miguel Guarniz <mi9uel9@gmail.com>
1 parent 0026034 commit 52f833a

File tree

1 file changed

+69
-52
lines changed

1 file changed

+69
-52
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 69 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_hir as hir;
88
use rustc_hir::def::{CtorOf, DefKind, Res};
99
use rustc_hir::def_id::{DefId, LocalDefId};
1010
use rustc_hir::intravisit::{self, Visitor};
11-
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1211
use rustc_hir::{Node, PatKind, TyKind};
1312
use rustc_middle::hir::nested_filter;
1413
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
@@ -468,7 +467,7 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
468467
tcx.lint_level_at_node(lint::builtin::DEAD_CODE, id).0 == lint::Allow
469468
}
470469

471-
// This visitor seeds items that
470+
// These check_* functions seeds items that
472471
// 1) We want to explicitly consider as live:
473472
// * Item annotated with #[allow(dead_code)]
474473
// - This is done so that if we want to suppress warnings for a
@@ -481,90 +480,99 @@ fn has_allow_dead_code_or_lang_attr(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
481480
// or
482481
// 2) We are not sure to be live or not
483482
// * Implementations of traits and trait methods
484-
struct LifeSeeder<'tcx> {
485-
worklist: Vec<LocalDefId>,
483+
fn check_item<'tcx>(
486484
tcx: TyCtxt<'tcx>,
487-
// see `MarkSymbolVisitor::struct_constructors`
488-
struct_constructors: FxHashMap<LocalDefId, LocalDefId>,
489-
}
490-
491-
impl<'v, 'tcx> ItemLikeVisitor<'v> for LifeSeeder<'tcx> {
492-
fn visit_item(&mut self, item: &hir::Item<'_>) {
493-
let allow_dead_code = has_allow_dead_code_or_lang_attr(self.tcx, item.hir_id());
494-
if allow_dead_code {
495-
self.worklist.push(item.def_id);
496-
}
497-
match item.kind {
498-
hir::ItemKind::Enum(ref enum_def, _) => {
499-
let hir = self.tcx.hir();
485+
worklist: &mut Vec<LocalDefId>,
486+
struct_constructors: &mut FxHashMap<LocalDefId, LocalDefId>,
487+
id: hir::ItemId,
488+
) {
489+
let allow_dead_code = has_allow_dead_code_or_lang_attr(tcx, id.hir_id());
490+
if allow_dead_code {
491+
worklist.push(id.def_id);
492+
}
493+
494+
match tcx.hir().def_kind(id.def_id) {
495+
DefKind::Enum => {
496+
let item = tcx.hir().item(id);
497+
if let hir::ItemKind::Enum(ref enum_def, _) = item.kind {
498+
let hir = tcx.hir();
500499
if allow_dead_code {
501-
self.worklist.extend(
500+
worklist.extend(
502501
enum_def.variants.iter().map(|variant| hir.local_def_id(variant.id)),
503502
);
504503
}
505504

506505
for variant in enum_def.variants {
507506
if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
508-
self.struct_constructors
507+
struct_constructors
509508
.insert(hir.local_def_id(ctor_hir_id), hir.local_def_id(variant.id));
510509
}
511510
}
512511
}
513-
hir::ItemKind::Impl(hir::Impl { ref of_trait, items, .. }) => {
512+
}
513+
DefKind::Impl => {
514+
let item = tcx.hir().item(id);
515+
if let hir::ItemKind::Impl(hir::Impl { ref of_trait, items, .. }) = item.kind {
514516
if of_trait.is_some() {
515-
self.worklist.push(item.def_id);
517+
worklist.push(item.def_id);
516518
}
517519
for impl_item_ref in *items {
518-
let impl_item = self.tcx.hir().impl_item(impl_item_ref.id);
520+
let impl_item = tcx.hir().impl_item(impl_item_ref.id);
519521
if of_trait.is_some()
520-
|| has_allow_dead_code_or_lang_attr(self.tcx, impl_item.hir_id())
522+
|| has_allow_dead_code_or_lang_attr(tcx, impl_item.hir_id())
521523
{
522-
self.worklist.push(impl_item_ref.id.def_id);
524+
worklist.push(impl_item_ref.id.def_id);
523525
}
524526
}
525527
}
526-
hir::ItemKind::Struct(ref variant_data, _) => {
528+
}
529+
DefKind::Struct => {
530+
let item = tcx.hir().item(id);
531+
if let hir::ItemKind::Struct(ref variant_data, _) = item.kind {
527532
if let Some(ctor_hir_id) = variant_data.ctor_hir_id() {
528-
self.struct_constructors
529-
.insert(self.tcx.hir().local_def_id(ctor_hir_id), item.def_id);
533+
struct_constructors.insert(tcx.hir().local_def_id(ctor_hir_id), item.def_id);
530534
}
531535
}
532-
hir::ItemKind::GlobalAsm(_) => {
533-
// global_asm! is always live.
534-
self.worklist.push(item.def_id);
535-
}
536-
_ => (),
537536
}
537+
DefKind::GlobalAsm => {
538+
// global_asm! is always live.
539+
worklist.push(id.def_id);
540+
}
541+
_ => {}
538542
}
543+
}
539544

540-
fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
541-
use hir::TraitItemKind::{Const, Fn};
545+
fn check_trait_item<'tcx>(tcx: TyCtxt<'tcx>, worklist: &mut Vec<LocalDefId>, id: hir::TraitItemId) {
546+
use hir::TraitItemKind::{Const, Fn};
547+
if matches!(tcx.hir().def_kind(id.def_id), DefKind::AssocConst | DefKind::AssocFn) {
548+
let trait_item = tcx.hir().trait_item(id);
542549
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_)))
543-
&& has_allow_dead_code_or_lang_attr(self.tcx, trait_item.hir_id())
550+
&& has_allow_dead_code_or_lang_attr(tcx, trait_item.hir_id())
544551
{
545-
self.worklist.push(trait_item.def_id);
552+
worklist.push(trait_item.def_id);
546553
}
547554
}
555+
}
548556

549-
fn visit_impl_item(&mut self, _item: &hir::ImplItem<'_>) {
550-
// ignore: we are handling this in `visit_item` above
551-
}
552-
553-
fn visit_foreign_item(&mut self, foreign_item: &hir::ForeignItem<'_>) {
554-
use hir::ForeignItemKind::{Fn, Static};
555-
if matches!(foreign_item.kind, Static(..) | Fn(..))
556-
&& has_allow_dead_code_or_lang_attr(self.tcx, foreign_item.hir_id())
557-
{
558-
self.worklist.push(foreign_item.def_id);
559-
}
557+
fn check_foreign_item<'tcx>(
558+
tcx: TyCtxt<'tcx>,
559+
worklist: &mut Vec<LocalDefId>,
560+
id: hir::ForeignItemId,
561+
) {
562+
if matches!(tcx.hir().def_kind(id.def_id), DefKind::Static(_) | DefKind::Fn)
563+
&& has_allow_dead_code_or_lang_attr(tcx, id.hir_id())
564+
{
565+
worklist.push(id.def_id);
560566
}
561567
}
562568

563569
fn create_and_seed_worklist<'tcx>(
564570
tcx: TyCtxt<'tcx>,
565571
) -> (Vec<LocalDefId>, FxHashMap<LocalDefId, LocalDefId>) {
566572
let access_levels = &tcx.privacy_access_levels(());
567-
let worklist = access_levels
573+
// see `MarkSymbolVisitor::struct_constructors`
574+
let mut struct_constructors = Default::default();
575+
let mut worklist = access_levels
568576
.map
569577
.iter()
570578
.filter_map(
@@ -576,11 +584,20 @@ fn create_and_seed_worklist<'tcx>(
576584
.chain(tcx.entry_fn(()).and_then(|(def_id, _)| def_id.as_local()))
577585
.collect::<Vec<_>>();
578586

579-
// Seed implemented trait items
580-
let mut life_seeder = LifeSeeder { worklist, tcx, struct_constructors: Default::default() };
581-
tcx.hir().visit_all_item_likes(&mut life_seeder);
587+
let crate_items = tcx.hir_crate_items(());
588+
for id in crate_items.items() {
589+
check_item(tcx, &mut worklist, &mut struct_constructors, id);
590+
}
591+
592+
for id in crate_items.trait_items() {
593+
check_trait_item(tcx, &mut worklist, id);
594+
}
595+
596+
for id in crate_items.foreign_items() {
597+
check_foreign_item(tcx, &mut worklist, id);
598+
}
582599

583-
(life_seeder.worklist, life_seeder.struct_constructors)
600+
(worklist, struct_constructors)
584601
}
585602

586603
fn live_symbols_and_ignored_derived_traits<'tcx>(

0 commit comments

Comments
 (0)