Skip to content

Commit 1fedf06

Browse files
committed
Avoid using a magic value for untracked locals.
1 parent 4e5fe04 commit 1fedf06

File tree

6 files changed

+41
-31
lines changed

6 files changed

+41
-31
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2483,9 +2483,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
24832483
/* Check if the mpi is initialized as an argument */
24842484
let mut is_argument = false;
24852485
for arg in self.body.args_iter() {
2486-
let path = self.move_data.rev_lookup.find_local(arg);
2487-
if mpis.contains(&path) {
2488-
is_argument = true;
2486+
if let Some(path) = self.move_data.rev_lookup.find_local(arg) {
2487+
if mpis.contains(&path) {
2488+
is_argument = true;
2489+
}
24892490
}
24902491
}
24912492

compiler/rustc_borrowck/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14121412
// As such we have to search for the local that this
14131413
// capture comes from and mark it as being used as mut.
14141414

1415-
let temp_mpi = self.move_data.rev_lookup.find_local(local);
1415+
let Some(temp_mpi) = self.move_data.rev_lookup.find_local(local) else {
1416+
bug!("temporary should be tracked");
1417+
};
14161418
let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
14171419
&self.move_data.inits[init_index]
14181420
} else {
@@ -2219,7 +2221,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22192221
local: Local,
22202222
flow_state: &Flows<'cx, 'tcx>,
22212223
) -> Option<InitIndex> {
2222-
let mpi = self.move_data.rev_lookup.find_local(local);
2224+
let mpi = self.move_data.rev_lookup.find_local(local)?;
22232225
let ii = &self.move_data.init_path_map[mpi];
22242226
ii.into_iter().find(|&&index| flow_state.ever_inits.contains(index)).copied()
22252227
}

