Skip to content

Commit 2a29b20

Browse files
committed
Stop computing error info in move path builder.
1 parent 187c6da commit 2a29b20

File tree

6 files changed

+61
-107
lines changed

6 files changed

+61
-107
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+4-3
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14101410
// As such we have to search for the local that this
14111411
// capture comes from and mark it as being used as mut.
14121412

1413-
let temp_mpi = self.move_data.rev_lookup.find_local(local);
1413+
let Some(temp_mpi) = self.move_data.rev_lookup.find_local(local) else {
1414+
bug!("temporary should be tracked");
1415+
};
14141416
let init = if let [init_index] = *self.move_data.init_path_map[temp_mpi] {
14151417
&self.move_data.inits[init_index]
14161418
} else {
@@ -2213,7 +2215,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22132215
local: Local,
22142216
flow_state: &Flows<'cx, 'tcx>,
22152217
) -> Option<InitIndex> {
2216-
let mpi = self.move_data.rev_lookup.find_local(local);
2218+
let mpi = self.move_data.rev_lookup.find_local(local)?;
22172219
let ii = &self.move_data.init_path_map[mpi];
22182220
ii.into_iter().find(|&&index| flow_state.ever_inits.contains(index)).copied()
22192221
}

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

+1-1
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

+7-3
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

+37-54
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ use smallvec::{smallvec, SmallVec};
77
use std::mem;
88

99
use super::abs_domain::Lift;
10-
use super::IllegalMoveOriginKind::*;
11-
use super::{Init, InitIndex, InitKind, InitLocation, LookupResult, MoveError};
10+
use super::{Init, InitIndex, InitKind, InitLocation, LookupResult};
1211
use super::{
1312
LocationMap, MoveData, MoveOut, MoveOutIndex, MovePath, MovePathIndex, MovePathLookup,
1413
};
@@ -39,15 +38,15 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
3938
.iter_enumerated()
4039
.map(|(i, l)| {
4140
if l.is_deref_temp() {
42-
MovePathIndex::MAX
41+
None
4342
} else {
44-
Self::new_move_path(
43+
Some(Self::new_move_path(
4544
&mut move_paths,
4645
&mut path_map,
4746
&mut init_path_map,
4847
None,
4948
Place::from(i),
50-
)
49+
))
5150
}
5251
})
5352
.collect(),
@@ -88,6 +87,12 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
8887
}
8988
}
9089

