Skip to content

Commit 179e046

Browse files
committed
Detect unused structs which implement private traits
1 parent c69fda7 commit 179e046

File tree

6 files changed

+84
-17
lines changed

6 files changed

+84
-17
lines changed

compiler/rustc_passes/src/dead.rs

+33-16
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,11 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
427427
&& let ItemKind::Impl(impl_ref) =
428428
self.tcx.hir().expect_item(local_impl_id).kind
429429
{
430-
if self.tcx.visibility(trait_id).is_public()
431-
&& matches!(trait_item.kind, hir::TraitItemKind::Fn(..))
430+
if matches!(trait_item.kind, hir::TraitItemKind::Fn(..))
432431
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
433432
{
433+
// skip methods of private ty,
434+
// they would be solved in `solve_rest_impl_items`
434435
continue;
435436
}
436437

@@ -487,32 +488,46 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
487488

488489
fn solve_rest_impl_items(&mut self, mut unsolved_impl_items: Vec<(hir::ItemId, LocalDefId)>) {
489490
let mut ready;
490-
(ready, unsolved_impl_items) = unsolved_impl_items
491-
.into_iter()
492-
.partition(|&(impl_id, _)| self.impl_item_with_used_self(impl_id));
491+
(ready, unsolved_impl_items) =
492+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
493+
self.impl_item_with_used_self(impl_id, impl_item_id)
494+
});
493495

494496
while !ready.is_empty() {
495497
self.worklist =
496498
ready.into_iter().map(|(_, id)| (id, ComesFromAllowExpect::No)).collect();
497499
self.mark_live_symbols();
498500

499-
(ready, unsolved_impl_items) = unsolved_impl_items
500-
.into_iter()
501-
.partition(|&(impl_id, _)| self.impl_item_with_used_self(impl_id));
501+
(ready, unsolved_impl_items) =
502+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
503+
self.impl_item_with_used_self(impl_id, impl_item_id)
504+
});
502505
}
503506
}
504507

505-
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId) -> bool {
508+
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId, impl_item_id: LocalDefId) -> bool {
506509
if let TyKind::Path(hir::QPath::Resolved(_, path)) =
507510
self.tcx.hir().item(impl_id).expect_impl().self_ty.kind
508511
&& let Res::Def(def_kind, def_id) = path.res
509512
&& let Some(local_def_id) = def_id.as_local()
510513
&& matches!(def_kind, DefKind::Struct | DefKind::Enum | DefKind::Union)
511514
{
512-
self.live_symbols.contains(&local_def_id)
513-
} else {
514-
false
515+
if self.tcx.visibility(impl_item_id).is_public() {
516+
// for the public method, we don't know the trait item is used or not,
517+
// so we mark the method live if the self is used
518+
return self.live_symbols.contains(&local_def_id);
519+
}
520+
521+
if let Some(trait_item_id) = self.tcx.associated_item(impl_item_id).trait_item_def_id
522+
&& let Some(local_id) = trait_item_id.as_local()
523+
{
524+
// for the private method, we can know the trait item is used or not,
525+
// so we mark the method live if the self is used and the trait item is used
526+
return self.live_symbols.contains(&local_id)
527+
&& self.live_symbols.contains(&local_def_id);
528+
}
515529
}
530+
false
516531
}
517532
}
518533

@@ -746,20 +761,22 @@ fn check_item<'tcx>(
746761
matches!(fn_sig.decl.implicit_self, hir::ImplicitSelfKind::None);
747762
}
748763

749-
// for impl trait blocks, mark associate functions live if the trait is public
764+
// for trait impl blocks,
765+
// mark the method live if the self_ty is public,
766+
// or the method is public and may construct self
750767
if of_trait
751768
&& (!matches!(tcx.def_kind(local_def_id), DefKind::AssocFn)
752769
|| tcx.visibility(local_def_id).is_public()
753770
&& (ty_is_pub || may_construct_self))
754771
{
755772
worklist.push((local_def_id, ComesFromAllowExpect::No));
756-
} else if of_trait && tcx.visibility(local_def_id).is_public() {
757-
// pub method && private ty & methods not construct self
758-
unsolved_impl_items.push((id, local_def_id));
759773
} else if let Some(comes_from_allow) =
760774
has_allow_dead_code_or_lang_attr(tcx, local_def_id)
761775
{
762776
worklist.push((local_def_id, comes_from_allow));
777+
} else if of_trait {
778+
// private method || public method not constructs self
779+
unsolved_impl_items.push((id, local_def_id));
763780
}
764781
}
765782
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![deny(dead_code)]
2+
3+
struct Used;
4+
struct Unused; //~ ERROR struct `Unused` is never constructed
5+
6+
pub trait PubTrait {
7+
fn foo(&self) -> Self;
8+
}
9+
10+
impl PubTrait for Used {
11+
fn foo(&self) -> Self { Used }
12+
}
13+
14+
impl PubTrait for Unused {
15+
fn foo(&self) -> Self { Unused }
16+
}
17+
18+
trait PriTrait {
19+
fn foo(&self) -> Self;
20+
}
21+
22+
impl PriTrait for Used {
23+
fn foo(&self) -> Self { Used }
24+
}
25+
26+
impl PriTrait for Unused {
27+
fn foo(&self) -> Self { Unused }
28+
}
29+
30+
fn main() {
31+
let t = Used;
32+
let _t = <Used as PubTrait>::foo(&t);
33+
let _t = <Used as PriTrait>::foo(&t);
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: struct `Unused` is never constructed
2+
--> $DIR/unused-adt-impls-trait.rs:4:8
3+
|
4+
LL | struct Unused;
5+
| ^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-adt-impls-trait.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/rust-2021/inherent-dyn-collision.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod inner {
2525
// having a struct of the same name as the trait in-scope, while *also*
2626
// implementing the trait for that struct but **without** importing the
2727
// trait itself into scope
28+
#[allow(dead_code)]
2829
struct TryIntoU32;
2930

3031
impl super::TryIntoU32 for TryIntoU32 {

tests/ui/rust-2021/inherent-dyn-collision.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod inner {
2525
// having a struct of the same name as the trait in-scope, while *also*
2626
// implementing the trait for that struct but **without** importing the
2727
// trait itself into scope
28+
#[allow(dead_code)]
2829
struct TryIntoU32;
2930

3031
impl super::TryIntoU32 for TryIntoU32 {

tests/ui/rust-2021/inherent-dyn-collision.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: trait method `try_into` will become ambiguous in Rust 2021
2-
--> $DIR/inherent-dyn-collision.rs:41:9
2+
--> $DIR/inherent-dyn-collision.rs:42:9
33
|
44
LL | get_dyn_trait().try_into().unwrap()
55
| ^^^^^^^^^^^^^^^ help: disambiguate the method call: `(&*get_dyn_trait())`

0 commit comments

Comments
 (0)