@@ -15,7 +15,7 @@ use rustc_hir::{Node, PatKind, TyKind};
15
15
use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
16
16
use rustc_middle:: middle:: privacy:: Level ;
17
17
use rustc_middle:: query:: Providers ;
18
- use rustc_middle:: ty:: { self , AssocItemContainer , TyCtxt } ;
18
+ use rustc_middle:: ty:: { self , TyCtxt } ;
19
19
use rustc_middle:: { bug, span_bug} ;
20
20
use rustc_session:: lint;
21
21
use rustc_session:: lint:: builtin:: DEAD_CODE ;
@@ -43,63 +43,16 @@ fn should_explore(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
43
43
)
44
44
}
45
45
46
- struct Publicness {
47
- ty_is_public : bool ,
48
- ty_and_all_fields_are_public : bool ,
49
- }
50
-
51
- impl Publicness {
52
- fn new ( ty_is_public : bool , ty_and_all_fields_are_public : bool ) -> Self {
53
- Self { ty_is_public, ty_and_all_fields_are_public }
54
- }
55
- }
56
-
57
- fn struct_all_fields_are_public ( tcx : TyCtxt < ' _ > , id : DefId ) -> bool {
58
- // treat PhantomData and positional ZST as public,
59
- // we don't want to lint types which only have them,
60
- // cause it's a common way to use such types to check things like well-formedness
61
- tcx. adt_def ( id) . all_fields ( ) . all ( |field| {
62
- let field_type = tcx. type_of ( field. did ) . instantiate_identity ( ) ;
63
- if field_type. is_phantom_data ( ) {
64
- return true ;
65
- }
66
- let is_positional = field. name . as_str ( ) . starts_with ( |c : char | c. is_ascii_digit ( ) ) ;
67
- if is_positional
68
- && tcx
69
- . layout_of ( tcx. param_env ( field. did ) . and ( field_type) )
70
- . map_or ( true , |layout| layout. is_zst ( ) )
71
- {
72
- return true ;
73
- }
74
- field. vis . is_public ( )
75
- } )
76
- }
77
-
78
- /// check struct and its fields are public or not,
79
- /// for enum and union, just check they are public,
80
- /// and doesn't solve types like &T for now, just skip them
81
- fn ty_ref_to_pub_struct ( tcx : TyCtxt < ' _ > , ty : & hir:: Ty < ' _ > ) -> Publicness {
46
+ fn ty_ref_to_pub_struct ( tcx : TyCtxt < ' _ > , ty : & hir:: Ty < ' _ > ) -> bool {
82
47
if let TyKind :: Path ( hir:: QPath :: Resolved ( _, path) ) = ty. kind
83
48
&& let Res :: Def ( def_kind, def_id) = path. res
84
49
&& def_id. is_local ( )
50
+ && matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
85
51
{
86
- return match def_kind {
87
- DefKind :: Enum | DefKind :: Union => {
88
- let ty_is_public = tcx. visibility ( def_id) . is_public ( ) ;
89
- Publicness :: new ( ty_is_public, ty_is_public)
90
- }
91
- DefKind :: Struct => {
92
- let ty_is_public = tcx. visibility ( def_id) . is_public ( ) ;
93
- Publicness :: new (
94
- ty_is_public,
95
- ty_is_public && struct_all_fields_are_public ( tcx, def_id) ,
96
- )
97
- }
98
- _ => Publicness :: new ( true , true ) ,
99
- } ;
52
+ tcx. visibility ( def_id) . is_public ( )
53
+ } else {
54
+ true
100
55
}
101
-
102
- Publicness :: new ( true , true )
103
56
}
104
57
105
58
/// Determine if a work from the worklist is coming from a `#[allow]`
@@ -498,11 +451,9 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
498
451
{
499
452
if !matches ! ( trait_item. kind, hir:: TraitItemKind :: Type ( ..) )
500
453
&& !ty_ref_to_pub_struct ( self . tcx , impl_ref. self_ty )
501
- . ty_and_all_fields_are_public
502
454
{
503
- // skip impl-items of non pure pub ty,
504
- // cause we don't know the ty is constructed or not,
505
- // check these later in `solve_rest_impl_items`
455
+ // skip methods of private ty,
456
+ // they would be solved in `solve_rest_impl_items`
506
457
continue ;
507
458
}
508
459
@@ -583,21 +534,22 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
583
534
&& let Some ( local_def_id) = def_id. as_local ( )
584
535
&& matches ! ( def_kind, DefKind :: Struct | DefKind :: Enum | DefKind :: Union )
585
536
{
537
+ if self . tcx . visibility ( impl_item_id) . is_public ( ) {
538
+ // for the public method, we don't know the trait item is used or not,
539
+ // so we mark the method live if the self is used
540
+ return self . live_symbols . contains ( & local_def_id) ;
541
+ }
542
+
586
543
if let Some ( trait_item_id) = self . tcx . associated_item ( impl_item_id) . trait_item_def_id
587
544
&& let Some ( local_id) = trait_item_id. as_local ( )
588
545
{
589
- // for the local impl item , we can know the trait item is used or not,
546
+ // for the private method , we can know the trait item is used or not,
590
547
// so we mark the method live if the self is used and the trait item is used
591
- self . live_symbols . contains ( & local_id) && self . live_symbols . contains ( & local_def_id)
592
- } else {
593
- // for the foreign method and inherent pub method,
594
- // we don't know the trait item or the method is used or not,
595
- // so we mark the method live if the self is used
596
- self . live_symbols . contains ( & local_def_id)
548
+ return self . live_symbols . contains ( & local_id)
549
+ && self . live_symbols . contains ( & local_def_id) ;
597
550
}
598
- } else {
599
- false
600
551
}
552
+ false
601
553
}
602
554
}
603
555
@@ -819,9 +771,7 @@ fn check_item<'tcx>(
819
771
. iter ( )
820
772
. filter_map ( |def_id| def_id. as_local ( ) ) ;
821
773
822
- let self_ty = tcx. hir ( ) . item ( id) . expect_impl ( ) . self_ty ;
823
- let Publicness { ty_is_public, ty_and_all_fields_are_public } =
824
- ty_ref_to_pub_struct ( tcx, self_ty) ;
774
+ let ty_is_pub = ty_ref_to_pub_struct ( tcx, tcx. hir ( ) . item ( id) . expect_impl ( ) . self_ty ) ;
825
775
826
776
// And we access the Map here to get HirId from LocalDefId
827
777
for local_def_id in local_def_ids {
@@ -837,20 +787,18 @@ fn check_item<'tcx>(
837
787
// for trait impl blocks,
838
788
// mark the method live if the self_ty is public,
839
789
// 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)
790
+ if of_trait
791
+ && ( !matches ! ( tcx. def_kind( local_def_id) , DefKind :: AssocFn )
792
+ || tcx. visibility ( local_def_id) . is_public ( )
793
+ && ( ty_is_pub || may_construct_self) )
843
794
{
844
- // if the impl item is public,
845
- // and the ty may be constructed or can be constructed in foreign crates,
846
- // mark the impl item live
847
795
worklist. push ( ( local_def_id, ComesFromAllowExpect :: No ) ) ;
848
796
} else if let Some ( comes_from_allow) =
849
797
has_allow_dead_code_or_lang_attr ( tcx, local_def_id)
850
798
{
851
799
worklist. push ( ( local_def_id, comes_from_allow) ) ;
852
- } else if of_trait || tcx . visibility ( local_def_id ) . is_public ( ) && ty_is_public {
853
- // private impl items of traits || public impl items not constructs self
800
+ } else if of_trait {
801
+ // private method || public method not constructs self
854
802
unsolved_impl_items. push ( ( id, local_def_id) ) ;
855
803
}
856
804
}
@@ -917,14 +865,6 @@ fn create_and_seed_worklist(
917
865
effective_vis
918
866
. is_public_at_level ( Level :: Reachable )
919
867
. then_some ( id)
920
- . filter ( |& id|
921
- // checks impls, impl-items and pub structs with all public fields later
922
- match tcx. def_kind ( id) {
923
- DefKind :: Impl { .. } => false ,
924
- DefKind :: AssocConst | DefKind :: AssocFn => !matches ! ( tcx. associated_item( id) . container, AssocItemContainer :: ImplContainer ) ,
925
- DefKind :: Struct => struct_all_fields_are_public ( tcx, id. to_def_id ( ) ) ,
926
- _ => true
927
- } )
928
868
. map ( |id| ( id, ComesFromAllowExpect :: No ) )
929
869
} )
930
870
// Seed entry point
@@ -1249,15 +1189,10 @@ fn check_mod_deathness(tcx: TyCtxt<'_>, module: LocalModDefId) {
1249
1189
|| ( def_kind == DefKind :: Trait && live_symbols. contains ( & item. owner_id . def_id ) )
1250
1190
{
1251
1191
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
1192
+ // We have diagnosed unused methods in traits
1253
1193
if matches ! ( def_kind, DefKind :: Impl { of_trait: true } )
1254
- && matches ! ( tcx. def_kind( def_id) , DefKind :: AssocConst | DefKind :: AssocFn )
1255
- // skip unused public inherent methods,
1256
- // cause we have diagnosed unconstructed struct
1257
- || matches ! ( def_kind, DefKind :: Impl { of_trait: false } )
1258
- && tcx. visibility ( def_id) . is_public ( )
1259
- && 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
1194
+ && tcx. def_kind ( def_id) == DefKind :: AssocFn
1195
+ || def_kind == DefKind :: Trait && tcx. def_kind ( def_id) != DefKind :: AssocFn
1261
1196
{
1262
1197
continue ;
1263
1198
}
0 commit comments