Skip to content

Extend dead code analysis of impls and impl items to non-ADTs #142968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_hir::{self as hir, ImplItem, ImplItemKind, Node, PatKind, QPath, TyKin
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::privacy::Level;
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::ty::{self, AssocItemContainer, TyCtxt};
use rustc_middle::{bug, span_bug};
use rustc_session::lint::builtin::DEAD_CODE;
use rustc_session::lint::{self, LintExpectationId};
Expand Down Expand Up @@ -58,6 +58,8 @@ fn local_adt_def_of_ty<'tcx>(ty: &hir::Ty<'tcx>) -> Option<LocalDefId> {
None
}
}
TyKind::Slice(ty) | TyKind::Array(ty, _) => local_adt_def_of_ty(ty),
TyKind::Ptr(ty) | TyKind::Ref(_, ty) => local_adt_def_of_ty(ty.ty),
_ => None,
}
}
Expand Down Expand Up @@ -832,6 +834,19 @@ fn create_and_seed_worklist(
effective_vis
.is_public_at_level(Level::Reachable)
.then_some(id)
.filter(|&id| match tcx.def_kind(id) {
// Check impls of trait and impl items of trait later in `check_impl_or_impl_item_live`,
// because `Self` may be non-ADTs and refer to local ADTs.
DefKind::Impl { of_trait } => !of_trait,
DefKind::AssocFn => {
let assoc_item = tcx.associated_item(id);
// Add reachable trait items and impl items not of traits
// into the worklist directly.
matches!(assoc_item.container, AssocItemContainer::Trait)
|| assoc_item.trait_item_def_id.is_none()
}
_ => true,
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this change related to supporting non-ADTs?
Isn't this reverting a part of changes from #141407?
The logic is again spread randomly between the various analysis stages, what is the principle behind all this, what is filtered away or analyzed at what stage?

.map(|id| (id, ComesFromAllowExpect::No))
})
// Seed entry point
Expand Down
45 changes: 45 additions & 0 deletions tests/ui/lint/dead-code/unused-impl-for-non-adts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#![deny(dead_code)]

struct Foo; //~ ERROR struct `Foo` is never constructed

trait Trait { //~ ERROR trait `Trait` is never used
fn foo(&self) {}
}

impl Trait for Foo {}

impl Trait for [Foo] {}
impl<const N: usize> Trait for [Foo; N] {}

impl Trait for *const Foo {}
impl Trait for *mut Foo {}

impl Trait for &Foo {}
impl Trait for &&Foo {}
impl Trait for &mut Foo {}

impl Trait for [&Foo] {}
impl Trait for &[Foo] {}
impl Trait for &*const Foo {}

pub trait Trait2 {
fn foo(&self) {}
}

impl Trait2 for Foo {}

impl Trait2 for [Foo] {}
impl<const N: usize> Trait2 for [Foo; N] {}

impl Trait2 for *const Foo {}
impl Trait2 for *mut Foo {}

impl Trait2 for &Foo {}
impl Trait2 for &&Foo {}
impl Trait2 for &mut Foo {}

impl Trait2 for [&Foo] {}
impl Trait2 for &[Foo] {}
impl Trait2 for &*const Foo {}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/lint/dead-code/unused-impl-for-non-adts.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: struct `Foo` is never constructed
--> $DIR/unused-impl-for-non-adts.rs:3:8
|
LL | struct Foo;
| ^^^
|
note: the lint level is defined here
--> $DIR/unused-impl-for-non-adts.rs:1:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: trait `Trait` is never used
--> $DIR/unused-impl-for-non-adts.rs:5:7
|
LL | trait Trait {
| ^^^^^

error: aborting due to 2 previous errors

Loading