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