Skip to content

Commit 13aaa5e

Browse files
committed
Propagate #[allow(dead_code)] on adts to their inherent impls
1 parent 59dabe5 commit 13aaa5e

2 files changed

Lines changed: 77 additions & 30 deletions

File tree

compiler/rustc_passes/src/dead.rs

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -943,38 +943,63 @@ fn maybe_record_as_seed<'tcx>(
943943
});
944944
}
945945

946+
let self_comes_from_allow = |impl_did| {
947+
if let ty::Adt(adt, _) =
948+
tcx.type_of(impl_did).instantiate_identity().skip_normalization().kind()
949+
&& let Some(adt_def_id) = adt.did().as_local()
950+
{
951+
has_allow_dead_code_or_lang_attr(tcx, adt_def_id)
952+
} else {
953+
None
954+
}
955+
};
956+
946957
match tcx.def_kind(owner_id) {
947-
DefKind::Enum => {
948-
if let Some(comes_from_allow) = allow_dead_code {
949-
let adt = tcx.adt_def(owner_id);
950-
for variant in adt.variants().iter() {
951-
push_into_worklist(WorkItem {
952-
id: variant.def_id.expect_local(),
953-
propagated: comes_from_allow,
954-
own: comes_from_allow,
955-
});
956-
}
958+
DefKind::Enum if let Some(comes_from_allow) = allow_dead_code => {
959+
let adt = tcx.adt_def(owner_id);
960+
for variant in adt.variants().iter() {
961+
push_into_worklist(WorkItem {
962+
id: variant.def_id.expect_local(),
963+
propagated: comes_from_allow,
964+
own: comes_from_allow,
965+
});
957966
}
958967
}
959-
DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::AssocTy => {
960-
if allow_dead_code.is_none() {
961-
let parent = tcx.local_parent(owner_id.def_id);
962-
match tcx.def_kind(parent) {
963-
DefKind::Impl { of_trait: false } | DefKind::Trait => {}
964-
DefKind::Impl { of_trait: true } => {
968+
DefKind::AssocFn | DefKind::AssocConst { .. } | DefKind::AssocTy
969+
if allow_dead_code.is_none() =>
970+
{
971+
let parent = tcx.local_parent(owner_id.def_id);
972+
match tcx.def_kind(parent) {
973+
DefKind::Trait => {}
974+
DefKind::Impl { of_trait } => {
975+
if of_trait {
965976
// We only care about associated items of traits,
966977
// because they cannot be visited directly,
967978
// so we later mark them as live if their corresponding traits
968979
// or trait items and self types are both live,
969980
// but inherent associated items can be visited and marked directly.
970981
unsolved_items.push(owner_id.def_id);
982+
} else if let Some(comes_from_allow) = self_comes_from_allow(parent) {
983+
push_into_worklist(WorkItem {
984+
id: owner_id.def_id,
985+
propagated: comes_from_allow,
986+
own: ComesFromAllowExpect::No,
987+
});
971988
}
972-
_ => bug!(),
973989
}
990+
_ => bug!(),
974991
}
975992
}
976-
DefKind::Impl { of_trait: true } if allow_dead_code.is_none() => {
977-
unsolved_items.push(owner_id.def_id);
993+
DefKind::Impl { of_trait } if allow_dead_code.is_none() => {
994+
if of_trait {
995+
unsolved_items.push(owner_id.def_id);
996+
} else if let Some(comes_from_allow) = self_comes_from_allow(owner_id.def_id) {
997+
push_into_worklist(WorkItem {
998+
id: owner_id.def_id,
999+
propagated: comes_from_allow,
1000+
own: ComesFromAllowExpect::No,
1001+
});
1002+
}
9781003
}
9791004
DefKind::GlobalAsm => {
9801005
// global_asm! is always live.
@@ -984,17 +1009,15 @@ fn maybe_record_as_seed<'tcx>(
9841009
own: ComesFromAllowExpect::No,
9851010
});
9861011
}
987-
DefKind::Const { .. } => {
988-
if tcx.item_name(owner_id.def_id) == kw::Underscore {
989-
// `const _` is always live, as that syntax only exists for the side effects
990-
// of type checking and evaluating the constant expression, and marking them
991-
// as dead code would defeat that purpose.
992-
push_into_worklist(WorkItem {
993-
id: owner_id.def_id,
994-
propagated: ComesFromAllowExpect::No,
995-
own: ComesFromAllowExpect::No,
996-
});
997-
}
1012+
DefKind::Const { .. } if tcx.item_name(owner_id.def_id) == kw::Underscore => {
1013+
// `const _` is always live, as that syntax only exists for the side effects
1014+
// of type checking and evaluating the constant expression, and marking them
1015+
// as dead code would defeat that purpose.
1016+
push_into_worklist(WorkItem {
1017+
id: owner_id.def_id,
1018+
propagated: ComesFromAllowExpect::No,
1019+
own: ComesFromAllowExpect::No,
1020+
});
9981021
}
9991022
_ => {}
10001023
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
3+
#![deny(dead_code)]
4+
5+
#[allow(dead_code)]
6+
struct Foo;
7+
8+
impl Foo {
9+
fn foo(&self) {}
10+
}
11+
12+
pub trait Tr {
13+
fn foo(&self);
14+
}
15+
16+
impl Tr for Foo {
17+
fn foo(&self) {
18+
bar()
19+
}
20+
}
21+
22+
fn bar() {}
23+
24+
fn main() {}

0 commit comments

Comments
 (0)