Skip to content

Commit 4bedd7d

Browse files
committed
Stop computing error info in move path builder.
1 parent 90e6d29 commit 4bedd7d

File tree

5 files changed

+50
-89
lines changed

5 files changed

+50
-89
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ mod mutability_errors;
4646
mod region_errors;
4747

4848
pub(crate) use bound_region_errors::{ToUniverseInfo, UniverseInfo};
49-
pub(crate) use move_errors::MoveError;
49+
pub(crate) use move_errors::{IllegalMoveOriginKind, MoveError};
5050
pub(crate) use mutability_errors::AccessKind;
5151
pub(crate) use outlives_suggestion::OutlivesSuggestionBuilder;
5252
pub(crate) use region_errors::{ErrorConstraintInfo, RegionErrorKind, RegionErrors};

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
22
use rustc_middle::mir::*;
3-
use rustc_middle::ty;
4-
use rustc_mir_dataflow::move_paths::{IllegalMoveOriginKind, LookupResult, MovePathIndex};
3+
use rustc_middle::ty::{self, Ty};
4+
use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex};
55
use rustc_span::{BytePos, Span};
66

77
use crate::diagnostics::CapturedMessageOpt;
88
use crate::diagnostics::{DescribePlaceOpt, UseSpans};
99
use crate::prefixes::PrefixSet;
1010
use crate::MirBorrowckCtxt;
1111

12+
#[derive(Debug)]
13+
pub enum IllegalMoveOriginKind<'tcx> {
14+
/// Illegal move due to attempt to move from behind a reference.
15+
BorrowedContent {
16+
/// The place the reference refers to: if erroneous code was trying to
17+
/// move from `(*x).f` this will be `*x`.
18+
target_place: Place<'tcx>,
19+
},
20+
21+
/// Illegal move due to attempt to move from field of an ADT that
22+
/// implements `Drop`. Rust maintains invariant that all `Drop`
23+
/// ADT's remain fully-initialized so that user-defined destructor
24+
/// can safely read from all of the ADT's fields.
25+
InteriorOfTypeWithDestructor { container_ty: Ty<'tcx> },
26+
27+
/// Illegal move due to attempt to move out of a slice or array.
28+
InteriorOfSliceOrArray { ty: Ty<'tcx>, is_index: bool },
29+
}
30+
1231
#[derive(Debug)]
1332
pub(crate) struct MoveError<'tcx> {
1433
place: Place<'tcx>,

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ use std::rc::Rc;
4949
use rustc_mir_dataflow::impls::{
5050
EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
5151
};
52-
use rustc_mir_dataflow::move_paths::{
53-
IllegalMoveOriginKind, InitIndex, MoveOutIndex, MovePathIndex,
54-
};
52+
use rustc_mir_dataflow::move_paths::{InitIndex, MoveOutIndex, MovePathIndex};
5553
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveData};
5654
use rustc_mir_dataflow::Analysis;
5755
use rustc_mir_dataflow::MoveDataParamEnv;
5856

5957
use crate::session_diagnostics::VarNeedNotMut;
6058

61-
use self::diagnostics::{AccessKind, MoveError, RegionName};
59+
use self::diagnostics::{AccessKind, IllegalMoveOriginKind, MoveError, RegionName};
6260
use self::location::LocationTable;
6361
use self::prefixes::PrefixSet;
6462
use consumers::{BodyWithBorrowckFacts, ConsumerOptions};

compiler/rustc_mir_dataflow/src/move_paths/builder.rs

Lines changed: 25 additions & 42 deletions
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
};
@@ -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,12 +101,12 @@ 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);
103108
let Some(mut base) = data.rev_lookup.find_local(place.local) else {
104-
return Err(MoveError::UntrackedLocal);
109+
return MovePathResult::Error;
105110
};
106111

