Skip to content

Commit 4f488be

Browse files
committed
move find_assignments to its only use site
this is to remove the entire `util` module
1 parent 85dcf2a commit 4f488be

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_hir::intravisit::Visitor;
1010
use rustc_hir::{self as hir, BindingMode, ByRef, Node};
1111
use rustc_middle::bug;
1212
use rustc_middle::hir::place::PlaceBase;
13+
use rustc_middle::mir::visit::PlaceContext;
1314
use rustc_middle::mir::{
1415
self, BindingForm, Local, LocalDecl, LocalInfo, LocalKind, Location, Mutability, Place,
1516
PlaceRef, ProjectionElem,
@@ -22,7 +23,6 @@ use rustc_trait_selection::traits;
2223
use tracing::debug;
2324

2425
use crate::diagnostics::BorrowedContentSource;
25-
use crate::util::FindAssignments;
2626
use crate::{MirBorrowckCtxt, session_diagnostics};
2727

2828
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
@@ -1081,6 +1081,38 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
10811081
}
10821082
}
10831083

1084+
/// Finds all statements that assign directly to local (i.e., X = ...) and returns their
1085+
/// locations.
1086+
fn find_assignments(&self, local: Local) -> Vec<Location> {
1087+
use rustc_middle::mir::visit::Visitor;
1088+
1089+
struct FindLocalAssignmentVisitor {
1090+
needle: Local,
1091+
locations: Vec<Location>,
1092+
}
1093+
1094+
impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
1095+
fn visit_local(
1096+
&mut self,
1097+
local: Local,
1098+
place_context: PlaceContext,
1099+
location: Location,
1100+
) {
1101+
if self.needle != local {
1102+
return;
1103+
}
1104+
1105+
if place_context.is_place_assignment() {
1106+
self.locations.push(location);
1107+
}
1108+
}
1109+
}
1110+
1111+
let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
1112+
visitor.visit_body(self.body);
1113+
visitor.locations
1114+
}
1115+
10841116
fn suggest_make_local_mut(&self, err: &mut Diag<'_>, local: Local, name: Symbol) {
10851117
let local_decl = &self.body.local_decls[local];
10861118

@@ -1114,7 +1146,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
11141146
})) => {
11151147
// check if the RHS is from desugaring
11161148
let opt_assignment_rhs_span =
1117-
self.body.find_assignments(local).first().map(|&location| {
1149+
self.find_assignments(local).first().map(|&location| {
11181150
if let Some(mir::Statement {
11191151
source_info: _,
11201152
kind:

0 commit comments

Comments
 (0)