@@ -14,7 +14,7 @@ use rustc_errors::MultiSpan;
1414use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
1515use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
1616use rustc_hir:: intravisit:: { self , Visitor } ;
17- use rustc_hir:: { self as hir, Node , PatKind , TyKind } ;
17+ use rustc_hir:: { self as hir, Node , PatKind , QPath , TyKind } ;
1818use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
1919use rustc_middle:: middle:: privacy:: Level ;
2020use rustc_middle:: query:: Providers ;
@@ -46,10 +46,10 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
4646}
4747
4848/// Returns the local def id and def kind of the adt,
49- /// if the given ty refers to one local adt definition
50- fn adt_def_of_ty < ' tcx > ( ty : & hir:: Ty < ' tcx > ) -> Option < ( LocalDefId , DefKind ) > {
49+ /// if the given ty refers to one local adt definition.
50+ fn local_adt_def_of_ty < ' tcx > ( ty : & hir:: Ty < ' tcx > ) -> Option < ( LocalDefId , DefKind ) > {
5151 match ty. kind {
52- TyKind :: Path ( hir :: QPath :: Resolved ( _, path) ) => {
52+ TyKind :: Path ( QPath :: Resolved ( _, path) ) => {
5353 if let Res :: Def ( def_kind, def_id) = path. res
5454 && let Some ( local_def_id) = def_id. as_local ( )
5555 {
@@ -366,7 +366,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
366366 && let Some ( fn_sig) =
367367 self . tcx . hir_fn_sig_by_hir_id ( self . tcx . local_def_id_to_hir_id ( local_def_id) )
368368 && matches ! ( fn_sig. decl. implicit_self, hir:: ImplicitSelfKind :: None )
369- && let TyKind :: Path ( hir :: QPath :: Resolved ( _, path) ) =
369+ && let TyKind :: Path ( QPath :: Resolved ( _, path) ) =
370370 self . tcx . hir_expect_item ( local_impl_of) . expect_impl ( ) . self_ty . kind
371371 && let Res :: Def ( def_kind, did) = path. res
372372 {
@@ -426,7 +426,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
426426 intravisit:: walk_item ( self , item)
427427 }
428428 hir:: ItemKind :: ForeignMod { .. } => { }
429- hir:: ItemKind :: Trait ( _ , _ , _ , _ , _ , trait_item_refs) => {
429+ hir:: ItemKind :: Trait ( .. , trait_item_refs) => {
430430 // mark assoc ty live if the trait is live
431431 for trait_item in trait_item_refs {
432432 if matches ! ( trait_item. kind, hir:: AssocItemKind :: Type ) {
@@ -485,12 +485,15 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
485485 }
486486 }
487487
488- /// Returns an impl or impl item should be checked or not
489- /// `impl_id` is the `ItemId` of the impl or the impl item belongs to,
490- /// `local_def_id` may point to the impl or the impl item,
491- /// both impl and impl item that may pass to this function are of trait,
488+ /// Returns whether `local_def_id` is live or not.
489+ /// `local_def_id` points to an impl or an impl item,
490+ /// both impl and impl item that may be passed to this function are of a trait,
492491 /// and added into the unsolved_items during `create_and_seed_worklist`
493- fn item_should_be_checked ( & mut self , impl_id : hir:: ItemId , local_def_id : LocalDefId ) -> bool {
492+ fn check_impl_or_impl_item_live (
493+ & mut self ,
494+ impl_id : hir:: ItemId ,
495+ local_def_id : LocalDefId ,
496+ ) -> bool {
494497 if self . should_ignore_item ( local_def_id. to_def_id ( ) ) {
495498 return false ;
496499 }
@@ -513,9 +516,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
513516 return false ;
514517 }
515518
516- // FIXME: legacy logic to check the fn may construct self ,
517- // this can be removed after supporting marking adt appears
518- // in patterns as live, then we can check private impls of
519+ // FIXME: legacy logic to check whether the function may construct `Self` ,
520+ // this can be removed after supporting marking ADTs appearing in patterns
521+ // as live, then we can check private impls of.
519522 // public traits directly
520523 if let Some ( fn_sig) =
521524 self . tcx . hir_fn_sig_by_hir_id ( self . tcx . local_def_id_to_hir_id ( local_def_id) )
@@ -526,9 +529,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
526529 }
527530 }
528531
529- // the impl/impl item is used if the trait/trait item is used and the ty is used
532+ // The impl/impl item is used if the trait/trait item is used and the ty is used.
530533 if let Some ( ( local_def_id, def_kind) ) =
531- adt_def_of_ty ( self . tcx . hir_item ( impl_id) . expect_impl ( ) . self_ty )
534+ local_adt_def_of_ty ( self . tcx . hir_item ( impl_id) . expect_impl ( ) . self_ty )
532535 && matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
533536 && !self . live_symbols . contains ( & local_def_id)
534537 {
@@ -570,7 +573,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
570573
571574 fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' tcx > ) {
572575 match expr. kind {
573- hir:: ExprKind :: Path ( ref qpath @ hir :: QPath :: TypeRelative ( ..) ) => {
576+ hir:: ExprKind :: Path ( ref qpath @ QPath :: TypeRelative ( ..) ) => {
574577 let res = self . typeck_results ( ) . qpath_res ( qpath, expr. hir_id ) ;
575578 self . handle_res ( res) ;
576579 }
@@ -750,12 +753,6 @@ fn check_item<'tcx>(
750753 }
751754 }
752755 DefKind :: Impl { of_trait } => {
753- // get DefIds from another query
754- let associated_item_def_ids = tcx
755- . associated_item_def_ids ( id. owner_id )
756- . iter ( )
757- . filter_map ( |def_id| def_id. as_local ( ) ) ;
758-
759756 if let Some ( comes_from_allow) =
760757 has_allow_dead_code_or_lang_attr ( tcx, id. owner_id . def_id )
761758 {
@@ -764,23 +761,26 @@ fn check_item<'tcx>(
764761 unsolved_items. push ( ( id, id. owner_id . def_id ) ) ;
765762 }
766763
767- // And we access the Map here to get HirId from LocalDefId
768- for local_def_id in associated_item_def_ids {
769- // FIXME: this condition can be removed
770- // if we support dead check for assoc consts and tys
771- if of_trait && !matches ! ( tcx. def_kind( local_def_id) , DefKind :: AssocFn ) {
772- worklist. push ( ( local_def_id, ComesFromAllowExpect :: No ) ) ;
773- } else if let Some ( comes_from_allow) =
774- has_allow_dead_code_or_lang_attr ( tcx, local_def_id)
764+ // We access the Map here to get HirId from LocalDefId
765+ for def_id in tcx. associated_item_def_ids ( id. owner_id ) {
766+ let local_def_id = def_id. expect_local ( ) ;
767+
768+ if let Some ( comes_from_allow) = has_allow_dead_code_or_lang_attr ( tcx, local_def_id)
775769 {
776770 worklist. push ( ( local_def_id, comes_from_allow) ) ;
777771 } else if of_trait {
778- // we only care about assoc items of trait,
779- // because they cannot be visited directly
780- // so we mark them based on the trait/trait item
781- // and self ty are checked first and both live,
782- // but inherent assoc items can be visited and marked directly
783- unsolved_items. push ( ( id, local_def_id) ) ;
772+ // FIXME: This condition can be removed
773+ // if we support dead check for assoc consts and tys.
774+ if !matches ! ( tcx. def_kind( local_def_id) , DefKind :: AssocFn ) {
775+ worklist. push ( ( local_def_id, ComesFromAllowExpect :: No ) ) ;
776+ } else {
777+ // We only care about assoc items of trait,
778+ // because they cannot be visited directly
779+ // so we mark them based on the trait/trait item
780+ // and self ty are checked first and both live,
781+ // but inherent assoc items can be visited and marked directly.
782+ unsolved_items. push ( ( id, local_def_id) ) ;
783+ }
784784 }
785785 }
786786 }
@@ -889,22 +889,20 @@ fn live_symbols_and_ignored_derived_traits(
889889 ignored_derived_traits : Default :: default ( ) ,
890890 } ;
891891 symbol_visitor. mark_live_symbols ( ) ;
892- let mut rest_items_should_be_checked ;
893- ( rest_items_should_be_checked , unsolved_items) =
892+ let mut items_to_check ;
893+ ( items_to_check , unsolved_items) =
894894 unsolved_items. into_iter ( ) . partition ( |& ( impl_id, local_def_id) | {
895- symbol_visitor. item_should_be_checked ( impl_id, local_def_id)
895+ symbol_visitor. check_impl_or_impl_item_live ( impl_id, local_def_id)
896896 } ) ;
897897
898- while !rest_items_should_be_checked. is_empty ( ) {
899- symbol_visitor. worklist = rest_items_should_be_checked
900- . into_iter ( )
901- . map ( |( _, id) | ( id, ComesFromAllowExpect :: No ) )
902- . collect ( ) ;
898+ while !items_to_check. is_empty ( ) {
899+ symbol_visitor. worklist =
900+ items_to_check. into_iter ( ) . map ( |( _, id) | ( id, ComesFromAllowExpect :: No ) ) . collect ( ) ;
903901 symbol_visitor. mark_live_symbols ( ) ;
904902
905- ( rest_items_should_be_checked , unsolved_items) =
903+ ( items_to_check , unsolved_items) =
906904 unsolved_items. into_iter ( ) . partition ( |& ( impl_id, local_def_id) | {
907- symbol_visitor. item_should_be_checked ( impl_id, local_def_id)
905+ symbol_visitor. check_impl_or_impl_item_live ( impl_id, local_def_id)
908906 } ) ;
909907 }
910908
@@ -1187,10 +1185,11 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
11871185 let def_kind = tcx. def_kind ( item. owner_id ) ;
11881186
11891187 let mut dead_codes = Vec :: new ( ) ;
1190- // only diagnose unused assoc items for inherient impl and used trait
1191- // for unused assoc items in impls of trait, we have diagnosed them in
1192- // the trait if they are unused, for unused assoc items in unused trait,
1193- // we have diagnosed the unused trait
1188+ // Only diagnose unused assoc items in inherient impl and used trait,
1189+ // for unused assoc items in impls of trait,
1190+ // we have diagnosed them in the trait if they are unused,
1191+ // for unused assoc items in unused trait,
1192+ // we have diagnosed the unused trait.
11941193 if matches ! ( def_kind, DefKind :: Impl { of_trait: false } )
11951194 || ( def_kind == DefKind :: Trait && live_symbols. contains ( & item. owner_id . def_id ) )
11961195 {
0 commit comments