Skip to content

Commit 1bae4f5

Browse files
committed
optimize redundant borrows
1 parent 3b7989d commit 1bae4f5

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/librustc_mir/borrow_check/borrow_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'gcx, 'tcx> {
159159
location: mir::Location,
160160
) {
161161
if let mir::Rvalue::Ref(region, kind, ref borrowed_place) = *rvalue {
162-
if borrowed_place.is_unsafe_place(self.tcx, self.mir) {
162+
if borrowed_place.ignore_borrow(self.tcx, self.mir) {
163163
return;
164164
}
165165

src/librustc_mir/borrow_check/place_ext.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ use rustc::ty::{self, TyCtxt};
1515

1616
/// Extension methods for the `Place` type.
1717
crate trait PlaceExt<'tcx> {
18-
/// True if this is a deref of a raw pointer.
19-
fn is_unsafe_place(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool;
18+
/// Returns true if we can safely ignore borrows of this place.
19+
/// This is true whenever there is no action that the user can do
20+
/// to the place `self` that would invalidate the borrow. This is true
21+
/// for borrows of raw pointer dereferents as well as shared references.
22+
fn ignore_borrow(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool;
2023

2124
/// If this is a place like `x.f.g`, returns the local
2225
/// `x`. Returns `None` if this is based in a static.
2326
fn root_local(&self) -> Option<Local>;
2427
}
2528

2629
impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
27-
fn is_unsafe_place(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool {
30+
fn ignore_borrow(&self, tcx: TyCtxt<'_, '_, 'tcx>, mir: &Mir<'tcx>) -> bool {
2831
match self {
2932
Place::Promoted(_) |
3033
Place::Local(_) => false,
@@ -36,12 +39,23 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
3639
| ProjectionElem::Downcast(..)
3740
| ProjectionElem::Subslice { .. }
3841
| ProjectionElem::ConstantIndex { .. }
39-
| ProjectionElem::Index(_) => proj.base.is_unsafe_place(tcx, mir),
42+
| ProjectionElem::Index(_) => proj.base.ignore_borrow(tcx, mir),
43+
4044
ProjectionElem::Deref => {
4145
let ty = proj.base.ty(mir, tcx).to_ty(tcx);
4246
match ty.sty {
43-
ty::TyRawPtr(..) => true,
44-
_ => proj.base.is_unsafe_place(tcx, mir),
47+
// For both derefs of raw pointers and `&T`
48+
// references, the original path is `Copy` and
49+
// therefore not significant. In particular,
50+
// there is nothing the user can do to the
51+
// original path that would invalidate the
52+
// newly created reference -- and if there
53+
// were, then the user could have copied the
54+
// original path into a new variable and
55+
// borrowed *that* one, leaving the original
56+
// path unborrowed.
57+
ty::TyRawPtr(..) | ty::TyRef(_, _, hir::MutImmutable) => true,
58+
_ => proj.base.ignore_borrow(tcx, mir),
4559
}
4660
}
4761
},

src/librustc_mir/dataflow/impls/borrows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl<'a, 'gcx, 'tcx> BitDenotation for Borrows<'a, 'gcx, 'tcx> {
233233
// propagate_call_return method.
234234

235235
if let mir::Rvalue::Ref(region, _, ref place) = *rhs {
236-
if place.is_unsafe_place(self.tcx, self.mir) { return; }
236+
if place.ignore_borrow(self.tcx, self.mir) { return; }
237237
let index = self.borrow_set.location_map.get(&location).unwrap_or_else(|| {
238238
panic!("could not find BorrowIndex for location {:?}", location);
239239
});

0 commit comments

Comments
 (0)