Skip to content

Commit

Permalink
Avoid math and use patterns to grab projection base
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Sep 13, 2019
1 parent 232a4a2 commit 07a706e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 35 deletions.
5 changes: 1 addition & 4 deletions src/librustc_mir/borrow_check/conflict_errors.rs
Expand Up @@ -1520,10 +1520,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
[] => {
StorageDeadOrDrop::LocalStorageDead
}
[.., elem] => {
// FIXME(spastorino) revisit when we get rid of Box
let base = &place.projection[..place.projection.len() - 1];

[base @ .., elem] => {
// FIXME(spastorino) make this iterate
let base_access = self.classify_drop_access_kind(PlaceRef {
base: place.base,
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -2324,14 +2324,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let mut place_projection = place_ref.projection;
let mut by_ref = false;

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

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

Expand Down
9 changes: 2 additions & 7 deletions src/librustc_mir/borrow_check/mutability_errors.rs
Expand Up @@ -82,11 +82,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {

PlaceRef {
base: _,
projection: [.., ProjectionElem::Deref],
projection: [base @ .., ProjectionElem::Deref],
} => {
// FIXME(spastorino) once released use box [base @ .., ProjectionElem::Deref]
let base = &the_place_err.projection[..the_place_err.projection.len() - 1];

if the_place_err.base == &PlaceBase::Local(Local::new(1)) &&
base.is_empty() &&
!self.upvars.is_empty() {
Expand Down Expand Up @@ -243,14 +240,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// after the field access).
PlaceRef {
base,
projection: [..,
projection: [base_proj @ ..,
ProjectionElem::Deref,
ProjectionElem::Field(field, _),
ProjectionElem::Deref,
],
} => {
let base_proj = &the_place_err.projection[..the_place_err.projection.len() - 3];

err.span_label(span, format!("cannot {ACT}", ACT = act));

if let Some((span, message)) = annotate_struct_field(
Expand Down
16 changes: 6 additions & 10 deletions src/librustc_mir/build/expr/as_rvalue.rs
Expand Up @@ -514,20 +514,16 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
Place {
ref base,
projection: box [.., ProjectionElem::Field(upvar_index, _)],
projection: box [ref base_proj @ .., ProjectionElem::Field(upvar_index, _)],
}
| Place {
ref base,
projection: box [.., ProjectionElem::Field(upvar_index, _), ProjectionElem::Deref],
projection: box [
ref base_proj @ ..,
ProjectionElem::Field(upvar_index, _),
ProjectionElem::Deref
],
} => {
let base_proj = if let ProjectionElem::Deref =
arg_place.projection[arg_place.projection.len() - 1]
{
&arg_place.projection[..arg_place.projection.len() - 2]
} else {
&arg_place.projection[..arg_place.projection.len() - 1]
};

let place = PlaceRef {
base,
projection: base_proj,
Expand Down
22 changes: 12 additions & 10 deletions src/librustc_mir/transform/instcombine.rs
Expand Up @@ -45,16 +45,18 @@ impl<'tcx> MutVisitor<'tcx> for InstCombineVisitor<'tcx> {
ref mut base,
projection: ref mut projection @ box [.., _],
}) => {
let [proj_l @ .., proj_r] = projection;

let place = Place {
// Replace with dummy
base: mem::replace(base, PlaceBase::Local(Local::new(0))),
projection: proj_l.to_vec().into_boxed_slice(),
};
*projection = proj_r.to_vec().into_boxed_slice();

place
if let box [proj_l @ .., proj_r] = projection {
let place = Place {
// Replace with dummy
base: mem::replace(base, PlaceBase::Local(Local::new(0))),
projection: proj_l.to_vec().into_boxed_slice(),
};
*projection = vec![proj_r.clone()].into_boxed_slice();

place
} else {
unreachable!();
}
}
_ => bug!("Detected `&*` but didn't find `&*`!"),
};
Expand Down

0 comments on commit 07a706e

Please sign in to comment.