From 8276dac08183e374d27b2c3165d10b9dfcde09b3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 19 Jun 2022 16:30:32 +0200 Subject: [PATCH] Mark inherent impls as using the type during liveness collection. --- compiler/rustc_passes/src/dead.rs | 46 ++++++++++++++----------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/compiler/rustc_passes/src/dead.rs b/compiler/rustc_passes/src/dead.rs index 01d93f6ff0c..648090f2b4d 100644 --- a/compiler/rustc_passes/src/dead.rs +++ b/compiler/rustc_passes/src/dead.rs @@ -285,20 +285,33 @@ fn visit_node(&mut self, node: Node<'tcx>) { let def = self.tcx.adt_def(item.def_id); self.repr_has_repr_c = def.repr().c(); - intravisit::walk_item(self, &item); - } - hir::ItemKind::Enum(..) => { - intravisit::walk_item(self, &item); + intravisit::walk_item(self, &item) } hir::ItemKind::ForeignMod { .. } => {} - _ => { - intravisit::walk_item(self, &item); - } + _ => intravisit::walk_item(self, &item), }, Node::TraitItem(trait_item) => { intravisit::walk_trait_item(self, trait_item); } Node::ImplItem(impl_item) => { + let item = self.tcx.local_parent(impl_item.def_id); + if self.tcx.impl_trait_ref(item).is_none() { + //// If it's a type whose items are live, then it's live, too. + //// This is done to handle the case where, for example, the static + //// method of a private type is used, but the type itself is never + //// called directly. + let self_ty = self.tcx.type_of(item); + match *self_ty.kind() { + ty::Adt(def, _) => self.check_def_id(def.did()), + ty::Foreign(did) => self.check_def_id(did), + ty::Dynamic(data, ..) => { + if let Some(def_id) = data.principal_def_id() { + self.check_def_id(def_id) + } + } + _ => {} + } + } intravisit::walk_impl_item(self, impl_item); } Node::ForeignItem(foreign_item) => { @@ -671,24 +684,7 @@ fn should_warn_about_foreign_item(&mut self, fi: &hir::ForeignItem<'_>) -> bool // id := HIR id of an item's definition. fn symbol_is_live(&mut self, def_id: LocalDefId) -> bool { - if self.live_symbols.contains(&def_id) { - return true; - } - // If it's a type whose items are live, then it's live, too. - // This is done to handle the case where, for example, the static - // method of a private type is used, but the type itself is never - // called directly. - let inherent_impls = self.tcx.inherent_impls(def_id); - for &impl_did in inherent_impls.iter() { - for item_did in self.tcx.associated_item_def_ids(impl_did) { - if let Some(def_id) = item_did.as_local() - && self.live_symbols.contains(&def_id) - { - return true; - } - } - } - false + self.live_symbols.contains(&def_id) } fn warn_multiple_dead_codes(