Skip to content

Commit 8276dac

Browse files
committed
Mark inherent impls as using the type during liveness collection.
1 parent 10f4ce3 commit 8276dac

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

compiler/rustc_passes/src/dead.rs

+21-25
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,33 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
285285
let def = self.tcx.adt_def(item.def_id);
286286
self.repr_has_repr_c = def.repr().c();
287287

288-
intravisit::walk_item(self, &item);
289-
}
290-
hir::ItemKind::Enum(..) => {
291-
intravisit::walk_item(self, &item);
288+
intravisit::walk_item(self, &item)
292289
}
293290
hir::ItemKind::ForeignMod { .. } => {}
294-
_ => {
295-
intravisit::walk_item(self, &item);
296-
}
291+
_ => intravisit::walk_item(self, &item),
297292
},
298293
Node::TraitItem(trait_item) => {
299294
intravisit::walk_trait_item(self, trait_item);
300295
}
301296
Node::ImplItem(impl_item) => {
297+
let item = self.tcx.local_parent(impl_item.def_id);
298+
if self.tcx.impl_trait_ref(item).is_none() {
299+
//// If it's a type whose items are live, then it's live, too.
300+
//// This is done to handle the case where, for example, the static
301+
//// method of a private type is used, but the type itself is never
302+
//// called directly.
303+
let self_ty = self.tcx.type_of(item);
304+
match *self_ty.kind() {
305+
ty::Adt(def, _) => self.check_def_id(def.did()),
306+
ty::Foreign(did) => self.check_def_id(did),
307+
ty::Dynamic(data, ..) => {
308+
if let Some(def_id) = data.principal_def_id() {
309+
self.check_def_id(def_id)
310+
}
311+
}
312+
_ => {}
313+
}
314+
}
302315
intravisit::walk_impl_item(self, impl_item);
303316
}
304317
Node::ForeignItem(foreign_item) => {
@@ -671,24 +684,7 @@ impl<'tcx> DeadVisitor<'tcx> {
671684

672685
// id := HIR id of an item's definition.
673686
fn symbol_is_live(&mut self, def_id: LocalDefId) -> bool {
674-
if self.live_symbols.contains(&def_id) {
675-
return true;
676-
}
677-
// If it's a type whose items are live, then it's live, too.
678-
// This is done to handle the case where, for example, the static
679-
// method of a private type is used, but the type itself is never
680-
// called directly.
681-
let inherent_impls = self.tcx.inherent_impls(def_id);
682-
for &impl_did in inherent_impls.iter() {
683-
for item_did in self.tcx.associated_item_def_ids(impl_did) {
684-
if let Some(def_id) = item_did.as_local()
685-
&& self.live_symbols.contains(&def_id)
686-
{
687-
return true;
688-
}
689-
}
690-
}
691-
false
687+
self.live_symbols.contains(&def_id)
692688
}
693689

694690
fn warn_multiple_dead_codes(

0 commit comments

Comments
 (0)