@@ -18,7 +18,7 @@ use rustc_hir::{self as hir, ImplItem, ImplItemKind, Node, PatKind, QPath, TyKin
1818use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
1919use rustc_middle:: middle:: privacy:: Level ;
2020use rustc_middle:: query:: Providers ;
21- use rustc_middle:: ty:: { self , TyCtxt } ;
21+ use rustc_middle:: ty:: { self , AssocTag , TyCtxt } ;
2222use rustc_middle:: { bug, span_bug} ;
2323use rustc_session:: lint:: builtin:: DEAD_CODE ;
2424use rustc_session:: lint:: { self , LintExpectationId } ;
@@ -115,7 +115,10 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
115115
116116 fn handle_res ( & mut self , res : Res ) {
117117 match res {
118- Res :: Def ( DefKind :: Const | DefKind :: AssocConst | DefKind :: TyAlias , def_id) => {
118+ Res :: Def (
119+ DefKind :: Const | DefKind :: AssocConst | DefKind :: AssocTy | DefKind :: TyAlias ,
120+ def_id,
121+ ) => {
119122 self . check_def_id ( def_id) ;
120123 }
121124 _ if self . in_pat => { }
@@ -482,7 +485,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
482485 ) -> bool {
483486 let trait_def_id = match self . tcx . def_kind ( local_def_id) {
484487 // assoc impl items of traits are live if the corresponding trait items are live
485- DefKind :: AssocFn => self
488+ DefKind :: AssocConst | DefKind :: AssocTy | DefKind :: AssocFn => self
486489 . tcx
487490 . associated_item ( local_def_id)
488491 . trait_item_def_id
@@ -647,6 +650,31 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
647650
648651 self . in_pat = in_pat;
649652 }
653+
654+ fn visit_trait_ref ( & mut self , t : & ' tcx hir:: TraitRef < ' tcx > ) {
655+ if let Some ( trait_def_id) = t. path . res . opt_def_id ( )
656+ && let Some ( segment) = t. path . segments . last ( )
657+ && let Some ( args) = segment. args
658+ {
659+ for constraint in args. constraints {
660+ if let Some ( local_def_id) = self
661+ . tcx
662+ . associated_items ( trait_def_id)
663+ . find_by_ident_and_kind (
664+ self . tcx ,
665+ constraint. ident ,
666+ AssocTag :: Const ,
667+ trait_def_id,
668+ )
669+ . and_then ( |item| item. def_id . as_local ( ) )
670+ {
671+ self . worklist . push ( ( local_def_id, ComesFromAllowExpect :: No ) ) ;
672+ }
673+ }
674+ }
675+
676+ intravisit:: walk_trait_ref ( self , t) ;
677+ }
650678}
651679
652680fn has_allow_dead_code_or_lang_attr (
@@ -744,18 +772,12 @@ fn check_item<'tcx>(
744772 {
745773 worklist. push ( ( local_def_id, comes_from_allow) ) ;
746774 } else if of_trait {
747- // FIXME: This condition can be removed
748- // if we support dead check for assoc consts and tys.
749- if !matches ! ( tcx. def_kind( local_def_id) , DefKind :: AssocFn ) {
750- worklist. push ( ( local_def_id, ComesFromAllowExpect :: No ) ) ;
751- } else {
752- // We only care about associated items of traits,
753- // because they cannot be visited directly,
754- // so we later mark them as live if their corresponding traits
755- // or trait items and self types are both live,
756- // but inherent associated items can be visited and marked directly.
757- unsolved_items. push ( ( id, local_def_id) ) ;
758- }
775+ // We only care about associated items of traits,
776+ // because they cannot be visited directly,
777+ // so we later mark them as live if their corresponding traits
778+ // or trait items and self types are both live,
779+ // but inherent associated items can be visited and marked directly.
780+ unsolved_items. push ( ( id, local_def_id) ) ;
759781 }
760782 }
761783 }
@@ -791,15 +813,14 @@ fn check_trait_item(
791813 worklist : & mut Vec < ( LocalDefId , ComesFromAllowExpect ) > ,
792814 id : hir:: TraitItemId ,
793815) {
794- use hir:: TraitItemKind :: { Const , Fn } ;
795- if matches ! ( tcx. def_kind( id. owner_id) , DefKind :: AssocConst | DefKind :: AssocFn ) {
796- let trait_item = tcx. hir_trait_item ( id) ;
797- if matches ! ( trait_item. kind, Const ( _, Some ( _) ) | Fn ( ..) )
798- && let Some ( comes_from_allow) =
799- has_allow_dead_code_or_lang_attr ( tcx, trait_item. owner_id . def_id )
800- {
801- worklist. push ( ( trait_item. owner_id . def_id , comes_from_allow) ) ;
802- }
816+ use hir:: TraitItemKind :: { Const , Fn , Type } ;
817+
818+ let trait_item = tcx. hir_trait_item ( id) ;
819+ if matches ! ( trait_item. kind, Const ( _, Some ( _) ) | Type ( _, Some ( _) ) | Fn ( ..) )
820+ && let Some ( comes_from_allow) =
821+ has_allow_dead_code_or_lang_attr ( tcx, trait_item. owner_id . def_id )
822+ {
823+ worklist. push ( ( trait_item. owner_id . def_id , comes_from_allow) ) ;
803824 }
804825}
805826
@@ -1163,6 +1184,7 @@ impl<'tcx> DeadVisitor<'tcx> {
11631184 }
11641185 match self . tcx . def_kind ( def_id) {
11651186 DefKind :: AssocConst
1187+ | DefKind :: AssocTy
11661188 | DefKind :: AssocFn
11671189 | DefKind :: Fn
11681190 | DefKind :: Static { .. }
0 commit comments