Mark assoc tys live only if the trait is live · rust-lang/rust@a264bff (original) (raw)

`@@ -156,7 +156,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

`

156

156

``

157

157

`fn handle_res(&mut self, res: Res) {

`

158

158

`match res {

`

159

``

`-

Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::TyAlias, def_id) => {

`

``

159

`+

Res::Def(

`

``

160

`+

DefKind::Const | DefKind::AssocConst | DefKind::AssocTy | DefKind::TyAlias,

`

``

161

`+

def_id,

`

``

162

`+

) => {

`

160

163

`self.check_def_id(def_id);

`

161

164

`}

`

162

165

` _ if self.in_pat => {}

`

`@@ -442,7 +445,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

`

442

445

` intravisit::walk_item(self, item)

`

443

446

`}

`

444

447

` hir::ItemKind::ForeignMod { .. } => {}

`

445

``

`-

hir::ItemKind::Trait(..) => {

`

``

448

`+

hir::ItemKind::Trait(_, _, _, _, trait_item_refs) => {

`

446

449

`for impl_def_id in self.tcx.all_impls(item.owner_id.to_def_id()) {

`

447

450

`if let Some(local_def_id) = impl_def_id.as_local()

`

448

451

` && let ItemKind::Impl(impl_ref) =

`

`@@ -455,7 +458,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

`

455

458

` intravisit::walk_path(self, impl_ref.of_trait.unwrap().path);

`

456

459

`}

`

457

460

`}

`

458

``

-

``

461

`+

// mark assoc ty live if the trait is live

`

``

462

`+

for trait_item in trait_item_refs {

`

``

463

`+

if let hir::AssocItemKind::Type = trait_item.kind {

`

``

464

`+

self.check_def_id(trait_item.id.owner_id.to_def_id());

`

``

465

`+

}

`

``

466

`+

}

`

459

467

` intravisit::walk_item(self, item)

`

460

468

`}

`

461

469

` _ => intravisit::walk_item(self, item),

`

`@@ -472,9 +480,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {

`

472

480

` && let ItemKind::Impl(impl_ref) =

`

473

481

`self.tcx.hir().expect_item(local_impl_id).kind

`

474

482

`{

`

475

``

`-

if !matches!(trait_item.kind, hir::TraitItemKind::Type(..))

`

476

``

`-

&& !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)

`

477

``

`-

.ty_and_all_fields_are_public

`

``

483

`+

if !ty_ref_to_pub_struct(self.tcx, impl_ref.self_ty)

`

``

484

`+

.ty_and_all_fields_are_public

`

478

485

`{

`

479

486

`// skip impl-items of non pure pub ty,

`

480

487

`// cause we don't know the ty is constructed or not,

`

`@@ -813,9 +820,8 @@ fn check_item<'tcx>(

`

813

820

`// for trait impl blocks,

`

814

821

`// mark the method live if the self_ty is public,

`

815

822

`// or the method is public and may construct self

`

816

``

`-

if of_trait && matches!(tcx.def_kind(local_def_id), DefKind::AssocTy)

`

817

``

`-

|| tcx.visibility(local_def_id).is_public()

`

818

``

`-

&& (ty_and_all_fields_are_public || may_construct_self)

`

``

823

`+

if tcx.visibility(local_def_id).is_public()

`

``

824

`+

&& (ty_and_all_fields_are_public || may_construct_self)

`

819

825

`{

`

820

826

`// if the impl item is public,

`

821

827

`// and the ty may be constructed or can be constructed in foreign crates,

`

`@@ -852,10 +858,13 @@ fn check_trait_item(

`

852

858

`worklist: &mut Vec<(LocalDefId, ComesFromAllowExpect)>,

`

853

859

`id: hir::TraitItemId,

`

854

860

`) {

`

855

``

`-

use hir::TraitItemKind::{Const, Fn};

`

856

``

`-

if matches!(tcx.def_kind(id.owner_id), DefKind::AssocConst | DefKind::AssocFn) {

`

``

861

`+

use hir::TraitItemKind::{Const, Fn, Type};

`

``

862

`+

if matches!(

`

``

863

`+

tcx.def_kind(id.owner_id),

`

``

864

`+

DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn

`

``

865

`+

) {

`

857

866

`let trait_item = tcx.hir().trait_item(id);

`

858

``

`-

if matches!(trait_item.kind, Const(, Some()) | Fn(..))

`

``

867

`+

if matches!(trait_item.kind, Const(, Some()) | Type(, Some()) | Fn(..))

`

859

868

` && let Some(comes_from_allow) =

`

860

869

`has_allow_dead_code_or_lang_attr(tcx, trait_item.owner_id.def_id)

`

861

870

`{

`

`@@ -897,7 +906,7 @@ fn create_and_seed_worklist(

`

897

906

`// checks impls, impl-items and pub structs with all public fields later

`

898

907

`match tcx.def_kind(id) {

`

899

908

`DefKind::Impl { .. } => false,

`

900

``

`-

DefKind::AssocConst | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),

`

``

909

`+

DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn => !matches!(tcx.associated_item(id).container, AssocItemContainer::ImplContainer),

`

901

910

`DefKind::Struct => struct_all_fields_are_public(tcx, id.to_def_id()) || has_allow_dead_code_or_lang_attr(tcx, id).is_some(),

`

902

911

` _ => true

`

903

912

`})

`

`@@ -1132,6 +1141,7 @@ impl<'tcx> DeadVisitor<'tcx> {

`

1132

1141

`}

`

1133

1142

`match self.tcx.def_kind(def_id) {

`

1134

1143

`DefKind::AssocConst

`

``

1144

`+

| DefKind::AssocTy

`

1135

1145

` | DefKind::AssocFn

`

1136

1146

` | DefKind::Fn

`

1137

1147

` | DefKind::Static { .. }

`

`@@ -1173,15 +1183,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {

`

1173

1183

` || (def_kind == DefKind::Trait && live_symbols.contains(&item.owner_id.def_id))

`

1174

1184

`{

`

1175

1185

`for &def_id in tcx.associated_item_def_ids(item.owner_id.def_id) {

`

1176

``

`-

// We have diagnosed unused assoc consts and fns in traits

`

``

1186

`+

// We have diagnosed unused assocs in traits

`

1177

1187

`if matches!(def_kind, DefKind::Impl { of_trait: true })

`

1178

``

`-

&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocFn)

`

``

1188

`+

&& matches!(tcx.def_kind(def_id), DefKind::AssocConst | DefKind::AssocTy | DefKind::AssocFn)

`

1179

1189

`// skip unused public inherent methods,

`

1180

1190

`// cause we have diagnosed unconstructed struct

`

1181

1191

` || matches!(def_kind, DefKind::Impl { of_trait: false })

`

1182

1192

` && tcx.visibility(def_id).is_public()

`

1183

1193

` && ty_ref_to_pub_struct(tcx, tcx.hir().item(item).expect_impl().self_ty).ty_is_public

`

1184

``

`-

|| def_kind == DefKind::Trait && tcx.def_kind(def_id) == DefKind::AssocTy

`

1185

1194

`{

`

1186

1195

`continue;

`

1187

1196

`}

`