Skip to content

Commit 6e96ae1

Browse files
committed
Wrap more state into AssignmentResult.
1 parent fb2c30b commit 6e96ae1

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,17 @@ pub(crate) fn check_liveness<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Den
133133
.iterate_to_fixpoint(tcx, body, None)
134134
.into_results_cursor(body);
135135

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());
137137

138-
assignments.merge_guards(&checked_places, body);
138+
let mut assignments =
139+
AssignmentResult::find_dead_assignments(tcx, typing_env, &checked_places, &mut live, body);
139140

140-
let dead_captures = assignments.compute_dead_captures(&checked_places, num_captures);
141+
assignments.merge_guards();
141142

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();
144147

145148
dead_captures
146149
}
@@ -549,6 +552,7 @@ impl<'tcx> PlaceSet<'tcx> {
549552
}
550553
}
551554

555+
#[inline]
552556
fn get(&self, place: PlaceRef<'tcx>) -> Option<(PlaceIndex, &'tcx [PlaceElem<'tcx>])> {
553557
if let Some(index) = self.locals[place.local] {
554558
return Some((index, place.projection));
@@ -582,7 +586,11 @@ impl<'tcx> PlaceSet<'tcx> {
582586
}
583587
}
584588

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>,
586594
/// Set of locals that are live at least once. This is used to report fully unused locals.
587595
ever_live: DenseBitSet<PlaceIndex>,
588596
/// Set of locals that have a non-trivial drop. This is used to skip reporting unused
@@ -597,16 +605,18 @@ struct AssignmentResult {
597605
assignments: IndexVec<PlaceIndex, FxIndexMap<SourceInfo, Access>>,
598606
}
599607

600-
impl AssignmentResult {
608+
impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
601609
/// Collect all assignments to checked locals.
602610
///
603611
/// Assignments are collected, even if they are live. Dead assignments are reported, and live
604612
/// 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>,
607617
cursor: &mut ResultsCursor<'_, 'tcx, MaybeLivePlaces<'_, 'tcx>>,
608-
body: &Body<'tcx>,
609-
) -> AssignmentResult {
618+
body: &'a Body<'tcx>,
619+
) -> AssignmentResult<'a, 'tcx> {
610620
let mut ever_live = DenseBitSet::new_empty(checked_places.len());
611621
let mut ever_dropped = DenseBitSet::new_empty(checked_places.len());
612622
let mut assignments = IndexVec::<PlaceIndex, FxIndexMap<_, _>>::from_elem(
@@ -716,7 +726,15 @@ impl AssignmentResult {
716726
}
717727
}
718728

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+
}
720738
}
721739

722740
/// Match guards introduce a different local to freeze the guarded value as immutable.
@@ -730,16 +748,16 @@ impl AssignmentResult {
730748
/// _ => {}
731749
/// }
732750
///
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() {
735753
let local = place.local;
736754
if let &LocalInfo::User(BindingForm::RefForGuard(arm_local)) =
737-
body.local_decls[local].local_info()
755+
self.body.local_decls[local].local_info()
738756
{
739757
debug_assert!(place.projection.is_empty());
740758

741759
// 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 {
743761
continue;
744762
};
745763
debug_assert_ne!(index, arm_index);
@@ -774,14 +792,10 @@ impl AssignmentResult {
774792
}
775793

776794
/// 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> {
782796
// Report to caller the set of dead captures.
783797
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() {
785799
if self.ever_live.contains(index) {
786800
continue;
787801
}
@@ -802,16 +816,11 @@ impl AssignmentResult {
802816
}
803817

804818
/// 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;
812821

813822
// First, report fully unused locals.
814-
for (index, place) in checked_places.iter() {
823+
for (index, place) in self.checked_places.iter() {
815824
if self.ever_live.contains(index) {
816825
continue;
817826
}
@@ -822,21 +831,21 @@ impl AssignmentResult {
822831
}
823832

824833
let local = place.local;
825-
let decl = &body.local_decls[local];
834+
let decl = &self.body.local_decls[local];
826835

827836
if decl.from_compiler_desugaring() {
828837
continue;
829838
}
830839

831840
// Only report actual user-defined binding from now on.
832841
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 {
834843
continue;
835844
};
836845

837846
let introductions = &binding.introductions;
838847

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 };
840849

841850
// #117284, when `ident_span` and `def_span` have different contexts
842851
// we can't provide a good suggestion, instead we pointed out the spans from macro
@@ -856,7 +865,7 @@ impl AssignmentResult {
856865
def_span,
857866
errors::UnusedVariable {
858867
name,
859-
string_interp: maybe_suggest_literal_matching_name(body, name),
868+
string_interp: maybe_suggest_literal_matching_name(self.body, name),
860869
sugg,
861870
},
862871
);
@@ -887,11 +896,11 @@ impl AssignmentResult {
887896
// This is probably a drop-guard, so we do not issue a warning there.
888897
if maybe_drop_guard(
889898
tcx,
890-
typing_env,
899+
self.typing_env,
891900
index,
892901
&self.ever_dropped,
893-
checked_places,
894-
body,
902+
self.checked_places,
903+
self.body,
895904
) {
896905
statements.clear();
897906
continue;
@@ -943,7 +952,7 @@ impl AssignmentResult {
943952
spans,
944953
errors::UnusedVariable {
945954
name,
946-
string_interp: maybe_suggest_literal_matching_name(body, name),
955+
string_interp: maybe_suggest_literal_matching_name(self.body, name),
947956
sugg,
948957
},
949958
);
@@ -952,25 +961,26 @@ impl AssignmentResult {
952961

953962
/// Second, report unused assignments that do not correspond to initialization.
954963
/// 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;
963966

964967
for (index, statements) in self.assignments.into_iter_enumerated() {
965968
if statements.is_empty() {
966969
continue;
967970
}
968971

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 };
970973

971974
// We have outstanding assignments and with non-trivial drop.
972975
// 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+
) {
974984
continue;
975985
}
976986

@@ -982,18 +992,18 @@ impl AssignmentResult {
982992
}
983993

984994
// 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 {
986996
continue;
987997
};
988998

989999
match kind {
9901000
AccessKind::Assign => {
9911001
let suggestion = annotate_mut_binding_to_immutable_binding(
9921002
tcx,
993-
checked_places.places[index],
994-
body_def_id,
1003+
self.checked_places.places[index],
1004+
self.body.source.def_id().expect_local(),
9951005
source_info.span,
996-
body,
1006+
self.body,
9971007
);
9981008
tcx.emit_node_span_lint(
9991009
lint::builtin::UNUSED_ASSIGNMENTS,

0 commit comments

Comments
 (0)