Skip to content

Commit 22da616

Browse files
Revert "Rollup merge of rust-lang#126618 - mu001999-contrib:dead/enhance, r=pnkfelix"
This reverts commit 2724aea, reversing changes made to d929a42.
1 parent 5f5b4ee commit 22da616

File tree

9 files changed

+36
-75
lines changed

9 files changed

+36
-75
lines changed

compiler/rustc_passes/src/dead.rs

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

157157
fn handle_res(&mut self, res: Res) {
158158
match res {
159-
Res::Def(
160-
DefKind::Const | DefKind::AssocConst | DefKind::AssocTy | DefKind::TyAlias,
161-
def_id,
162-
) => {
159+
Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::TyAlias, def_id) => {
163160
self.check_def_id(def_id);
164161
}
165162
_ if self.in_pat => {}
@@ -470,7 +467,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
470467
intravisit::walk_item(self, item)
471468
}
472469
hir::ItemKind::ForeignMod { .. } => {}
473-
hir::ItemKind::Trait(_, _, _, _, trait_item_refs) => {
470+
hir::ItemKind::Trait(..) => {
474471
for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {
475472
if let Some(local_def_id) = impl_def_id.as_local()
476473
&& let ItemKind::Impl(impl_ref) =
@@ -483,12 +480,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
483480
intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);
484481
}
485482
}
486-
// mark assoc ty live if the trait is live
487-
for trait_item in trait_item_refs {
488-
if let hir::AssocItemKind::Type = trait_item.kind {
489-
self.check_def_id(trait_item.id.owner_id.to_def_id());
490-
}
491-
}
483+
492484
intravisit::walk_item(self, item)
493485
}
494486
_ => intravisit::walk_item(self, item),
@@ -505,8 +497,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
505497
&& let ItemKind::Impl(impl_ref) =
506498
self.tcx.hir().expect_item(local_impl_id).kind
507499
{
508-
if !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
509-
.ty_and_all_fields_are_public
500+
if !matches!(trait_item.kind, hir::TraitItemKind::Type(..))
501+
&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)
502+
.ty_and_all_fields_are_public
510503
{
511504
// skip impl-items of non pure pub ty,
512505
// cause we don't know the ty is constructed or not,
@@ -845,8 +838,9 @@ fn check_item<'tcx>(
845838
// for trait impl blocks,
846839
// mark the method live if the self_ty is public,
847840
// or the method is public and may construct self
848-
if tcx.visibility(local_def_id).is_public()
849-
&& (ty_and_all_fields_are_public || may_construct_self)
841+
if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy)
842+
|| tcx.visibility(local_def_id).is_public()
843+
&& (ty_and_all_fields_are_public || may_construct_self)
850844
{
851845
// if the impl item is public,
852846
// and the ty may be constructed or can be constructed in foreign crates,
@@ -883,13 +877,10 @@ fn check_trait_item(
883877
worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
884878
id: hir::TraitItemId,
885879
) {
886-
use hir::TraitItemKind::{Const, Fn, Type};
887-
if matches!(
888-
tcx.def_kind(id.owner_id),
889-
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn
890-
) {
880+
use hir::TraitItemKind::{Const, Fn};
881+
if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {
891882
let trait_item = tcx.hir().trait_item(id);
892-
if matches!(trait_item.kind, Const(_, Some(_)) | Type(_, Some(_)) | Fn(..))
883+
if matches!(trait_item.kind, Const(_, Some(_)) | Fn(..))
893884
&& let Some(comes_from_allow) =
894885
has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
895886
{
@@ -931,7 +922,7 @@ fn create_and_seed_worklist(
931922
// checks impls, impl-items and pub structs with all public fields later
932923
match tcx.def_kind(id) {
933924
DefKind::Impl { .. } => false,
934-
DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
925+
DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),
935926
DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
936927
_ => true
937928
})
@@ -1218,7 +1209,6 @@ impl<'tcx> DeadVisitor<'tcx> {
12181209
}
12191210
match self.tcx.def_kind(def_id) {
12201211
DefKind::AssocConst
1221-
| DefKind::AssocTy
12221212
| DefKind::AssocFn
12231213
| DefKind::Fn
12241214
| DefKind::Static { .. }
@@ -1260,14 +1250,15 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
12601250
|| (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
12611251
{
12621252
for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {
1263-
// We have diagnosed unused assocs in traits
1253+
// We have diagnosed unused assoc consts and fns in traits
12641254
if matches!(def_kind, DefKind::Impl { of_trait: true })
1265-
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn)
1255+
&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn)
12661256
// skip unused public inherent methods,
12671257
// cause we have diagnosed unconstructed struct
12681258
|| matches!(def_kind, DefKind::Impl { of_trait: false })
12691259
&& tcx.visibility(def_id).is_public()
12701260
&& ty_ref_to_pub_struct(tcx, tcx.hir().item(item).expect_impl().self_ty).ty_is_public
1261+
|| def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy
12711262
{
12721263
continue;
12731264
}

tests/ui/const-generics/cross_crate_complex.rs

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

14-
#[allow(dead_code)]
1514
struct Faz<const N: usize>;
1615

1716
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,7 +2,6 @@
22

33
use std::ops::Add;
44

5-
#[allow(dead_code)]
65
struct A<B>(B);
76

87
impl<B> Add for A<B> where B: Add<Output = B> {
@@ -13,7 +12,6 @@ impl<B> Add for A<B> where B: Add<Output = B> {
1312
}
1413
}
1514

16-
#[allow(dead_code)]
1715
struct C<B>(B);
1816

1917
impl<B: Add<Output = B>> Add for C<B> {
@@ -24,7 +22,6 @@ impl<B: Add<Output = B>> Add for C<B> {
2422
}
2523
}
2624

27-
#[allow(dead_code)]
2825
struct D<B>(B);
2926

3027
impl<B: std::ops::Add<Output = B>> Add for D<B> {
@@ -35,7 +32,6 @@ impl<B: std::ops::Add<Output = B>> Add for D<B> {
3532
}
3633
}
3734

38-
#[allow(dead_code)]
3935
struct E<B>(B);
4036

4137
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,7 +2,6 @@
22

33
use std::ops::Add;
44

5-
#[allow(dead_code)]
65
struct A<B>(B);
76

87
impl<B> Add for A<B> where B: Add {
@@ -13,7 +12,6 @@ impl<B> Add for A<B> where B: Add {
1312
}
1413
}
1514

16-
#[allow(dead_code)]
1715
struct C<B>(B);
1816

1917
impl<B: Add> Add for C<B> {
@@ -24,7 +22,6 @@ impl<B: Add> Add for C<B> {
2422
}
2523
}
2624

27-
#[allow(dead_code)]
2825
struct D<B>(B);
2926

3027
impl<B> Add for D<B> {
@@ -35,7 +32,6 @@ impl<B> Add for D<B> {
3532
}
3633
}
3734

38-
#[allow(dead_code)]
3935
struct E<B>(B);
4036

4137
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:41:33
2+
--> $DIR/missing-bounds.rs:37: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:12:11
14+
--> $DIR/missing-bounds.rs:11: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:12:9
27+
--> $DIR/missing-bounds.rs:11: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:6:8
34+
--> $DIR/missing-bounds.rs:5: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:23:14
44+
--> $DIR/missing-bounds.rs:21: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:17:8
57+
--> $DIR/missing-bounds.rs:15: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:34:21
67+
--> $DIR/missing-bounds.rs:31: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:46:14
80+
--> $DIR/missing-bounds.rs:42: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:39:8
93+
--> $DIR/missing-bounds.rs:35:8
9494
|
9595
LL | struct E<B>(B);
9696
| ^

tests/ui/lint/dead-code/unused-trait-with-assoc-ty.rs

-11
This file was deleted.

tests/ui/lint/dead-code/unused-trait-with-assoc-ty.stderr

-20
This file was deleted.

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 {
18+
trait Tr { //~ WARN trait `Tr` is never used
1919
type U;
2020
}
2121

tests/ui/pattern/issue-22546.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: trait `Tr` is never used
2+
--> $DIR/issue-22546.rs:18:7
3+
|
4+
LL | trait Tr {
5+
| ^^
6+
|
7+
= note: `#[warn(dead_code)]` on by default
8+
9+
warning: 1 warning emitted
10+

0 commit comments

Comments
 (0)