@@ -15,16 +15,19 @@ use rustc::ty::{self, TyCtxt};
1515
1616/// Extension methods for the `Place` type.
1717crate 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
2629impl < ' 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 } ,
0 commit comments