@@ -156,7 +156,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
156156
157157 fn handle_res ( & mut self , res : Res ) {
158158 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+ ) => {
160163 self . check_def_id ( def_id) ;
161164 }
162165 _ if self . in_pat => { }
@@ -442,7 +445,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
442445 intravisit:: walk_item ( self , item)
443446 }
444447 hir:: ItemKind :: ForeignMod { .. } => { }
445- hir:: ItemKind :: Trait ( .. ) => {
448+ hir:: ItemKind :: Trait ( _ , _ , _ , _ , trait_item_refs ) => {
446449 for impl_def_id in self . tcx . all_impls ( item. owner_id . to_def_id ( ) ) {
447450 if let Some ( local_def_id) = impl_def_id. as_local ( )
448451 && let ItemKind :: Impl ( impl_ref) =
@@ -455,7 +458,12 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
455458 intravisit:: walk_path ( self , impl_ref. of_trait . unwrap ( ) . path ) ;
456459 }
457460 }
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+ }
459467 intravisit:: walk_item ( self , item)
460468 }
461469 _ => intravisit:: walk_item ( self , item) ,
@@ -472,9 +480,8 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
472480 && let ItemKind :: Impl ( impl_ref) =
473481 self . tcx . hir ( ) . expect_item ( local_impl_id) . kind
474482 {
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
478485 {
479486 // skip impl-items of non pure pub ty,
480487 // cause we don't know the ty is constructed or not,
@@ -813,9 +820,8 @@ fn check_item<'tcx>(
813820 // for trait impl blocks,
814821 // mark the method live if the self_ty is public,
815822 // 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)
819825 {
820826 // if the impl item is public,
821827 // and the ty may be constructed or can be constructed in foreign crates,
@@ -852,10 +858,13 @@ fn check_trait_item(
852858 worklist : & mut Vec < ( LocalDefId , ComesFromAllowExpect ) > ,
853859 id : hir:: TraitItemId ,
854860) {
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+ ) {
857866 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 ( ..) )
859868 && let Some ( comes_from_allow) =
860869 has_allow_dead_code_or_lang_attr ( tcx, trait_item. owner_id . def_id )
861870 {
@@ -897,7 +906,7 @@ fn create_and_seed_worklist(
897906 // checks impls, impl-items and pub structs with all public fields later
898907 match tcx. def_kind ( id) {
899908 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 ) ,
901910 DefKind :: Struct => struct_all_fields_are_public ( tcx, id. to_def_id ( ) ) || has_allow_dead_code_or_lang_attr ( tcx, id) . is_some ( ) ,
902911 _ => true
903912 } )
@@ -1132,6 +1141,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11321141 }
11331142 match self . tcx . def_kind ( def_id) {
11341143 DefKind :: AssocConst
1144+ | DefKind :: AssocTy
11351145 | DefKind :: AssocFn
11361146 | DefKind :: Fn
11371147 | DefKind :: Static { .. }
@@ -1173,15 +1183,14 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
11731183 || ( def_kind == DefKind :: Trait && live_symbols. contains ( & item. owner_id . def_id ) )
11741184 {
11751185 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
11771187 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 )
11791189 // skip unused public inherent methods,
11801190 // cause we have diagnosed unconstructed struct
11811191 || matches ! ( def_kind, DefKind :: Impl { of_trait: false } )
11821192 && tcx. visibility ( def_id) . is_public ( )
11831193 && 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
11851194 {
11861195 continue ;
11871196 }
0 commit comments