Avoid math and use patterns to grab projection base

This commit is contained in:
Santiago Pastorino 2019-09-12 16:16:43 -03:00
parent 232a4a2881
commit 07a706ecf5
No known key found for this signature in database
GPG Key ID: 88C941CDA1D46432
5 changed files with 23 additions and 34 deletions

View File

@ -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,

View File

@ -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;

View File

@ -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() {
@ -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(

View File

@ -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,

View File

@ -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;
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();
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
place
} else {
unreachable!();
}
}
_ => bug!("Detected `&*` but didn't find `&*`!"),
};