Skip to content

Commit be87443

Browse files
authored
Unrolled build for rust-lang#126618
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 fda509e + a264bff commit be87443

File tree

9 files changed

+75
-36
lines changed

9 files changed

+75
-36
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 25 additions & 16 deletions
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 => {}
@@ -466,7 +469,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
466469
intravisit::walk_item(self, item)
467470
}
468471
hir::ItemKind::ForeignMod { .. } => {}
469-
hir::ItemKind::Trait(..) => {
472+
hir::ItemKind::Trait(_, _, _, _, trait_item_refs) => {
470473
for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {
471474
if let Some(local_def_id) = impl_def_id.as_local()
472475
&& let ItemKind::Impl(impl_ref) =
@@ -479,7 +482,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
479482
intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);
480483
}
481484
}
482-
485+
// mark assoc ty live if the trait is live
486+
for trait_item in trait_item_refs {
487+
if let hir::AssocItemKind::Type = trait_item.kind {
488+
self.check_def_id(trait_item.id.owner_id.to_def_id());
489+
}
490+
}
483491
intravisit::walk_item(self, item)
484492
}
485493
_ => intravisit::walk_item(self, item),
@@ -496,9 +504,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
496504
&& let ItemKind::Impl(impl_ref) =
497505
self.tcx.hir().expect_item(local_impl_id).kind
498506
{
499-
if !matches!(trait_item.kind, hir::TraitItemKind::Type(..))
500-
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
501-
.ty_and_all_fields_are_public
507+
if !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
508+
.ty_and_all_fields_are_public
502509
{
503510
// skip impl-items of non pure pub ty,
504511
// cause we don't know the ty is constructed or not,
@@ -837,9 +844,8 @@ fn check_item<'tcx>(
837844
// for trait impl blocks,
838845
// mark the method live if the self_ty is public,
839846
// or the method is public and may construct self
840-
if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy)
841-
|| tcx.visibility(local_def_id).is_public()
842-
&& (ty_and_all_fields_are_public || may_construct_self)
847+
if tcx.visibility(local_def_id).is_public()
848+
&& (ty_and_all_fields_are_public || may_construct_self)
843849
{
844850
// if the impl item is public,
845851
// and the ty may be constructed or can be constructed in foreign crates,
@@ -876,10 +882,13 @@ fn check_trait_item(
876882
worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
877883
id: hir::TraitItemId,
878884
) {
879-
use hir::TraitItemKind::{Const, Fn};
880-
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
885+
use hir::TraitItemKind::{Const, Fn, Type};
886+
if matches!(
887+
tcx.def_kind(id.owner_id),
888+
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn
889+
) {
881890
let trait_item = tcx.hir().trait_item(id);
882-
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(..))
891+
if matches!(trait_item.kind, Const(_, Some(_)) | Type(_, Some(_)) | Fn(..))
883892
&& let Some(comes_from_allow) =
884893
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
885894
{
@@ -921,7 +930,7 @@ fn create_and_seed_worklist(
921930
// checks impls, impl-items and pub structs with all public fields later
922931
match tcx.def_kind(id) {
923932
DefKind::Impl { .. } => false,
924-
DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
933+
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
925934
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
926935
_ => true
927936
})
@@ -1208,6 +1217,7 @@ impl<'tcx> DeadVisitor<'tcx> {
12081217
}
12091218
match self.tcx.def_kind(def_id) {
12101219
DefKind::AssocConst
1220+
| DefKind::AssocTy
12111221
| DefKind::AssocFn
12121222
| DefKind::Fn
12131223
| DefKind::Static { .. }
@@ -1249,15 +1259,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
12491259
|| (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
12501260
{
12511261
for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {
1252-
// We have diagnosed unused assoc consts and fns in traits
1262+
// We have diagnosed unused assocs in traits
12531263
if matches!(def_kind, DefKind::Impl { of_trait: true })
1254-
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn)
1264+
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn)
12551265
// skip unused public inherent methods,
12561266
// cause we have diagnosed unconstructed struct
12571267
|| matches!(def_kind, DefKind::Impl { of_trait: false })
12581268
&& tcx.visibility(def_id).is_public()
12591269
&& ty_ref_to_pub_struct(tcx, tcx.hir().item(item).expect_impl().self_ty).ty_is_public
1260-
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy
12611270
{
12621271
continue;
12631272
}

tests/ui/const-generics/cross_crate_complex.rs

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 9 additions & 9 deletions
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
| ^
Lines changed: 11 additions & 0 deletions
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() {}
Lines changed: 20 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)