Skip to content

Commit 07a706e

Browse files
committed
Avoid math and use patterns to grab projection base
1 parent 232a4a2 commit 07a706e

File tree

5 files changed

+24
-35
lines changed

5 files changed

+24
-35
lines changed

src/librustc_mir/borrow_check/conflict_errors.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1520,10 +1520,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15201520
[] => {
15211521
StorageDeadOrDrop::LocalStorageDead
15221522
}
1523-
[.., elem] => {
1524-
// FIXME(spastorino) revisit when we get rid of Box
1525-
let base = &place.projection[..place.projection.len() - 1];
1526-
1523+
[base @ .., elem] => {
15271524
// FIXME(spastorino) make this iterate
15281525
let base_access = self.classify_drop_access_kind(PlaceRef {
15291526
base: place.base,

src/librustc_mir/borrow_check/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2324,14 +2324,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
23242324
let mut place_projection = place_ref.projection;
23252325
let mut by_ref = false;
23262326

2327-
if let [.., ProjectionElem::Deref] = place_projection {
2328-
place_projection = &place_projection[..place_projection.len() - 1];
2327+
if let [proj_base @ .., ProjectionElem::Deref] = place_projection {
2328+
place_projection = proj_base;
23292329
by_ref = true;
23302330
}
23312331

23322332
match place_projection {
2333-
[.., ProjectionElem::Field(field, _ty)] => {
2334-
let base = &place_projection[..place_projection.len() - 1];
2333+
[base @ .., ProjectionElem::Field(field, _ty)] => {
23352334
let tcx = self.infcx.tcx;
23362335
let base_ty = Place::ty_from(place_ref.base, base, self.body, tcx).ty;
23372336

src/librustc_mir/borrow_check/mutability_errors.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
8282

8383
PlaceRef {
8484
base: _,
85-
projection: [.., ProjectionElem::Deref],
85+
projection: [base @ .., ProjectionElem::Deref],
8686
} => {
87-
// FIXME(spastorino) once released use box [base @ .., ProjectionElem::Deref]
88-
let base = &the_place_err.projection[..the_place_err.projection.len() - 1];
89-
9087
if the_place_err.base == &PlaceBase::Local(Local::new(1)) &&
9188
base.is_empty() &&
9289
!self.upvars.is_empty() {
@@ -243,14 +240,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
243240
// after the field access).
244241
PlaceRef {
245242
base,
246-
projection: [..,
243+
projection: [base_proj @ ..,
247244
ProjectionElem::Deref,
248245
ProjectionElem::Field(field, _),
249246
ProjectionElem::Deref,
250247
],
251248
} => {
252-
let base_proj = &the_place_err.projection[..the_place_err.projection.len() - 3];
253-
254249
err.span_label(span, format!("cannot {ACT}", ACT = act));
255250

256251
if let Some((span, message)) = annotate_struct_field(

src/librustc_mir/build/expr/as_rvalue.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -514,20 +514,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
514514
}
515515
Place {
516516
ref base,
517-
projection: box [.., ProjectionElem::Field(upvar_index, _)],
517+
projection: box [ref base_proj @ .., ProjectionElem::Field(upvar_index, _)],
518518
}
519519
| Place {
520520
ref base,
521-
projection: box [.., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
521+
projection: box [
522+
ref base_proj @ ..,
523+
ProjectionElem::Field(upvar_index, _),
524+
ProjectionElem::Deref
525+
],
522526
} => {
523-
let base_proj = if let ProjectionElem::Deref =
524-
arg_place.projection[arg_place.projection.len() - 1]
525-
{
526-
&arg_place.projection[..arg_place.projection.len() - 2]
527-
} else {
528-
&arg_place.projection[..arg_place.projection.len() - 1]
529-
};
530-
531527
let place = PlaceRef {
532528
base,
533529
projection: base_proj,

src/librustc_mir/transform/instcombine.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
4545
ref mut base,
4646
projection: ref mut projection @ box [.., _],
4747
}) => {
48-
let [proj_l @ .., proj_r] = projection;
49-
50-
let place = Place {
51-
// Replace with dummy
52-
base: mem::replace(base, PlaceBase::Local(Local::new(0))),
53-
projection: proj_l.to_vec().into_boxed_slice(),
54-
};
55-
*projection = proj_r.to_vec().into_boxed_slice();
56-
57-
place
48+
if let box [proj_l @ .., proj_r] = projection {
49+
let place = Place {
50+
// Replace with dummy
51+
base: mem::replace(base, PlaceBase::Local(Local::new(0))),
52+
projection: proj_l.to_vec().into_boxed_slice(),
53+
};
54+
*projection = vec![proj_r.clone()].into_boxed_slice();
55+
56+
place
57+
} else {
58+
unreachable!();
59+
}
5860
}
5961
_ => bug!("Detected `&*` but didn't find `&*`!"),
6062
};

0 commit comments

Comments
 (0)