Skip to content

Commit af4b5be

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

File tree

8 files changed

+92
-15
lines changed

8 files changed

+92
-15
lines changed

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,11 @@ rustc_queries! {
819819
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
820820
}
821821

822+
query impl_item_trait_item_ids(impl_id: DefId) -> &'tcx DefIdMap<DefId> {
823+
arena_cache
824+
desc { |tcx| "comparing impl items against trait for `{}`", tcx.def_path_str(impl_id) }
825+
}
826+
822827
/// Given `fn_def_id` of a trait or of an impl that implements a given trait:
823828
/// if `fn_def_id` is the def id of a function defined inside a trait, then it creates and returns
824829
/// the associated items that correspond to each impl trait in return position for that trait.

compiler/rustc_passes/src/dead.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ 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
{
434433
continue;
@@ -487,32 +486,47 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
487486

488487
fn solve_rest_impl_items(&mut self, mut unsolved_impl_items: Vec<(hir::ItemId, LocalDefId)>) {
489488
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));
489+
(ready, unsolved_impl_items) =
490+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
491+
self.impl_item_with_used_self_and_trait_term(impl_id, impl_item_id)
492+
});
493493

494494
while !ready.is_empty() {
495495
self.worklist =
496496
ready.into_iter().map(|(_, id)| (id, ComesFromAllowExpect::No)).collect();
497497
self.mark_live_symbols();
498498

499-
(ready, unsolved_impl_items) = unsolved_impl_items
500-
.into_iter()
501-
.partition(|&(impl_id, _)| self.impl_item_with_used_self(impl_id));
499+
(ready, unsolved_impl_items) =
500+
unsolved_impl_items.into_iter().partition(|&(impl_id, impl_item_id)| {
501+
self.impl_item_with_used_self_and_trait_term(impl_id, impl_item_id)
502+
});
502503
}
503504
}
504505

505-
fn impl_item_with_used_self(&mut self, impl_id: hir::ItemId) -> bool {
506+
fn impl_item_with_used_self_and_trait_term(
507+
&mut self,
508+
impl_id: hir::ItemId,
509+
impl_item_id: LocalDefId,
510+
) -> bool {
506511
if let TyKind::Path(hir::QPath::Resolved(_, path)) =
507512
self.tcx.hir().item(impl_id).expect_impl().self_ty.kind
508513
&& let Res::Def(def_kind, def_id) = path.res
509514
&& let Some(local_def_id) = def_id.as_local()
510515
&& matches!(def_kind, DefKind::Struct | DefKind::Enum | DefKind::Union)
511516
{
512-
self.live_symbols.contains(&local_def_id)
513-
} else {
514-
false
517+
if self.tcx.visibility(impl_item_id).is_public() {
518+
return self.live_symbols.contains(&local_def_id);
519+
} else if let Some(trait_item_id) = self
520+
.tcx
521+
.impl_item_trait_item_ids(impl_id.owner_id.to_def_id())
522+
.get(&impl_item_id.to_def_id())
523+
&& let Some(local_id) = trait_item_id.as_local()
524+
{
525+
return self.live_symbols.contains(&local_id)
526+
&& self.live_symbols.contains(&local_def_id);
527+
}
515528
}
529+
false
516530
}
517531
}
518532

@@ -753,8 +767,8 @@ fn check_item<'tcx>(
753767
&& (ty_is_pub || may_construct_self))
754768
{
755769
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
770+
} else if of_trait {
771+
// private ty & methods not construct self
758772
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)

compiler/rustc_ty_utils/src/assoc.rs

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub(crate) fn provide(providers: &mut Providers) {
1515
associated_types_for_impl_traits_in_associated_fn,
1616
associated_type_for_impl_trait_in_trait,
1717
impl_item_implementor_ids,
18+
impl_item_trait_item_ids,
1819
..*providers
1920
};
2021
}
@@ -92,6 +93,13 @@ fn impl_item_implementor_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId>
9293
.collect()
9394
}
9495

96+
fn impl_item_trait_item_ids(tcx: TyCtxt<'_>, impl_id: DefId) -> DefIdMap<DefId> {
97+
tcx.associated_items(impl_id)
98+
.in_definition_order()
99+
.filter_map(|item| item.trait_item_def_id.map(|trait_item| (item.def_id, trait_item)))
100+
.collect()
101+
}
102+
95103
fn associated_item(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AssocItem {
96104
let id = tcx.local_def_id_to_hir_id(def_id);
97105
let parent_def_id = tcx.hir().get_parent_item(id);
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)