Rollup merge of #126618 - mu001999-contrib:dead/enhance, r=pnkfelix · rust-lang-ci/rust@2724aea (original) (raw)
`@@ -155,7 +155,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
`
155
155
``
156
156
`fn handle_res(&mut self, res: Res) {
`
157
157
`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
`+
) => {
`
159
162
`self.check_def_id(def_id);
`
160
163
`}
`
161
164
` _ if self.in_pat => {}
`
`@@ -441,7 +444,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
`
441
444
` intravisit::walk_item(self, item)
`
442
445
`}
`
443
446
` hir::ItemKind::ForeignMod { .. } => {}
`
444
``
`-
hir::ItemKind::Trait(..) => {
`
``
447
`+
hir::ItemKind::Trait(_, _, _, _, trait_item_refs) => {
`
445
448
`for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {
`
446
449
`if let Some(local_def_id) = impl_def_id.as_local()
`
447
450
` && let ItemKind::Impl(impl_ref) =
`
`@@ -454,7 +457,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
`
454
457
` intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);
`
455
458
`}
`
456
459
`}
`
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
`+
}
`
458
466
` intravisit::walk_item(self, item)
`
459
467
`}
`
460
468
` _ => intravisit::walk_item(self, item),
`
`@@ -471,9 +479,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
`
471
479
` && let ItemKind::Impl(impl_ref) =
`
472
480
`self.tcx.hir().expect_item(local_impl_id).kind
`
473
481
`{
`
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
`
477
484
`{
`
478
485
`// skip impl-items of non pure pub ty,
`
479
486
`// cause we don't know the ty is constructed or not,
`
`@@ -812,9 +819,8 @@ fn check_item<'tcx>(
`
812
819
`// for trait impl blocks,
`
813
820
`// mark the method live if the self_ty is public,
`
814
821
`// 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)
`
818
824
`{
`
819
825
`// if the impl item is public,
`
820
826
`// and the ty may be constructed or can be constructed in foreign crates,
`
`@@ -851,10 +857,13 @@ fn check_trait_item(
`
851
857
`worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,
`
852
858
`id: hir::TraitItemId,
`
853
859
`) {
`
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
`+
) {
`
856
865
`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(..))
`
858
867
` && let Some(comes_from_allow) =
`
859
868
`has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)
`
860
869
`{
`
`@@ -896,7 +905,7 @@ fn create_and_seed_worklist(
`
896
905
`// checks impls, impl-items and pub structs with all public fields later
`
897
906
`match tcx.def_kind(id) {
`
898
907
`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),
`
900
909
`DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),
`
901
910
` _ => true
`
902
911
`})
`
`@@ -1183,6 +1192,7 @@ impl<'tcx> DeadVisitor<'tcx> {
`
1183
1192
`}
`
1184
1193
`match self.tcx.def_kind(def_id) {
`
1185
1194
`DefKind::AssocConst
`
``
1195
`+
| DefKind::AssocTy
`
1186
1196
` | DefKind::AssocFn
`
1187
1197
` | DefKind::Fn
`
1188
1198
` | DefKind::Static { .. }
`
`@@ -1224,15 +1234,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
`
1224
1234
` || (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))
`
1225
1235
`{
`
1226
1236
`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
`
1228
1238
`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)
`
1230
1240
`// skip unused public inherent methods,
`
1231
1241
`// cause we have diagnosed unconstructed struct
`
1232
1242
` || matches!(def_kind, DefKind::Impl { of_trait: false })
`
1233
1243
` && tcx.visibility(def_id).is_public()
`
1234
1244
` && 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
`
1236
1245
`{
`
1237
1246
`continue;
`
1238
1247
`}
`