Skip to content

Commit 74f21dd

Browse files
Rollup merge of rust-lang#144113 - mu001999-contrib:dead-code/allow-trait, r=jdonszelmann
Impls and impl items inherit `dead_code` lint level of the corresponding traits and trait items https://github.com/rust-lang/rust/blob/master/compiler/rustc_passes/src/dead.rs#L360-L361 won't insert assoc items into the live set, so that impl items cannot be marked live. This PR lets impls and impl items can inherit lint levels of the corresponding traits and trait items. Fixes rust-lang#144060 r? ```@petrochenkov```
2 parents da476f1 + 12474ce commit 74f21dd

4 files changed

Lines changed: 110 additions & 0 deletions

File tree

compiler/rustc_passes/src/dead.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,15 @@ fn maybe_record_as_seed<'tcx>(
778778
match tcx.def_kind(parent) {
779779
DefKind::Impl { of_trait: false } | DefKind::Trait => {}
780780
DefKind::Impl { of_trait: true } => {
781+
if let Some(trait_item_def_id) =
782+
tcx.associated_item(owner_id.def_id).trait_item_def_id()
783+
&& let Some(trait_item_local_def_id) = trait_item_def_id.as_local()
784+
&& let Some(comes_from_allow) =
785+
has_allow_dead_code_or_lang_attr(tcx, trait_item_local_def_id)
786+
{
787+
worklist.push((owner_id.def_id, comes_from_allow));
788+
}
789+
781790
// We only care about associated items of traits,
782791
// because they cannot be visited directly,
783792
// so we later mark them as live if their corresponding traits
@@ -791,6 +800,14 @@ fn maybe_record_as_seed<'tcx>(
791800
}
792801
DefKind::Impl { of_trait: true } => {
793802
if allow_dead_code.is_none() {
803+
if let Some(trait_def_id) =
804+
tcx.impl_trait_ref(owner_id.def_id).skip_binder().def_id.as_local()
805+
&& let Some(comes_from_allow) =
806+
has_allow_dead_code_or_lang_attr(tcx, trait_def_id)
807+
{
808+
worklist.push((owner_id.def_id, comes_from_allow));
809+
}
810+
794811
unsolved_items.push(owner_id.def_id);
795812
}
796813
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![deny(dead_code)]
2+
3+
pub mod a {
4+
pub trait Foo { }
5+
impl Foo for u32 { }
6+
7+
struct PrivateType; //~ ERROR struct `PrivateType` is never constructed
8+
impl Foo for PrivateType { } // <-- warns as dead, even though Foo is public
9+
10+
struct AnotherPrivateType; //~ ERROR struct `AnotherPrivateType` is never constructed
11+
impl Foo for AnotherPrivateType { } // <-- warns as dead, even though Foo is public
12+
}
13+
14+
pub mod b {
15+
#[allow(dead_code)]
16+
pub trait Foo { }
17+
impl Foo for u32 { }
18+
19+
struct PrivateType;
20+
impl Foo for PrivateType { } // <-- no warning, trait is "allowed"
21+
22+
struct AnotherPrivateType;
23+
impl Foo for AnotherPrivateType { } // <-- no warning, trait is "allowed"
24+
}
25+
26+
pub mod c {
27+
pub trait Foo { }
28+
impl Foo for u32 { }
29+
30+
struct PrivateType;
31+
#[allow(dead_code)]
32+
impl Foo for PrivateType { } // <-- no warning, impl is allowed
33+
34+
struct AnotherPrivateType; //~ ERROR struct `AnotherPrivateType` is never constructed
35+
impl Foo for AnotherPrivateType { } // <-- warns as dead, even though Foo is public
36+
}
37+
38+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: struct `PrivateType` is never constructed
2+
--> $DIR/allow-trait-or-impl.rs:7:12
3+
|
4+
LL | struct PrivateType;
5+
| ^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/allow-trait-or-impl.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: struct `AnotherPrivateType` is never constructed
14+
--> $DIR/allow-trait-or-impl.rs:10:12
15+
|
16+
LL | struct AnotherPrivateType;
17+
| ^^^^^^^^^^^^^^^^^^
18+
19+
error: struct `AnotherPrivateType` is never constructed
20+
--> $DIR/allow-trait-or-impl.rs:34:12
21+
|
22+
LL | struct AnotherPrivateType;
23+
| ^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 3 previous errors
26+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ check-pass
2+
3+
#![deny(dead_code)]
4+
5+
#[allow(dead_code)]
6+
trait Foo {
7+
const FOO: u32;
8+
type Baz;
9+
fn foobar();
10+
}
11+
12+
const fn bar(x: u32) -> u32 {
13+
x
14+
}
15+
16+
struct Qux;
17+
18+
struct FooBar;
19+
20+
impl Foo for u32 {
21+
const FOO: u32 = bar(0);
22+
type Baz = Qux;
23+
24+
fn foobar() {
25+
let _ = FooBar;
26+
}
27+
}
28+
29+
fn main() {}

0 commit comments

Comments
 (0)