@@ -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 => { }
@@ -466,7 +469,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
466
469
intravisit:: walk_item ( self , item)
467
470
}
468
471
hir:: ItemKind :: ForeignMod { .. } => { }
469
- hir:: ItemKind :: Trait ( .. ) => {
472
+ hir:: ItemKind :: Trait ( _ , _ , _ , _ , trait_item_refs ) => {
470
473
for impl_def_id in self . tcx . all_impls ( item. owner_id . to_def_id ( ) ) {
471
474
if let Some ( local_def_id) = impl_def_id. as_local ( )
472
475
&& let ItemKind :: Impl ( impl_ref) =
@@ -479,7 +482,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
479
482
intravisit:: walk_path ( self , impl_ref. of_trait . unwrap ( ) . path ) ;
480
483
}
481
484
}
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
+ }
483
491
intravisit:: walk_item ( self , item)
484
492
}
485
493
_ => intravisit:: walk_item ( self , item) ,
@@ -496,9 +504,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
496
504
&& let ItemKind :: Impl ( impl_ref) =
497
505
self . tcx . hir ( ) . expect_item ( local_impl_id) . kind
498
506
{
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
502
509
{
503
510
// skip impl-items of non pure pub ty,
504
511
// cause we don't know the ty is constructed or not,
@@ -837,9 +844,8 @@ fn check_item<'tcx>(
837
844
// for trait impl blocks,
838
845
// mark the method live if the self_ty is public,
839
846
// 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)
843
849
{
844
850
// if the impl item is public,
845
851
// and the ty may be constructed or can be constructed in foreign crates,
@@ -876,10 +882,13 @@ fn check_trait_item(
876
882
worklist : & mut Vec < ( LocalDefId , ComesFromAllowExpect ) > ,
877
883
id : hir:: TraitItemId ,
878
884
) {
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
+ ) {
881
890
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 ( ..) )
883
892
&& let Some ( comes_from_allow) =
884
893
has_allow_dead_code_or_lang_attr ( tcx, trait_item. owner_id . def_id )
885
894
{
@@ -921,7 +930,7 @@ fn create_and_seed_worklist(
921
930
// checks impls, impl-items and pub structs with all public fields later
922
931
match tcx. def_kind ( id) {
923
932
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 ) ,
925
934
DefKind :: Struct => struct_all_fields_are_public ( tcx, id. to_def_id ( ) ) || has_allow_dead_code_or_lang_attr ( tcx, id) . is_some ( ) ,
926
935
_ => true
927
936
} )
@@ -1208,6 +1217,7 @@ impl<'tcx> DeadVisitor<'tcx> {
1208
1217
}
1209
1218
match self . tcx . def_kind ( def_id) {
1210
1219
DefKind :: AssocConst
1220
+ | DefKind :: AssocTy
1211
1221
| DefKind :: AssocFn
1212
1222
| DefKind :: Fn
1213
1223
| DefKind :: Static { .. }
@@ -1249,15 +1259,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
1249
1259
|| ( def_kind == DefKind :: Trait && live_symbols. contains ( & item. owner_id . def_id ) )
1250
1260
{
1251
1261
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
1253
1263
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 )
1255
1265
// skip unused public inherent methods,
1256
1266
// cause we have diagnosed unconstructed struct
1257
1267
|| matches ! ( def_kind, DefKind :: Impl { of_trait: false } )
1258
1268
&& tcx. visibility ( def_id) . is_public ( )
1259
1269
&& 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
1261
1270
{
1262
1271
continue ;
1263
1272
}
0 commit comments