@@ -14,7 +14,7 @@ use rustc_errors::MultiSpan;
14
14
use rustc_hir:: def:: { CtorOf , DefKind , Res } ;
15
15
use rustc_hir:: def_id:: { DefId , LocalDefId , LocalModDefId } ;
16
16
use 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 } ;
18
18
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
19
19
use rustc_middle:: middle:: privacy:: Level ;
20
20
use rustc_middle:: query:: Providers ;
@@ -46,10 +46,10 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
46
46
}
47
47
48
48
/// 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 ) > {
51
51
match ty. kind {
52
- TyKind :: Path ( hir :: QPath :: Resolved ( _, path) ) => {
52
+ TyKind :: Path ( QPath :: Resolved ( _, path) ) => {
53
53
if let Res :: Def ( def_kind, def_id) = path. res
54
54
&& let Some ( local_def_id) = def_id. as_local ( )
55
55
{
@@ -366,7 +366,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
366
366
&& let Some ( fn_sig) =
367
367
self . tcx . hir_fn_sig_by_hir_id ( self . tcx . local_def_id_to_hir_id ( local_def_id) )
368
368
&& matches ! ( fn_sig. decl. implicit_self, hir:: ImplicitSelfKind :: None )
369
- && let TyKind :: Path ( hir :: QPath :: Resolved ( _, path) ) =
369
+ && let TyKind :: Path ( QPath :: Resolved ( _, path) ) =
370
370
self . tcx . hir_expect_item ( local_impl_of) . expect_impl ( ) . self_ty . kind
371
371
&& let Res :: Def ( def_kind, did) = path. res
372
372
{
@@ -426,7 +426,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
426
426
intravisit:: walk_item ( self , item)
427
427
}
428
428
hir:: ItemKind :: ForeignMod { .. } => { }
429
- hir:: ItemKind :: Trait ( _ , _ , _ , _ , _ , trait_item_refs) => {
429
+ hir:: ItemKind :: Trait ( .. , trait_item_refs) => {
430
430
// mark assoc ty live if the trait is live
431
431
for trait_item in trait_item_refs {
432
432
if matches ! ( trait_item. kind, hir:: AssocItemKind :: Type ) {
@@ -485,12 +485,15 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
485
485
}
486
486
}
487
487
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,
492
491
/// 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 {
494
497
if self . should_ignore_item ( local_def_id. to_def_id ( ) ) {
495
498
return false ;
496
499
}
@@ -513,9 +516,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
513
516
return false ;
514
517
}
515
518
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.
519
522
// public traits directly
520
523
if let Some ( fn_sig) =
521
524
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> {
526
529
}
527
530
}
528
531
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.
530
533
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 )
532
535
&& matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
533
536
&& !self . live_symbols . contains ( & local_def_id)
534
537
{
@@ -570,7 +573,7 @@ impl<'tcx> Visitor<'tcx> for MarkSymbolVisitor<'tcx> {
570
573
571
574
fn visit_expr ( & mut self , expr : & ' tcx hir:: Expr < ' tcx > ) {
572
575
match expr. kind {
573
- hir:: ExprKind :: Path ( ref qpath @ hir :: QPath :: TypeRelative ( ..) ) => {
576
+ hir:: ExprKind :: Path ( ref qpath @ QPath :: TypeRelative ( ..) ) => {
574
577
let res = self . typeck_results ( ) . qpath_res ( qpath, expr. hir_id ) ;
575
578
self . handle_res ( res) ;
576
579
}
@@ -750,12 +753,6 @@ fn check_item<'tcx>(
750
753
}
751
754
}
752
755
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
-
759
756
if let Some ( comes_from_allow) =
760
757
has_allow_dead_code_or_lang_attr ( tcx, id. owner_id . def_id )
761
758
{
@@ -764,23 +761,26 @@ fn check_item<'tcx>(
764
761
unsolved_items. push ( ( id, id. owner_id . def_id ) ) ;
765
762
}
766
763
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)
775
769
{
776
770
worklist. push ( ( local_def_id, comes_from_allow) ) ;
777
771
} 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
+ }
784
784
}
785
785
}
786
786
}
@@ -889,22 +889,20 @@ fn live_symbols_and_ignored_derived_traits(
889
889
ignored_derived_traits : Default :: default ( ) ,
890
890
} ;
891
891
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) =
894
894
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)
896
896
} ) ;
897
897
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 ( ) ;
903
901
symbol_visitor. mark_live_symbols ( ) ;
904
902
905
- ( rest_items_should_be_checked , unsolved_items) =
903
+ ( items_to_check , unsolved_items) =
906
904
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)
908
906
} ) ;
909
907
}
910
908
@@ -1187,10 +1185,11 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
1187
1185
let def_kind = tcx. def_kind ( item. owner_id ) ;
1188
1186
1189
1187
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.
1194
1193
if matches ! ( def_kind, DefKind :: Impl { of_trait: false } )
1195
1194
|| ( def_kind == DefKind :: Trait && live_symbols. contains ( & item. owner_id . def_id ) )
1196
1195
{
0 commit comments