@@ -15,16 +15,19 @@ use rustc::ty::{self, TyCtxt};
15
15
16
16
/// Extension methods for the `Place` type.
17
17
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 ;
20
23
21
24
/// If this is a place like `x.f.g`, returns the local
22
25
/// `x`. Returns `None` if this is based in a static.
23
26
fn root_local ( & self ) -> Option < Local > ;
24
27
}
25
28
26
29
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 {
28
31
match self {
29
32
Place :: Promoted ( _) |
30
33
Place :: Local ( _) => false ,
@@ -36,12 +39,23 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
36
39
| ProjectionElem :: Downcast ( ..)
37
40
| ProjectionElem :: Subslice { .. }
38
41
| ProjectionElem :: ConstantIndex { .. }
39
- | ProjectionElem :: Index ( _) => proj. base . is_unsafe_place ( tcx, mir) ,
42
+ | ProjectionElem :: Index ( _) => proj. base . ignore_borrow ( tcx, mir) ,
43
+
40
44
ProjectionElem :: Deref => {
41
45
let ty = proj. base . ty ( mir, tcx) . to_ty ( tcx) ;
42
46
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) ,
45
59
}
46
60
}
47
61
} ,
0 commit comments