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
2 changes: 2 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
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
20 changes: 17 additions & 3 deletions compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,31 @@ impl<'tcx> Visitor<'tcx> for EmbargoVisitor<'tcx> {
// `impl ReachableTrait<UnreachableTy> for ReachableTy<UnreachableTy> { ... }`
// can be usable from other crates (#57264). So we skip args when calculating
// reachability and consider an impl reachable if its "shallow" type and trait are
// reachable.
// reachable if there were no private types.
//
// The assumption we make here is that type-inference won't let you use an impl
// without knowing both "shallow" version of its self type and "shallow" version of
// its trait if it exists (which require reaching the `DefId`s in them).
let item_ev = EffectiveVisibility::of_impl::<true>(
let impl_vis = ty::Visibility::of_impl::<false>(
item.owner_id.def_id,
self.tcx,
&self.effective_visibilities,
&Default::default(),
);

let item_ev = if impl_vis.is_public() {
EffectiveVisibility::of_impl::<true>(
item.owner_id.def_id,
self.tcx,
&self.effective_visibilities,
)
} else {
EffectiveVisibility::of_impl::<false>(
item.owner_id.def_id,
self.tcx,
&self.effective_visibilities,
)
};

self.update_eff_vis(item.owner_id.def_id, item_ev, None, Level::Direct);

self.reach(item.owner_id.def_id, item_ev).generics().predicates().ty().trait_ref();
Expand Down
89 changes: 89 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,89 @@
#![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 {
fn foo(&self) {}
}

impl Trait for [Foo] {
fn foo(&self) {}
}
impl<const N: usize> Trait for [Foo; N] {
fn foo(&self) {}
}

impl Trait for *const Foo {
fn foo(&self) {}
}
impl Trait for *mut Foo {
fn foo(&self) {}
}

impl Trait for &Foo {
fn foo(&self) {}
}
impl Trait for &&Foo {
fn foo(&self) {}
}
impl Trait for &mut Foo {
fn foo(&self) {}
}

impl Trait for [&Foo] {
fn foo(&self) {}
}
impl Trait for &[Foo] {
fn foo(&self) {}
}
impl Trait for &*const Foo {
fn foo(&self) {}
}

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

impl Trait2 for Foo {
fn foo(&self) {}
}

impl Trait2 for [Foo] {
fn foo(&self) {}
}
impl<const N: usize> Trait2 for [Foo; N] {
fn foo(&self) {}
}

impl Trait2 for *const Foo {
fn foo(&self) {}
}
impl Trait2 for *mut Foo {
fn foo(&self) {}
}

impl Trait2 for &Foo {
fn foo(&self) {}
}
impl Trait2 for &&Foo {
fn foo(&self) {}
}
impl Trait2 for &mut Foo {
fn foo(&self) {}
}

impl Trait2 for [&Foo] {
fn foo(&self) {}
}
impl Trait2 for &[Foo] {
fn foo(&self) {}
}
impl Trait2 for &*const Foo {
fn foo(&self) {}
}

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