107112
// The move path index of the first union that we find. Once this is
@@ -118,12 +123,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
118123
match elem {
119124
ProjectionElem::Deref => match place_ty.kind() {
120125
ty::Ref(..) | ty::RawPtr(..) => {
121-
return Err(MoveError::cannot_move_out_of(
122-
self.loc,
123-
BorrowedContent {
124-
target_place: place_ref.project_deeper(&[elem], tcx),
125-
},
126-
));
126+
return MovePathResult::Error;
127127
}
128128
ty::Adt(adt, _) => {
129129
if !adt.is_box() {
@@ -159,10 +159,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
159159
ProjectionElem::Field(_, _) => match place_ty.kind() {
160160
ty::Adt(adt, _) => {
161161
if adt.has_dtor(tcx) {
162-
return Err(MoveError::cannot_move_out_of(
163-
self.loc,
164-
InteriorOfTypeWithDestructor { container_ty: place_ty },
165-
));
162+
return MovePathResult::Error;
166163
}
167164
if adt.is_union() {
168165
union_path.get_or_insert(base);
@@ -197,33 +194,15 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
197194
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
198195
match place_ty.kind() {
199196
ty::Slice(_) => {
200-
return Err(MoveError::cannot_move_out_of(
201-
self.loc,
202-
InteriorOfSliceOrArray {
203-
ty: place_ty,
204-
is_index: matches!(elem, ProjectionElem::Index(..)),
205-
},
206-
));
197+
return MovePathResult::Error;
207198
}
208199
ty::Array(_, _) => (),
209200
_ => bug!("Unexpected type {:#?}", place_ty.is_array()),
210201
}
211202
}
212203
ProjectionElem::Index(_) => match place_ty.kind() {
213-
ty::Array(..) => {
214-
return Err(MoveError::cannot_move_out_of(
215-
self.loc,
216-
InteriorOfSliceOrArray { ty: place_ty, is_index: true },
217-
));
218-
}
219-
ty::Slice(_) => {
220-
return Err(MoveError::cannot_move_out_of(
221-
self.loc,
222-
InteriorOfSliceOrArray {
223-
ty: place_ty,
224-
is_index: matches!(elem, ProjectionElem::Index(..)),
225-
},
226-
));
204+
ty::Array(..) | ty::Slice(_) => {
205+
return MovePathResult::Error;
227206
}
228207
_ => bug!("Unexpected type {place_ty:#?}"),
229208
},
@@ -252,9 +231,9 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
252231

253232
if let Some(base) = union_path {
254233
// Move out of union - always move the entire union.
255-
Err(MoveError::UnionMove { path: base })
234+
MovePathResult::Union(base)
256235
} else {
257-
Ok(base)
236+
MovePathResult::Path(base)
258237
}
259238
}
260239

@@ -543,12 +522,14 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
543522
let base_place =
544523
Place { local: place.local, projection: self.builder.tcx.mk_place_elems(base) };
545524
let base_path = match self.move_path_for(base_place) {
546-
Ok(path) => path,
547-
Err(MoveError::UnionMove { path }) => {
525+
MovePathResult::Path(path) => path,
526+
MovePathResult::Union(path) => {
548527
self.record_move(place, path);
549528
return;
550529
}
551-
Err(MoveError::IllegalMove { .. } | MoveError::UntrackedLocal) => return,
530+
MovePathResult::Error => {
531+
return;
532+
}
552533
};
553534
let base_ty = base_place.ty(self.builder.body, self.builder.tcx).ty;
554535
let len: u64 = match base_ty.kind() {
@@ -566,8 +547,10 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
566547
}
567548
} else {
568549
match self.move_path_for(place) {
569-
Ok(path) | Err(MoveError::UnionMove { path }) => self.record_move(place, path),
570-
Err(MoveError::IllegalMove { .. } | MoveError::UntrackedLocal) => {}
550+
MovePathResult::Path(path) | MovePathResult::Union(path) => {
551+
self.record_move(place, path)
552+
}
553+
MovePathResult::Error => {}
571554
};
572555
}
573556
}

compiler/rustc_mir_dataflow/src/move_paths/mod.rs

Lines changed: 1 addition & 40 deletions
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

@@ -346,45 +346,6 @@ impl<'tcx> MovePathLookup<'tcx> {
346346
}
347347
}
348348

349-
#[derive(Debug)]
350-
pub struct IllegalMoveOrigin<'tcx> {
351-
pub location: Location,
352-
pub kind: IllegalMoveOriginKind<'tcx>,
353-
}
354-
355-
#[derive(Debug)]
356-
pub enum IllegalMoveOriginKind<'tcx> {
357-
/// Illegal move due to attempt to move from behind a reference.
358-
BorrowedContent {
359-
/// The place the reference refers to: if erroneous code was trying to
360-
/// move from `(*x).f` this will be `*x`.
361-
target_place: Place<'tcx>,
362-
},
363-
364-
/// Illegal move due to attempt to move from field of an ADT that
365-
/// implements `Drop`. Rust maintains invariant that all `Drop`
366-
/// ADT's remain fully-initialized so that user-defined destructor
367-
/// can safely read from all of the ADT's fields.
368-
InteriorOfTypeWithDestructor { container_ty: Ty<'tcx> },
369-
370-
/// Illegal move due to attempt to move out of a slice or array.
371-
InteriorOfSliceOrArray { ty: Ty<'tcx>, is_index: bool },
372-
}
373-
374-
#[derive(Debug)]
375-
pub enum MoveError<'tcx> {
376-
IllegalMove { cannot_move_out_of: IllegalMoveOrigin<'tcx> },
377-
UnionMove { path: MovePathIndex },
378-
UntrackedLocal,
379-
}
380-
381-
impl<'tcx> MoveError<'tcx> {
382-
pub fn cannot_move_out_of(location: Location, kind: IllegalMoveOriginKind<'tcx>) -> Self {
383-
let origin = IllegalMoveOrigin { location, kind };
384-
MoveError::IllegalMove { cannot_move_out_of: origin }
385-
}
386-
}
387-
388349
impl<'tcx> MoveData<'tcx> {
389350
pub fn gather_moves(
390351
body: &Body<'tcx>,

0 commit comments

Comments
 (0)