90+
enum MovePathResult {
91+
Path(MovePathIndex),
92+
Union(MovePathIndex),
93+
Error,
94+
}
95+
9196
impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
9297
/// This creates a MovePath for a given place, returning an `MovePathError`
9398
/// if that place can't be moved from.
@@ -96,11 +101,13 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
96101
/// problematic for borrowck.
97102
///
98103
/// Maybe we should have separate "borrowck" and "moveck" modes.
99-
fn move_path_for(&mut self, place: Place<'tcx>) -> Result<MovePathIndex, MoveError<'tcx>> {
104+
fn move_path_for(&mut self, place: Place<'tcx>) -> MovePathResult {
100105
let data = &mut self.builder.data;
101106

102107
debug!("lookup({:?})", place);
103-
let mut base = data.rev_lookup.find_local(place.local);
108+
let Some(mut base) = data.rev_lookup.find_local(place.local) else {
109+
return MovePathResult::Error;
110+
};
104111

105112
// The move path index of the first union that we find. Once this is
106113
// some we stop creating child move paths, since moves from unions
@@ -116,12 +123,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
116123
match elem {
117124
ProjectionElem::Deref => match place_ty.kind() {
118125
ty::Ref(..) | ty::RawPtr(..) => {
119-
return Err(MoveError::cannot_move_out_of(
120-
self.loc,
121-
BorrowedContent {
122-
target_place: place_ref.project_deeper(&[elem], tcx),
123-
},
124-
));
126+
return MovePathResult::Error;
125127
}
126128
ty::Adt(adt, _) => {
127129
if !adt.is_box() {
@@ -157,10 +159,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
157159
ProjectionElem::Field(_, _) => match place_ty.kind() {
158160
ty::Adt(adt, _) => {
159161
if adt.has_dtor(tcx) {
160-
return Err(MoveError::cannot_move_out_of(
161-
self.loc,
162-
InteriorOfTypeWithDestructor { container_ty: place_ty },
163-
));
162+
return MovePathResult::Error;
164163
}
165164
if adt.is_union() {
166165
union_path.get_or_insert(base);
@@ -195,33 +194,15 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
195194
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
196195
match place_ty.kind() {
197196
ty::Slice(_) => {
198-
return Err(MoveError::cannot_move_out_of(
199-
self.loc,
200-
InteriorOfSliceOrArray {
201-
ty: place_ty,
202-
is_index: matches!(elem, ProjectionElem::Index(..)),
203-
},
204-
));
197+
return MovePathResult::Error;
205198
}
206199
ty::Array(_, _) => (),
207200
_ => bug!("Unexpected type {:#?}", place_ty.is_array()),
208201
}
209202
}
210203
ProjectionElem::Index(_) => match place_ty.kind() {
211-
ty::Array(..) => {
212-
return Err(MoveError::cannot_move_out_of(
213-
self.loc,
214-
InteriorOfSliceOrArray { ty: place_ty, is_index: true },
215-
));
216-
}
217-
ty::Slice(_) => {
218-
return Err(MoveError::cannot_move_out_of(
219-
self.loc,
220-
InteriorOfSliceOrArray {
221-
ty: place_ty,
222-
is_index: matches!(elem, ProjectionElem::Index(..)),
223-
},
224-
));
204+
ty::Array(..) | ty::Slice(_) => {
205+
return MovePathResult::Error;
225206
}
226207
_ => bug!("Unexpected type {place_ty:#?}"),
227208
},
@@ -247,9 +228,9 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
247228

248229
if let Some(base) = union_path {
249230
// Move out of union - always move the entire union.
250-
Err(MoveError::UnionMove { path: base })
231+
MovePathResult::Union(base)
251232
} else {
252-
Ok(base)
233+
MovePathResult::Path(base)
253234
}
254235
}
255236

@@ -325,17 +306,17 @@ pub(super) fn gather_moves<'tcx>(
325306
impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
326307
fn gather_args(&mut self) {
327308
for arg in self.body.args_iter() {
328-
let path = self.data.rev_lookup.find_local(arg);
329-
330-
let init = self.data.inits.push(Init {
331-
path,
332-
kind: InitKind::Deep,
333-
location: InitLocation::Argument(arg),
334-
});
309+
if let Some(path) = self.data.rev_lookup.find_local(arg) {
310+
let init = self.data.inits.push(Init {
311+
path,
312+
kind: InitKind::Deep,
313+
location: InitLocation::Argument(arg),
314+
});
335315

336-
debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
316+
debug!("gather_args: adding init {:?} of {:?} for argument {:?}", init, path, arg);
337317

338-
self.data.init_path_map[path].push(init);
318+
self.data.init_path_map[path].push(init);
319+
}
339320
}
340321
}
341322

@@ -538,12 +519,12 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
538519
let base_place =
539520
Place { local: place.local, projection: self.builder.tcx.mk_place_elems(base) };
540521
let base_path = match self.move_path_for(base_place) {
541-
Ok(path) => path,
542-
Err(MoveError::UnionMove { path }) => {
522+
MovePathResult::Path(path) => path,
523+
MovePathResult::Union(path) => {
543524
self.record_move(place, path);
544525
return;
545526
}
546-
Err(MoveError::IllegalMove { .. }) => {
527+
MovePathResult::Error => {
547528
return;
548529
}
549530
};
@@ -563,8 +544,10 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
563544
}
564545
} else {
565546
match self.move_path_for(place) {
566-
Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
567-
Err(MoveError::IllegalMove { .. }) => {}
547+
MovePathResult::Path(path) | MovePathResult::Union(path) => {
548+
self.record_move(place, path)
549+
}
550+
MovePathResult::Error => {}
568551
};
569552
}
570553
}

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

+8-44
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::un_derefer::UnDerefer;
22
use rustc_data_structures::fx::FxHashMap;
33
use rustc_index::{IndexSlice, IndexVec};
44
use rustc_middle::mir::*;
5-
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
5+
use rustc_middle::ty::{ParamEnv, TyCtxt};
66
use rustc_span::Span;
77
use smallvec::SmallVec;
88

@@ -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,54 +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-
}
345-
}
346-
347-
#[derive(Debug)]
348-
pub struct IllegalMoveOrigin<'tcx> {
349-
pub location: Location,
350-
pub kind: IllegalMoveOriginKind<'tcx>,
351-
}
352-
353-
#[derive(Debug)]
354-
pub enum IllegalMoveOriginKind<'tcx> {
355-
/// Illegal move due to attempt to move from behind a reference.
356-
BorrowedContent {
357-
/// The place the reference refers to: if erroneous code was trying to
358-
/// move from `(*x).f` this will be `*x`.
359-
target_place: Place<'tcx>,
360-
},
361-
362-
/// Illegal move due to attempt to move from field of an ADT that
363-
/// implements `Drop`. Rust maintains invariant that all `Drop`
364-
/// ADT's remain fully-initialized so that user-defined destructor
365-
/// can safely read from all of the ADT's fields.
366-
InteriorOfTypeWithDestructor { container_ty: Ty<'tcx> },
367-
368-
/// Illegal move due to attempt to move out of a slice or array.
369-
InteriorOfSliceOrArray { ty: Ty<'tcx>, is_index: bool },
370-
}
371-
372-
#[derive(Debug)]
373-
pub enum MoveError<'tcx> {
374-
IllegalMove { cannot_move_out_of: IllegalMoveOrigin<'tcx> },
375-
UnionMove { path: MovePathIndex },
376-
}
377-
378-
impl<'tcx> MoveError<'tcx> {
379-
pub fn cannot_move_out_of(location: Location, kind: IllegalMoveOriginKind<'tcx>) -> Self {
380-
let origin = IllegalMoveOrigin { location, kind };
381-
MoveError::IllegalMove { cannot_move_out_of: origin }
344+
) -> impl DoubleEndedIterator<Item = (Local, MovePathIndex)> + '_ {
345+
self.locals.iter_enumerated().filter_map(|(l, &idx)| Some((l, idx?)))
382346
}
383347
}
384348

0 commit comments

Comments
 (0)