@@ -12,14 +12,24 @@ pub(crate) struct PointerCheck<'tcx> {
12
12
pub ( crate ) assert_kind : Box < AssertKind < Operand < ' tcx > > > ,
13
13
}
14
14
15
+ /// Indicates whether we insert the checks for borrow places of a raw pointer.
16
+ /// Concretely places with [MutatingUseContext::Borrow] or
17
+ /// [NonMutatingUseContext::SharedBorrow].
18
+ #[ derive( Copy , Clone ) ]
19
+ pub ( crate ) enum BorrowCheckMode {
20
+ IncludeBorrows ,
21
+ ExcludeBorrows ,
22
+ }
23
+
15
24
/// Utility for adding a check for read/write on every sized, raw pointer.
16
25
///
17
26
/// Visits every read/write access to a [Sized], raw pointer and inserts a
18
27
/// new basic block directly before the pointer access. (Read/write accesses
19
- /// are determined by the `PlaceContext` of the MIR visitor. In particular,
20
- /// uses of pointers in borrow expressions are *not* visited). Then calls
28
+ /// are determined by the `PlaceContext` of the MIR visitor.) Then calls
21
29
/// `on_finding` to insert the actual logic for a pointer check (e.g. check for
22
- /// alignment).
30
+ /// alignment). A check can choose to be inserted for (mutable) borrows of
31
+ /// raw pointers via the `borrow_check_mode` parameter.
32
+ ///
23
33
/// This utility takes care of the right order of blocks, the only thing a
24
34
/// caller must do in `on_finding` is:
25
35
/// - Append [Statement]s to `stmts`.
@@ -35,7 +45,7 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
35
45
body : & mut Body < ' tcx > ,
36
46
excluded_pointees : & ' a [ Ty < ' tcx > ] ,
37
47
on_finding : F ,
38
- check_for_borrows : bool ,
48
+ borrow_check_mode : BorrowCheckMode ,
39
49
) where
40
50
F : Fn (
41
51
/* tcx: */ TyCtxt < ' tcx > ,
@@ -72,7 +82,7 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
72
82
local_decls,
73
83
typing_env,
74
84
excluded_pointees,
75
- check_for_borrows ,
85
+ borrow_check_mode ,
76
86
) ;
77
87
finder. visit_statement ( statement, location) ;
78
88
@@ -117,7 +127,7 @@ struct PointerFinder<'a, 'tcx> {
117
127
typing_env : ty:: TypingEnv < ' tcx > ,
118
128
pointers : Vec < ( Place < ' tcx > , Ty < ' tcx > ) > ,
119
129
excluded_pointees : & ' a [ Ty < ' tcx > ] ,
120
- check_for_borrows : bool ,
130
+ borrow_check_mode : BorrowCheckMode ,
121
131
}
122
132
123
133
impl < ' a , ' tcx > PointerFinder < ' a , ' tcx > {
@@ -126,15 +136,15 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
126
136
local_decls : & ' a mut LocalDecls < ' tcx > ,
127
137
typing_env : ty:: TypingEnv < ' tcx > ,
128
138
excluded_pointees : & ' a [ Ty < ' tcx > ] ,
129
- check_for_borrows : bool ,
139
+ borrow_check_mode : BorrowCheckMode ,
130
140
) -> Self {
131
141
PointerFinder {
132
142
tcx,
133
143
local_decls,
134
144
typing_env,
135
145
excluded_pointees,
136
146
pointers : Vec :: new ( ) ,
137
- check_for_borrows ,
147
+ borrow_check_mode ,
138
148
}
139
149
}
140
150
@@ -145,8 +155,6 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
145
155
146
156
impl < ' a , ' tcx > Visitor < ' tcx > for PointerFinder < ' a , ' tcx > {
147
157
fn visit_place ( & mut self , place : & Place < ' tcx > , context : PlaceContext , location : Location ) {
148
- // We want to only check reads and writes to Places, so we specifically exclude
149
- // Borrow and RawBorrow.
150
158
match context {
151
159
PlaceContext :: MutatingUse (
152
160
MutatingUseContext :: Store
@@ -159,7 +167,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PointerFinder<'a, 'tcx> {
159
167
) => { }
160
168
PlaceContext :: MutatingUse ( MutatingUseContext :: Borrow )
161
169
| PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: SharedBorrow )
162
- if self . check_for_borrows => { }
170
+ if matches ! ( self . borrow_check_mode , BorrowCheckMode :: IncludeBorrows ) => { }
163
171
_ => {
164
172
return ;
165
173
}
0 commit comments