Skip to content

Commit d4f6f9e

Browse files
committed
Auto merge of #118257 - mu001999:dead_code/trait, r=cjgillot
Make traits / trait methods detected by the dead code lint Fixes #118139 and #41883
2 parents 586893c + c7519d4 commit d4f6f9e

File tree

279 files changed

+1185
-254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

279 files changed

+1185
-254
lines changed

compiler/rustc_passes/src/dead.rs

+71-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// is dead.
55

66
use hir::def_id::{LocalDefIdMap, LocalDefIdSet};
7+
use hir::ItemKind;
78
use rustc_data_structures::unord::UnordSet;
89
use rustc_errors::MultiSpan;
910
use rustc_hir as hir;
@@ -14,7 +15,7 @@ use rustc_hir::{Node, PatKind, TyKind};
1415
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1516
use rustc_middle::middle::privacy::Level;
1617
use rustc_middle::query::Providers;
17-
use rustc_middle::ty::{self, TyCtxt};
18+
use rustc_middle::ty::{self, TyCtxt, Visibility};
1819
use rustc_session::lint;
1920
use rustc_session::lint::builtin::DEAD_CODE;
2021
use rustc_span::symbol::{sym, Symbol};
@@ -381,9 +382,46 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
381382
intravisit::walk_item(self, item)
382383
}
383384
hir::ItemKind::ForeignMod { .. } => {}
385+
hir::ItemKind::Trait(..) => {
386+
for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {
387+
if let Some(local_def_id) = impl_def_id.as_local()
388+
&& let ItemKind::Impl(impl_ref) =
389+
self.tcx.hir().expect_item(local_def_id).kind
390+
{
391+
// skip items
392+
// mark dependent traits live
393+
intravisit::walk_generics(self, impl_ref.generics);
394+
// mark dependent parameters live
395+
intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);
396+
}
397+
}
398+
399+
intravisit::walk_item(self, item)
400+
}
384401
_ => intravisit::walk_item(self, item),
385402
},
386403
Node::TraitItem(trait_item) => {
404+
// mark corresponing ImplTerm live
405+
let trait_item_id = trait_item.owner_id.to_def_id();
406+
if let Some(trait_id) = self.tcx.trait_of_item(trait_item_id) {
407+
// mark the trait live
408+
self.check_def_id(trait_id);
409+
410+
for impl_id in self.tcx.all_impls(trait_id) {
411+
if let Some(local_impl_id) = impl_id.as_local()
412+
&& let ItemKind::Impl(impl_ref) =
413+
self.tcx.hir().expect_item(local_impl_id).kind
414+
{
415+
// mark self_ty live
416+
intravisit::walk_ty(self, impl_ref.self_ty);
417+
if let Some(&impl_item_id) =
418+
self.tcx.impl_item_implementor_ids(impl_id).get(&trait_item_id)
419+
{
420+
self.check_def_id(impl_item_id);
421+
}
422+
}
423+
}
424+
}
387425
intravisit::walk_trait_item(self, trait_item);
388426
}
389427
Node::ImplItem(impl_item) => {
@@ -636,10 +674,6 @@ fn check_item<'tcx>(
636674
}
637675
}
638676
DefKind::Impl { of_trait } => {
639-
if of_trait {
640-
worklist.push((id.owner_id.def_id, ComesFromAllowExpect::No));
641-
}
642-
643677
// get DefIds from another query
644678
let local_def_ids = tcx
645679
.associated_item_def_ids(id.owner_id)
@@ -648,7 +682,11 @@ fn check_item<'tcx>(
648682

649683
// And we access the Map here to get HirId from LocalDefId
650684
for id in local_def_ids {
651-
if of_trait {
685+
// for impl trait blocks, mark associate functions live if the trait is public
686+
if of_trait
687+
&& (!matches!(tcx.def_kind(id), DefKind::AssocFn)
688+
|| tcx.local_visibility(id) == Visibility::Public)
689+
{
652690
worklist.push((id, ComesFromAllowExpect::No));
653691
} else if let Some(comes_from_allow) = has_allow_dead_code_or_lang_attr(tcx, id) {
654692
worklist.push((id, comes_from_allow));
@@ -679,7 +717,7 @@ fn check_trait_item(
679717
use hir::TraitItemKind::{Const, Fn};
680718
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
681719
let trait_item = tcx.hir().trait_item(id);
682-
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(_, hir::TraitFn::Provided(_)))
720+
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(..))
683721
&& let Some(comes_from_allow) =
684722
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
685723
{
@@ -948,7 +986,8 @@ impl<'tcx> DeadVisitor<'tcx> {
948986
| DefKind::TyAlias
949987
| DefKind::Enum
950988
| DefKind::Union
951-
| DefKind::ForeignTy => self.warn_dead_code(def_id, "used"),
989+
| DefKind::ForeignTy
990+
| DefKind::Trait => self.warn_dead_code(def_id, "used"),
952991
DefKind::Struct => self.warn_dead_code(def_id, "constructed"),
953992
DefKind::Variant | DefKind::Field => bug!("should be handled specially"),
954993
_ => {}
@@ -973,18 +1012,33 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
9731012
let module_items = tcx.hir_module_items(module);
9741013

9751014
for item in module_items.items() {
976-
if let hir::ItemKind::Impl(impl_item) = tcx.hir().item(item).kind {
977-
let mut dead_items = Vec::new();
978-
for item in impl_item.items {
979-
let def_id = item.id.owner_id.def_id;
980-
if !visitor.is_live_code(def_id) {
981-
let name = tcx.item_name(def_id.to_def_id());
982-
let level = visitor.def_lint_level(def_id);
1015+
let def_kind = tcx.def_kind(item.owner_id);
9831016

984-
dead_items.push(DeadItem { def_id, name, level })
1017+
let mut dead_codes = Vec::new();
1018+
// if we have diagnosed the trait, do not diagnose unused methods
1019+
if matches!(def_kind, DefKind::Impl { .. })
1020+
|| (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
1021+
{
1022+
for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {
1023+
// We have diagnosed unused methods in traits
1024+
if matches!(def_kind, DefKind::Impl { of_trait: true })
1025+
&& tcx.def_kind(def_id) == DefKind::AssocFn
1026+
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) != DefKind::AssocFn
1027+
{
1028+
continue;
1029+
}
1030+
1031+
if let Some(local_def_id) = def_id.as_local()
1032+
&& !visitor.is_live_code(local_def_id)
1033+
{
1034+
let name = tcx.item_name(def_id);
1035+
let level = visitor.def_lint_level(local_def_id);
1036+
dead_codes.push(DeadItem { def_id: local_def_id, name, level });
9851037
}
9861038
}
987-
visitor.warn_multiple(item.owner_id.def_id, "used", dead_items, ReportOn::NamedField);
1039+
}
1040+
if !dead_codes.is_empty() {
1041+
visitor.warn_multiple(item.owner_id.def_id, "used", dead_codes, ReportOn::NamedField);
9881042
}
9891043

9901044
if !live_symbols.contains(&item.owner_id.def_id) {
@@ -997,7 +1051,6 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
9971051
continue;
9981052
}
9991053

1000-
let def_kind = tcx.def_kind(item.owner_id);
10011054
if let DefKind::Struct | DefKind::Union | DefKind::Enum = def_kind {
10021055
let adt = tcx.adt_def(item.owner_id);
10031056
let mut dead_variants = Vec::new();
@@ -1044,8 +1097,6 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
10441097
for foreign_item in module_items.foreign_items() {
10451098
visitor.check_definition(foreign_item.owner_id.def_id);
10461099
}
1047-
1048-
// We do not warn trait items.
10491100
}
10501101

10511102
pub(crate) fn provide(providers: &mut Providers) {

compiler/rustc_span/src/source_map/tests.rs

+2-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use super::*;
22

3+
use rustc_data_structures::sync::FreezeLock;
4+
35
fn init_source_map() -> SourceMap {
46
let sm = SourceMap::new(FilePathMapping::empty());
57
sm.new_source_file(PathBuf::from("blork.rs").into(), "first line.\nsecond line".to_string());
@@ -263,53 +265,6 @@ fn t10() {
263265
);
264266
}
265267

266-
/// Returns the span corresponding to the `n`th occurrence of `substring` in `source_text`.
267-
trait SourceMapExtension {
268-
fn span_substr(
269-
&self,
270-
file: &Lrc<SourceFile>,
271-
source_text: &str,
272-
substring: &str,
273-
n: usize,
274-
) -> Span;
275-
}
276-
277-
impl SourceMapExtension for SourceMap {
278-
fn span_substr(
279-
&self,
280-
file: &Lrc<SourceFile>,
281-
source_text: &str,
282-
substring: &str,
283-
n: usize,
284-
) -> Span {
285-
eprintln!(
286-
"span_substr(file={:?}/{:?}, substring={:?}, n={})",
287-
file.name, file.start_pos, substring, n
288-
);
289-
let mut i = 0;
290-
let mut hi = 0;
291-
loop {
292-
let offset = source_text[hi..].find(substring).unwrap_or_else(|| {
293-
panic!(
294-
"source_text `{}` does not have {} occurrences of `{}`, only {}",
295-
source_text, n, substring, i
296-
);
297-
});
298-
let lo = hi + offset;
299-
hi = lo + substring.len();
300-
if i == n {
301-
let span = Span::with_root_ctxt(
302-
BytePos(lo as u32 + file.start_pos.0),
303-
BytePos(hi as u32 + file.start_pos.0),
304-
);
305-
assert_eq!(&self.span_to_snippet(span).unwrap()[..], substring);
306-
return span;
307-
}
308-
i += 1;
309-
}
310-
}
311-
}
312-
313268
// Takes a unix-style path and returns a platform specific path.
314269
fn path(p: &str) -> PathBuf {
315270
path_str(p).into()

library/core/tests/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[allow(dead_code)]
12
trait Trait {
23
fn blah(&self);
34
}

library/std/src/sys_common/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ cfg_if::cfg_if! {
5959

6060
/// A trait for viewing representations from std types
6161
#[doc(hidden)]
62+
#[allow(dead_code)] // not used on all platforms
6263
pub trait AsInner<Inner: ?Sized> {
6364
fn as_inner(&self) -> &Inner;
6465
}
6566

6667
/// A trait for viewing representations from std types
6768
#[doc(hidden)]
69+
#[allow(dead_code)] // not used on all platforms
6870
pub trait AsInnerMut<Inner: ?Sized> {
6971
fn as_inner_mut(&mut self) -> &mut Inner;
7072
}

src/tools/miri/tests/fail/dyn-call-trait-mismatch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
trait T1 {
2+
#[allow(dead_code)]
23
fn method1(self: Box<Self>);
34
}
45
trait T2 {

src/tools/miri/tests/fail/dyn-upcast-trait-mismatch.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,36 @@
22
#![allow(incomplete_features)]
33

44
trait Foo: PartialEq<i32> + std::fmt::Debug + Send + Sync {
5+
#[allow(dead_code)]
56
fn a(&self) -> i32 {
67
10
78
}
89

10+
#[allow(dead_code)]
911
fn z(&self) -> i32 {
1012
11
1113
}
1214

15+
#[allow(dead_code)]
1316
fn y(&self) -> i32 {
1417
12
1518
}
1619
}
1720

1821
trait Bar: Foo {
22+
#[allow(dead_code)]
1923
fn b(&self) -> i32 {
2024
20
2125
}
2226

27+
#[allow(dead_code)]
2328
fn w(&self) -> i32 {
2429
21
2530
}
2631
}
2732

2833
trait Baz: Bar {
34+
#[allow(dead_code)]
2935
fn c(&self) -> i32 {
3036
30
3137
}

src/tools/miri/tests/pass/cast-rfc0401-vtable-kinds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ trait Foo<T> {
99
}
1010
}
1111

12+
#[allow(dead_code)]
1213
trait Bar {
1314
fn bar(&self) {
1415
println!("Bar!");

src/tools/miri/tests/pass/dyn-upcast.rs

+3
Original file line numberDiff line numberDiff line change
@@ -383,14 +383,17 @@ fn struct_() {
383383

384384
fn replace_vptr() {
385385
trait A {
386+
#[allow(dead_code)]
386387
fn foo_a(&self);
387388
}
388389

389390
trait B {
391+
#[allow(dead_code)]
390392
fn foo_b(&self);
391393
}
392394

393395
trait C: A + B {
396+
#[allow(dead_code)]
394397
fn foo_c(&self);
395398
}
396399

src/tools/miri/tests/pass/weak_memory/weak.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::sync::atomic::Ordering::*;
1111
use std::sync::atomic::{fence, AtomicUsize};
1212
use std::thread::spawn;
1313

14+
#[allow(dead_code)]
1415
#[derive(Copy, Clone)]
1516
struct EvilSend<T>(pub T);
1617

src/tools/tidy/src/ui_tests.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ use std::path::{Path, PathBuf};
1313
// desirable, because large numbers of files are unwieldy in general. See issue
1414
// #73494.
1515
const ENTRY_LIMIT: usize = 900;
16-
const ISSUES_ENTRY_LIMIT: usize = 1807;
17-
const ROOT_ENTRY_LIMIT: usize = 868;
16+
// FIXME: The following limits should be reduced eventually.
17+
const ISSUES_ENTRY_LIMIT: usize = 1819;
18+
const ROOT_ENTRY_LIMIT: usize = 870;
1819

1920
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
2021
"rs", // test source files

tests/codegen-units/item-collection/instantiation-through-vtable.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ fn start(_: isize, _: *const *const u8) -> isize {
2626
//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct<u32>> - shim(None) @@ instantiation_through_vtable-cgu.0[Internal]
2727
//~ MONO_ITEM fn <Struct<u32> as Trait>::foo
2828
//~ MONO_ITEM fn <Struct<u32> as Trait>::bar
29-
let _ = &s1 as &Trait;
29+
let r1 = &s1 as &Trait;
30+
r1.foo();
31+
r1.bar();
3032

3133
let s1 = Struct { _a: 0u64 };
3234
//~ MONO_ITEM fn std::ptr::drop_in_place::<Struct<u64>> - shim(None) @@ instantiation_through_vtable-cgu.0[Internal]

tests/codegen-units/item-collection/trait-method-default-impl.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ fn start(_: isize, _: *const *const u8) -> isize {
5757
//~ MONO_ITEM fn <u32 as SomeGenericTrait<i16>>::bar::<()>
5858
0u32.bar(0i16, ());
5959

60+
0i8.foo();
61+
0i32.foo();
62+
6063
0
6164
}

tests/codegen-units/item-collection/unsizing.rs

+2
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,7 @@ fn start(_: isize, _: *const *const u8) -> isize {
7575
//~ MONO_ITEM fn <u32 as Trait>::foo
7676
let _wrapper_sized = wrapper_sized as Wrapper<Trait>;
7777

78+
false.foo();
79+
7880
0
7981
}

tests/ui-fulldeps/rustc_encodable_hygiene.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// run-pass
1+
// check-pass
22

33
#![feature(rustc_private)]
44

tests/ui/anon-params/anon-params-deprecated.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// edition:2015
66
// run-rustfix
77

8+
#[allow(dead_code)]
89
trait T {
910
fn foo(_: i32); //~ WARNING anonymous parameters are deprecated
1011
//~| WARNING this is accepted in the current edition

tests/ui/anon-params/anon-params-deprecated.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// edition:2015
66
// run-rustfix
77

8+
#[allow(dead_code)]
89
trait T {
910
fn foo(i32); //~ WARNING anonymous parameters are deprecated
1011
//~| WARNING this is accepted in the current edition

0 commit comments

Comments
 (0)