@@ -135,14 +135,17 @@ pub(crate) fn check_liveness<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Den
135
135
. iterate_to_fixpoint ( tcx, body, None )
136
136
. into_results_cursor ( body) ;
137
137
138
- let mut assignments = AssignmentResult :: find_dead_assignments ( & checked_places , & mut live , body) ;
138
+ let typing_env = ty :: TypingEnv :: post_analysis ( tcx , body. source . def_id ( ) ) ;
139
139
140
- assignments. merge_guards ( & checked_places, body) ;
140
+ let mut assignments =
141
+ AssignmentResult :: find_dead_assignments ( tcx, typing_env, & checked_places, & mut live, body) ;
141
142
142
- let dead_captures = assignments. compute_dead_captures ( & checked_places , num_captures ) ;
143
+ assignments. merge_guards ( ) ;
143
144
144
- assignments. report_fully_unused ( tcx, & checked_places, body) ;
145
- assignments. report_unused_assignments ( tcx, def_id, & checked_places, body) ;
145
+ let dead_captures = assignments. compute_dead_captures ( num_captures) ;
146
+
147
+ assignments. report_fully_unused ( ) ;
148
+ assignments. report_unused_assignments ( ) ;
146
149
147
150
dead_captures
148
151
}
@@ -551,6 +554,7 @@ impl<'tcx> PlaceSet<'tcx> {
551
554
}
552
555
}
553
556
557
+ #[ inline]
554
558
fn get ( & self , place : PlaceRef < ' tcx > ) -> Option < ( PlaceIndex , & ' tcx [ PlaceElem < ' tcx > ] ) > {
555
559
if let Some ( index) = self . locals [ place. local ] {
556
560
return Some ( ( index, place. projection ) ) ;
@@ -584,7 +588,11 @@ impl<'tcx> PlaceSet<'tcx> {
584
588
}
585
589
}
586
590
587
- struct AssignmentResult {
591
+ struct AssignmentResult < ' a , ' tcx > {
592
+ tcx : TyCtxt < ' tcx > ,
593
+ typing_env : ty:: TypingEnv < ' tcx > ,
594
+ checked_places : & ' a PlaceSet < ' tcx > ,
595
+ body : & ' a Body < ' tcx > ,
588
596
/// Set of locals that are live at least once. This is used to report fully unused locals.
589
597
ever_live : DenseBitSet < PlaceIndex > ,
590
598
/// Set of locals that have a non-trivial drop. This is used to skip reporting unused
@@ -599,16 +607,18 @@ struct AssignmentResult {
599
607
assignments : IndexVec < PlaceIndex , FxIndexMap < SourceInfo , Access > > ,
600
608
}
601
609
602
- impl AssignmentResult {
610
+ impl < ' a , ' tcx > AssignmentResult < ' a , ' tcx > {
603
611
/// Collect all assignments to checked locals.
604
612
///
605
613
/// Assignments are collected, even if they are live. Dead assignments are reported, and live
606
614
/// assignments are used to make diagnostics correct for match guards.
607
- fn find_dead_assignments < ' tcx > (
608
- checked_places : & PlaceSet < ' tcx > ,
615
+ fn find_dead_assignments (
616
+ tcx : TyCtxt < ' tcx > ,
617
+ typing_env : ty:: TypingEnv < ' tcx > ,
618
+ checked_places : & ' a PlaceSet < ' tcx > ,
609
619
cursor : & mut ResultsCursor < ' _ , ' tcx , MaybeLivePlaces < ' _ , ' tcx > > ,
610
- body : & Body < ' tcx > ,
611
- ) -> AssignmentResult {
620
+ body : & ' a Body < ' tcx > ,
621
+ ) -> AssignmentResult < ' a , ' tcx > {
612
622
let mut ever_live = DenseBitSet :: new_empty ( checked_places. len ( ) ) ;
613
623
let mut ever_dropped = DenseBitSet :: new_empty ( checked_places. len ( ) ) ;
614
624
let mut assignments = IndexVec :: < PlaceIndex , FxIndexMap < _ , _ > > :: from_elem (
@@ -718,7 +728,15 @@ impl AssignmentResult {
718
728
}
719
729
}
720
730
721
- AssignmentResult { ever_live, ever_dropped, assignments }
731
+ AssignmentResult {
732
+ tcx,
733
+ typing_env,
734
+ checked_places,
735
+ ever_live,
736
+ ever_dropped,
737
+ assignments,
738
+ body,
739
+ }
722
740
}
723
741
724
742
/// Match guards introduce a different local to freeze the guarded value as immutable.
@@ -732,16 +750,16 @@ impl AssignmentResult {
732
750
/// _ => {}
733
751
/// }
734
752
///
735
- fn merge_guards < ' tcx > ( & mut self , checked_places : & PlaceSet < ' tcx > , body : & Body < ' _ > ) {
736
- for ( index, place) in checked_places. iter ( ) {
753
+ fn merge_guards ( & mut self ) {
754
+ for ( index, place) in self . checked_places . iter ( ) {
737
755
let local = place. local ;
738
756
if let & LocalInfo :: User ( BindingForm :: RefForGuard ( arm_local) ) =
739
- body. local_decls [ local] . local_info ( )
757
+ self . body . local_decls [ local] . local_info ( )
740
758
{
741
759
debug_assert ! ( place. projection. is_empty( ) ) ;
742
760
743
761
// Local to use in the arm.
744
- let Some ( ( arm_index, _proj) ) = checked_places. get ( arm_local. into ( ) ) else {
762
+ let Some ( ( arm_index, _proj) ) = self . checked_places . get ( arm_local. into ( ) ) else {
745
763
continue ;
746
764
} ;
747
765
debug_assert_ne ! ( index, arm_index) ;
@@ -776,14 +794,10 @@ impl AssignmentResult {
776
794
}
777
795
778
796
/// Compute captures that are fully dead.
779
- fn compute_dead_captures < ' tcx > (
780
- & self ,
781
- checked_places : & PlaceSet < ' tcx > ,
782
- num_captures : usize ,
783
- ) -> DenseBitSet < FieldIdx > {
797
+ fn compute_dead_captures ( & self , num_captures : usize ) -> DenseBitSet < FieldIdx > {
784
798
// Report to caller the set of dead captures.
785
799
let mut dead_captures = DenseBitSet :: new_empty ( num_captures) ;
786
- for ( index, place) in checked_places. iter ( ) {
800
+ for ( index, place) in self . checked_places . iter ( ) {
787
801
if self . ever_live . contains ( index) {
788
802
continue ;
789
803
}
@@ -804,16 +818,11 @@ impl AssignmentResult {
804
818
}
805
819
806
820
/// Report fully unused locals, and forget the corresponding assignments.
807
- fn report_fully_unused < ' tcx > (
808
- & mut self ,
809
- tcx : TyCtxt < ' tcx > ,
810
- checked_places : & PlaceSet < ' tcx > ,
811
- body : & Body < ' tcx > ,
812
- ) {
813
- let typing_env = ty:: TypingEnv :: post_analysis ( tcx, body. source . def_id ( ) ) ;
821
+ fn report_fully_unused ( & mut self ) {
822
+ let tcx = self . tcx ;
814
823
815
824
// First, report fully unused locals.
816
- for ( index, place) in checked_places. iter ( ) {
825
+ for ( index, place) in self . checked_places . iter ( ) {
817
826
if self . ever_live . contains ( index) {
818
827
continue ;
819
828
}
@@ -824,21 +833,21 @@ impl AssignmentResult {
824
833
}
825
834
826
835
let local = place. local ;
827
- let decl = & body. local_decls [ local] ;
836
+ let decl = & self . body . local_decls [ local] ;
828
837
829
838
if decl. from_compiler_desugaring ( ) {
830
839
continue ;
831
840
}
832
841
833
842
// Only report actual user-defined binding from now on.
834
843
let LocalInfo :: User ( BindingForm :: Var ( binding) ) = decl. local_info ( ) else { continue } ;
835
- let Some ( hir_id) = decl. source_info . scope . lint_root ( & body. source_scopes ) else {
844
+ let Some ( hir_id) = decl. source_info . scope . lint_root ( & self . body . source_scopes ) else {
836
845
continue ;
837
846
} ;
838
847
839
848
let introductions = & binding. introductions ;
840
849
841
- let Some ( ( name, def_span) ) = checked_places. names [ index] else { continue } ;
850
+ let Some ( ( name, def_span) ) = self . checked_places . names [ index] else { continue } ;
842
851
843
852
// #117284, when `ident_span` and `def_span` have different contexts
844
853
// we can't provide a good suggestion, instead we pointed out the spans from macro
@@ -858,7 +867,7 @@ impl AssignmentResult {
858
867
def_span,
859
868
errors:: UnusedVariable {
860
869
name,
861
- string_interp : maybe_suggest_literal_matching_name ( body, name) ,
870
+ string_interp : maybe_suggest_literal_matching_name ( self . body , name) ,
862
871
sugg,
863
872
} ,
864
873
) ;
@@ -889,11 +898,11 @@ impl AssignmentResult {
889
898
// This is probably a drop-guard, so we do not issue a warning there.
890
899
if maybe_drop_guard (
891
900
tcx,
892
- typing_env,
901
+ self . typing_env ,
893
902
index,
894
903
& self . ever_dropped ,
895
- checked_places,
896
- body,
904
+ self . checked_places ,
905
+ self . body ,
897
906
) {
898
907
statements. clear ( ) ;
899
908
continue ;
@@ -945,7 +954,7 @@ impl AssignmentResult {
945
954
spans,
946
955
errors:: UnusedVariable {
947
956
name,
948
- string_interp : maybe_suggest_literal_matching_name ( body, name) ,
957
+ string_interp : maybe_suggest_literal_matching_name ( self . body , name) ,
949
958
sugg,
950
959
} ,
951
960
) ;
@@ -954,25 +963,26 @@ impl AssignmentResult {
954
963
955
964
/// Second, report unused assignments that do not correspond to initialization.
956
965
/// Initializations have been removed in the previous loop reporting unused variables.
957
- fn report_unused_assignments < ' tcx > (
958
- self ,
959
- tcx : TyCtxt < ' tcx > ,
960
- body_def_id : LocalDefId ,
961
- checked_places : & PlaceSet < ' tcx > ,
962
- body : & Body < ' tcx > ,
963
- ) {
964
- let typing_env = ty:: TypingEnv :: post_analysis ( tcx, body. source . def_id ( ) ) ;
966
+ fn report_unused_assignments ( self ) {
967
+ let tcx = self . tcx ;
965
968
966
969
for ( index, statements) in self . assignments . into_iter_enumerated ( ) {
967
970
if statements. is_empty ( ) {
968
971
continue ;
969
972
}
970
973
971
- let Some ( ( name, decl_span) ) = checked_places. names [ index] else { continue } ;
974
+ let Some ( ( name, decl_span) ) = self . checked_places . names [ index] else { continue } ;
972
975
973
976
// We have outstanding assignments and with non-trivial drop.
974
977
// This is probably a drop-guard, so we do not issue a warning there.
975
- if maybe_drop_guard ( tcx, typing_env, index, & self . ever_dropped , checked_places, body) {
978
+ if maybe_drop_guard (
979
+ tcx,
980
+ self . typing_env ,
981
+ index,
982
+ & self . ever_dropped ,
983
+ self . checked_places ,
984
+ self . body ,
985
+ ) {
976
986
continue ;
977
987
}
978
988
@@ -984,18 +994,18 @@ impl AssignmentResult {
984
994
}
985
995
986
996
// Report the dead assignment.
987
- let Some ( hir_id) = source_info. scope . lint_root ( & body. source_scopes ) else {
997
+ let Some ( hir_id) = source_info. scope . lint_root ( & self . body . source_scopes ) else {
988
998
continue ;
989
999
} ;
990
1000
991
1001
match kind {
992
1002
AccessKind :: Assign => {
993
1003
let suggestion = annotate_mut_binding_to_immutable_binding (
994
1004
tcx,
995
- checked_places. places [ index] ,
996
- body_def_id ,
1005
+ self . checked_places . places [ index] ,
1006
+ self . body . source . def_id ( ) . expect_local ( ) ,
997
1007
source_info. span ,
998
- body,
1008
+ self . body ,
999
1009
) ;
1000
1010
tcx. emit_node_span_lint (
1001
1011
lint:: builtin:: UNUSED_ASSIGNMENTS ,
0 commit comments