Skip to content

Commit 2724aea

Browse files
authored
Rollup merge of rust-lang#126618 - mu001999-contrib:dead/enhance, r=pnkfelix
Mark assoc tys live only if the corresponding trait is live r? ````@pnkfelix````
2 parents d929a42 + a264bff commit 2724aea

File tree

9 files changed

+75
-36
lines changed

9 files changed

+75
-36
lines changed

compiler/rustc_passes/src/dead.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
155155

156156
fn handle_res(&mut self, res: Res) {
157157
match res {
158-
Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::TyAlias, def_id) => {
158+
Res::Def(
159+
DefKind::Const | DefKind::AssocConst | DefKind::AssocTy | DefKind::TyAlias,
160+
def_id,
161+
) => {
159162
self.check_def_id(def_id);
160163
}
161164
_ if self.in_pat => {}
@@ -441,7 +444,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
441444
intravisit::walk_item(self, item)
442445
}
443446
hir::ItemKind::ForeignMod { .. } => {}
444-
hir::ItemKind::Trait(..) => {
447+
hir::ItemKind::Trait(_, _, _, _, trait_item_refs) => {
445448
for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {
446449
if let Some(local_def_id) = impl_def_id.as_local()
447450
&& let ItemKind::Impl(impl_ref) =
@@ -454,7 +457,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
454457
intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);
455458
}
456459
}
457-
460+
// mark assoc ty live if the trait is live
461+
for trait_item in trait_item_refs {
462+
if let hir::AssocItemKind::Type = trait_item.kind {
463+
self.check_def_id(trait_item.id.owner_id.to_def_id());
464+
}
465+
}
458466
intravisit::walk_item(self, item)
459467
}
460468
_ => intravisit::walk_item(self, item),
@@ -471,9 +479,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
471479
&& let ItemKind::Impl(impl_ref) =
472480
self.tcx.hir().expect_item(local_impl_id).kind
473481
{
474-
if !matches!(trait_item.kind, hir::TraitItemKind::Type(..))
475-
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
476-
.ty_and_all_fields_are_public
482+
if !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
483+
.ty_and_all_fields_are_public
477484
{
478485
// skip impl-items of non pure pub ty,
479486
// cause we don't know the ty is constructed or not,
@@ -812,9 +819,8 @@ fn check_item<'tcx>(
812819
// for trait impl blocks,
813820
// mark the method live if the self_ty is public,
814821
// or the method is public and may construct self
815-
if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy)
816-
|| tcx.visibility(local_def_id).is_public()
817-
&& (ty_and_all_fields_are_public || may_construct_self)
822+
if tcx.visibility(local_def_id).is_public()
823+
&& (ty_and_all_fields_are_public || may_construct_self)
818824
{
819825
// if the impl item is public,
820826
// and the ty may be constructed or can be constructed in foreign crates,
@@ -851,10 +857,13 @@ fn check_trait_item(
851857
worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
852858
id: hir::TraitItemId,
853859
) {
854-
use hir::TraitItemKind::{Const, Fn};
855-
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
860+
use hir::TraitItemKind::{Const, Fn, Type};
861+
if matches!(
862+
tcx.def_kind(id.owner_id),
863+
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn
864+
) {
856865
let trait_item = tcx.hir().trait_item(id);
857-
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(..))
866+
if matches!(trait_item.kind, Const(_, Some(_)) | Type(_, Some(_)) | Fn(..))
858867
&& let Some(comes_from_allow) =
859868
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
860869
{
@@ -896,7 +905,7 @@ fn create_and_seed_worklist(
896905
// checks impls, impl-items and pub structs with all public fields later
897906
match tcx.def_kind(id) {
898907
DefKind::Impl { .. } => false,
899-
DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
908+
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
900909
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
901910
_ => true
902911
})
@@ -1183,6 +1192,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11831192
}
11841193
match self.tcx.def_kind(def_id) {
11851194
DefKind::AssocConst
1195+
| DefKind::AssocTy
11861196
| DefKind::AssocFn
11871197
| DefKind::Fn
11881198
| DefKind::Static { .. }
@@ -1224,15 +1234,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
12241234
|| (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
12251235
{
12261236
for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {
1227-
// We have diagnosed unused assoc consts and fns in traits
1237+
// We have diagnosed unused assocs in traits
12281238
if matches!(def_kind, DefKind::Impl { of_trait: true })
1229-
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn)
1239+
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn)
12301240
// skip unused public inherent methods,
12311241
// cause we have diagnosed unconstructed struct
12321242
|| matches!(def_kind, DefKind::Impl { of_trait: false })
12331243
&& tcx.visibility(def_id).is_public()
12341244
&& ty_ref_to_pub_struct(tcx, tcx.hir().item(item).expect_impl().self_ty).ty_is_public
1235-
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy
12361245
{
12371246
continue;
12381247
}

tests/ui/const-generics/cross_crate_complex.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ async fn foo() {
1111
async_in_foo(async_out_foo::<4>().await).await;
1212
}
1313

14+
#[allow(dead_code)]
1415
struct Faz<const N: usize>;
1516

1617
impl<const N: usize> Foo<N> for Faz<N> {}

tests/ui/generic-associated-types/missing-bounds.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::ops::Add;
44

5+
#[allow(dead_code)]
56
struct A<B>(B);
67

78
impl<B> Add for A<B> where B: Add<Output = B> {
@@ -12,6 +13,7 @@ impl<B> Add for A<B> where B: Add<Output = B> {
1213
}
1314
}
1415

16+
#[allow(dead_code)]
1517
struct C<B>(B);
1618

1719
impl<B: Add<Output = B>> Add for C<B> {
@@ -22,6 +24,7 @@ impl<B: Add<Output = B>> Add for C<B> {
2224
}
2325
}
2426

27+
#[allow(dead_code)]
2528
struct D<B>(B);
2629

2730
impl<B: std::ops::Add<Output = B>> Add for D<B> {
@@ -32,6 +35,7 @@ impl<B: std::ops::Add<Output = B>> Add for D<B> {
3235
}
3336
}
3437

38+
#[allow(dead_code)]
3539
struct E<B>(B);
3640

3741
impl<B: Add<Output = B>> Add for E<B> where B: Add<Output = B> {

tests/ui/generic-associated-types/missing-bounds.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use std::ops::Add;
44

5+
#[allow(dead_code)]
56
struct A<B>(B);
67

78
impl<B> Add for A<B> where B: Add {
@@ -12,6 +13,7 @@ impl<B> Add for A<B> where B: Add {
1213
}
1314
}
1415

16+
#[allow(dead_code)]
1517
struct C<B>(B);
1618

1719
impl<B: Add> Add for C<B> {
@@ -22,6 +24,7 @@ impl<B: Add> Add for C<B> {
2224
}
2325
}
2426

27+
#[allow(dead_code)]
2528
struct D<B>(B);
2629

2730
impl<B> Add for D<B> {
@@ -32,6 +35,7 @@ impl<B> Add for D<B> {
3235
}
3336
}
3437

38+
#[allow(dead_code)]
3539
struct E<B>(B);
3640

3741
impl<B: Add> Add for E<B> where <B as Add>::Output = B {

tests/ui/generic-associated-types/missing-bounds.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: equality constraints are not yet supported in `where` clauses
2-
--> $DIR/missing-bounds.rs:37:33
2+
--> $DIR/missing-bounds.rs:41:33
33
|
44
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
55
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
@@ -11,7 +11,7 @@ LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
1111
| ~~~~~~~~~~~~~~~~~~
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/missing-bounds.rs:11:11
14+
--> $DIR/missing-bounds.rs:12:11
1515
|
1616
LL | impl<B> Add for A<B> where B: Add {
1717
| - expected this type parameter
@@ -24,14 +24,14 @@ LL | A(self.0 + rhs.0)
2424
= note: expected type parameter `B`
2525
found associated type `<B as Add>::Output`
2626
help: the type constructed contains `<B as Add>::Output` due to the type of the argument passed
27-
--> $DIR/missing-bounds.rs:11:9
27+
--> $DIR/missing-bounds.rs:12:9
2828
|
2929
LL | A(self.0 + rhs.0)
3030
| ^^--------------^
3131
| |
3232
| this argument influences the type of `A`
3333
note: tuple struct defined here
34-
--> $DIR/missing-bounds.rs:5:8
34+
--> $DIR/missing-bounds.rs:6:8
3535
|
3636
LL | struct A<B>(B);
3737
| ^
@@ -41,7 +41,7 @@ LL | impl<B> Add for A<B> where B: Add<Output = B> {
4141
| ++++++++++++
4242

4343
error[E0308]: mismatched types
44-
--> $DIR/missing-bounds.rs:21:14
44+
--> $DIR/missing-bounds.rs:23:14
4545
|
4646
LL | impl<B: Add> Add for C<B> {
4747
| - expected this type parameter
@@ -54,7 +54,7 @@ LL | Self(self.0 + rhs.0)
5454
= note: expected type parameter `B`
5555
found associated type `<B as Add>::Output`
5656
note: tuple struct defined here
57-
--> $DIR/missing-bounds.rs:15:8
57+
--> $DIR/missing-bounds.rs:17:8
5858
|
5959
LL | struct C<B>(B);
6060
| ^
@@ -64,7 +64,7 @@ LL | impl<B: Add<Output = B>> Add for C<B> {
6464
| ++++++++++++
6565

6666
error[E0369]: cannot add `B` to `B`
67-
--> $DIR/missing-bounds.rs:31:21
67+
--> $DIR/missing-bounds.rs:34:21
6868
|
6969
LL | Self(self.0 + rhs.0)
7070
| ------ ^ ----- B
@@ -77,7 +77,7 @@ LL | impl<B: std::ops::Add<Output = B>> Add for D<B> {
7777
| +++++++++++++++++++++++++++
7878

7979
error[E0308]: mismatched types
80-
--> $DIR/missing-bounds.rs:42:14
80+
--> $DIR/missing-bounds.rs:46:14
8181
|
8282
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
8383
| - expected this type parameter
@@ -90,7 +90,7 @@ LL | Self(self.0 + rhs.0)
9090
= note: expected type parameter `B`
9191
found associated type `<B as Add>::Output`
9292
note: tuple struct defined here
93-
--> $DIR/missing-bounds.rs:35:8
93+
--> $DIR/missing-bounds.rs:39:8
9494
|
9595
LL | struct E<B>(B);
9696
| ^
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![deny(dead_code)]
2+
3+
struct T1; //~ ERROR struct `T1` is never constructed
4+
5+
trait Foo { type Unused; } //~ ERROR trait `Foo` is never used
6+
impl Foo for T1 { type Unused = Self; }
7+
8+
pub trait Bar { type Used; }
9+
impl Bar for T1 { type Used = Self; }
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: struct `T1` is never constructed
2+
--> $DIR/unused-trait-with-assoc-ty.rs:3:8
3+
|
4+
LL | struct T1;
5+
| ^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused-trait-with-assoc-ty.rs:1:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: trait `Foo` is never used
14+
--> $DIR/unused-trait-with-assoc-ty.rs:5:7
15+
|
16+
LL | trait Foo { type Unused; }
17+
| ^^^
18+
19+
error: aborting due to 2 previous errors
20+

tests/ui/pattern/issue-22546.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<T: ::std::fmt::Display> Foo<T> {
1515
}
1616
}
1717

18-
trait Tr { //~ WARN trait `Tr` is never used
18+
trait Tr {
1919
type U;
2020
}
2121

tests/ui/pattern/issue-22546.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)