Skip to content

Commit

Permalink
Avoid cloning Place in check_access_permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jul 20, 2019
1 parent 34e3b70 commit 95e727a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
12 changes: 6 additions & 6 deletions src/librustc_mir/borrow_check/mod.rs
Expand Up @@ -1996,9 +1996,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
self.report_mutability_error(
place,
span,
&Place {
base: _place_err.0.clone(),
projection: _place_err.1.clone(),
PlaceRef {
base: _place_err.0,
projection: _place_err.1,
},
error_access,
location,
Expand Down Expand Up @@ -2033,9 +2033,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
self.report_mutability_error(
place,
span,
&Place {
base: the_place_err.0.clone(),
projection: the_place_err.1.clone(),
PlaceRef {
base: the_place_err.0,
projection: the_place_err.1,
},
error_access,
location,
Expand Down
52 changes: 30 additions & 22 deletions src/librustc_mir/borrow_check/mutability_errors.rs
Expand Up @@ -2,7 +2,9 @@ use core::unicode::property::Pattern_White_Space;
use rustc::hir;
use rustc::hir::Node;
use rustc::mir::{self, BindingForm, ClearCrossCrate, Local, Location, Body};
use rustc::mir::{Mutability, Place, PlaceBase, Projection, ProjectionElem, Static, StaticKind};
use rustc::mir::{
Mutability, Place, PlaceRef, PlaceBase, Projection, ProjectionElem, Static, StaticKind
};
use rustc::ty::{self, Ty, TyCtxt};
use rustc_data_structures::indexed_vec::Idx;
use syntax_pos::Span;
Expand All @@ -25,7 +27,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
&mut self,
access_place: &Place<'tcx>,
span: Span,
the_place_err: &Place<'tcx>,
the_place_err: PlaceRef<'cx, 'tcx>,
error_access: AccessKind,
location: Location,
) {
Expand All @@ -44,7 +46,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);

match the_place_err {
Place {
PlaceRef {
base: PlaceBase::Local(local),
projection: None,
} => {
Expand All @@ -62,7 +64,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

Place {
PlaceRef {
base: _,
projection:
Some(box Projection {
Expand All @@ -83,21 +85,27 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

Place {
PlaceRef {
base: _,
projection:
Some(box Projection {
base,
elem: ProjectionElem::Deref,
}),
} => {
if the_place_err.base == PlaceBase::Local(Local::new(1)) &&
if the_place_err.base == &PlaceBase::Local(Local::new(1)) &&
base.is_none() &&
!self.upvars.is_empty() {
item_msg = format!("`{}`", access_place_desc.unwrap());
debug_assert!(self.body.local_decls[Local::new(1)].ty.is_region_ptr());
debug_assert!(is_closure_or_generator(
the_place_err.ty(self.body, self.infcx.tcx).ty
Place::ty_from(
the_place_err.base,
the_place_err.projection,
self.body,
self.infcx.tcx
)
.ty
));

reason =
Expand Down Expand Up @@ -138,7 +146,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

Place {
PlaceRef {
base:
PlaceBase::Static(box Static {
kind: StaticKind::Promoted(_),
Expand All @@ -147,7 +155,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
projection: None,
} => unreachable!(),

Place {
PlaceRef {
base:
PlaceBase::Static(box Static {
kind: StaticKind::Static(def_id),
Expand All @@ -168,30 +176,30 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

Place {
PlaceRef {
base: _,
projection:
Some(box Projection {
base: _,
elem: ProjectionElem::Index(_),
}),
}
| Place {
| PlaceRef {
base: _,
projection:
Some(box Projection {
base: _,
elem: ProjectionElem::ConstantIndex { .. },
}),
}
| Place {
| PlaceRef {
base: _,
projection: Some(box Projection {
base: _,
elem: ProjectionElem::Subslice { .. },
}),
}
| Place {
| PlaceRef {
base: _,
projection: Some(box Projection {
base: _,
Expand Down Expand Up @@ -252,7 +260,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// something like `*((*_1).0`. The local that we get will be a reference to the
// struct we've got a field access of (it must be a reference since there's a deref
// after the field access).
Place {
PlaceRef {
base,
projection: Some(box Projection {
base: Some(box Projection {
Expand Down Expand Up @@ -282,7 +290,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
},

// Suggest removing a `&mut` from the use of a mutable reference.
Place {
PlaceRef {
base: PlaceBase::Local(local),
projection: None,
} if {
Expand Down Expand Up @@ -318,7 +326,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {

// We want to suggest users use `let mut` for local (user
// variable) mutations...
Place {
PlaceRef {
base: PlaceBase::Local(local),
projection: None,
} if self.body.local_decls[*local].can_be_made_mutable() => {
Expand All @@ -339,7 +347,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}

// Also suggest adding mut for upvars
Place {
PlaceRef {
base,
projection: Some(box Projection {
base: proj_base,
Expand Down Expand Up @@ -375,7 +383,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
// complete hack to approximate old AST-borrowck
// diagnostic: if the span starts with a mutable borrow of
// a local variable, then just suggest the user remove it.
Place {
PlaceRef {
base: PlaceBase::Local(_),
projection: None,
} if {
Expand All @@ -390,7 +398,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
err.span_label(span, "try removing `&mut` here");
}

Place {
PlaceRef {
base: PlaceBase::Local(local),
projection: Some(box Projection {
base: None,
Expand All @@ -417,7 +425,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
//
// FIXME: can this case be generalized to work for an
// arbitrary base for the projection?
Place {
PlaceRef {
base: PlaceBase::Local(local),
projection: Some(box Projection {
base: None,
Expand Down Expand Up @@ -500,7 +508,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}
}

Place {
PlaceRef {
base,
projection: Some(box Projection {
base: None,
Expand All @@ -517,7 +525,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
}

Place {
PlaceRef {
base: _,
projection: Some(box Projection {
base: _,
Expand Down

0 comments on commit 95e727a

Please sign in to comment.