compiler/rustc_borrowck/src/type_check/liveness/trace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'me, 'typeck, 'flow, 'tcx> LivenessResults<'me, 'typeck, 'flow, 'tcx> {
284284
fn compute_drop_live_points_for(&mut self, local: Local) {
285285
debug!("compute_drop_live_points_for(local={:?})", local);
286286

287-
let mpi = self.cx.move_data.rev_lookup.find_local(local);
287+
let Some(mpi) = self.cx.move_data.rev_lookup.find_local(local) else { return };
288288
debug!("compute_drop_live_points_for: mpi = {:?}", mpi);
289289

290290
// Find the drops where `local` is initialized.

compiler/rustc_mir_dataflow/src/impls/initialized.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,9 +690,13 @@ impl<'tcx> GenKillAnalysis<'tcx> for EverInitializedPlaces<'_, 'tcx> {
690690
if let mir::StatementKind::StorageDead(local) = stmt.kind {
691691
// End inits for StorageDead, so that an immutable variable can
692692
// be reinitialized on the next iteration of the loop.
693-
let move_path_index = rev_lookup.find_local(local);
694-
debug!("clears the ever initialized status of {:?}", init_path_map[move_path_index]);
695-
trans.kill_all(init_path_map[move_path_index].iter().copied());
693+
if let Some(move_path_index) = rev_lookup.find_local(local) {
694+
debug!(
695+
"clears the ever initialized status of {:?}",
696+
init_path_map[move_path_index]
697+
);
698+
trans.kill_all(init_path_map[move_path_index].iter().copied());
699+
}
696700
}
697701
}
698702

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
3939
.iter_enumerated()
4040
.map(|(i, l)| {
4141
if l.is_deref_temp() {
42-
MovePathIndex::MAX
42+
None
4343
} else {
44-
Self::new_move_path(
44+
Some(Self::new_move_path(
4545
&mut move_paths,
4646
&mut path_map,
4747
&mut init_path_map,
4848
None,
4949
Place::from(i),
50-
)
50+
))
5151
}
5252
})
5353
.collect(),
@@ -100,7 +100,9 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
100100
let data = &mut self.builder.data;
101101

102102
debug!("lookup({:?})", place);
103-
let mut base = data.rev_lookup.find_local(place.local);
103+
let Some(mut base) = data.rev_lookup.find_local(place.local) else {
104+
return Err(MoveError::UntrackedLocal);
105+
};
104106

105107
// The move path index of the first union that we find. Once this is
106108
// some we stop creating child move paths, since moves from unions
@@ -328,17 +330,17 @@ pub(super) fn gather_moves<'tcx>(
328330
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
329331
fn gather_args(&mut self) {
330332
for arg in self.body.args_iter() {
331-
let path = self.data.rev_lookup.find_local(arg);
333+
if let Some(path) = self.data.rev_lookup.find_local(arg) {
334+
let init = self.data.inits.push(Init {
335+
path,
336+
kind: InitKind::Deep,
337+
location: InitLocation::Argument(arg),
338+
});
332339

333-
let init = self.data.inits.push(Init {
334-
path,
335-
kind: InitKind::Deep,
336-
location: InitLocation::Argument(arg),
337-
});
338-
339-
debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
340+
debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
340341

341-
self.data.init_path_map[path].push(init);
342+
self.data.init_path_map[path].push(init);
343+
}
342344
}
343345
}
344346

@@ -546,9 +548,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
546548
self.record_move(place, path);
547549
return;
548550
}
549-
Err(MoveError::IllegalMove { .. }) => {
550-
return;
551-
}
551+
Err(MoveError::IllegalMove { .. } | MoveError::UntrackedLocal) => return,
552552
};
553553
let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
554554
let len: u64 = match base_ty.kind() {
@@ -567,7 +567,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
567567
} else {
568568
match self.move_path_for(place) {
569569
Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
570-
Err(MoveError::IllegalMove { .. }) => {}
570+
Err(MoveError::IllegalMove { .. } | MoveError::UntrackedLocal) => {}
571571
};
572572
}
573573
}

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl Init {
290290
/// Tables mapping from a place to its MovePathIndex.
291291
#[derive(Debug)]
292292
pub struct MovePathLookup<'tcx> {
293-
locals: IndexVec<Local, MovePathIndex>,
293+
locals: IndexVec<Local, Option<MovePathIndex>>,
294294

295295
/// projections are made from a base-place and a projection
296296
/// elem. The base-place will have a unique MovePathIndex; we use
@@ -317,7 +317,9 @@ impl<'tcx> MovePathLookup<'tcx> {
317317
// unknown place, but will rather return the nearest available
318318
// parent.
319319
pub fn find(&self, place: PlaceRef<'tcx>) -> LookupResult {
320-
let mut result = self.find_local(place.local);
320+
let Some(mut result) = self.find_local(place.local) else {
321+
return LookupResult::Parent(None);
322+
};
321323

322324
for (_, elem) in self.un_derefer.iter_projections(place) {
323325
if let Some(&subpath) = self.projections.get(&(result, elem.lift())) {
@@ -331,16 +333,16 @@ impl<'tcx> MovePathLookup<'tcx> {
331333
}
332334

333335
#[inline]
334-
pub fn find_local(&self, local: Local) -> MovePathIndex {
336+
pub fn find_local(&self, local: Local) -> Option<MovePathIndex> {
335337
self.locals[local]
336338
}
337339

338340
/// An enumerated iterator of `local`s and their associated
339341
/// `MovePathIndex`es.
340342
pub fn iter_locals_enumerated(
341343
&self,
342-
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + ExactSizeIterator + '_ {
343-
self.locals.iter_enumerated().map(|(l, &idx)| (l, idx))
344+
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + '_ {
345+
self.locals.iter_enumerated().filter_map(|(l, &idx)| Some((l, idx?)))
344346
}
345347
}
346348

@@ -373,6 +375,7 @@ pub enum IllegalMoveOriginKind<'tcx> {
373375
pub enum MoveError<'tcx> {
374376
IllegalMove { cannot_move_out_of: IllegalMoveOrigin<'tcx> },
375377
UnionMove { path: MovePathIndex },
378+
UntrackedLocal,
376379
}
377380

378381
impl<'tcx> MoveError<'tcx> {

0 commit comments

Comments
 (0)