@@ -12,14 +12,24 @@ pub(crate) struct PointerCheck<'tcx> {
1212 pub ( crate ) assert_kind : Box < AssertKind < Operand < ' tcx > > > ,
1313}
1414
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+
1524/// Utility for adding a check for read/write on every sized, raw pointer.
1625///
1726/// Visits every read/write access to a [Sized], raw pointer and inserts a
1827/// 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
2129/// `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+ ///
2333/// This utility takes care of the right order of blocks, the only thing a
2434/// caller must do in `on_finding` is:
2535/// - Append [Statement]s to `stmts`.
@@ -35,7 +45,7 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
3545 body : & mut Body < ' tcx > ,
3646 excluded_pointees : & ' a [ Ty < ' tcx > ] ,
3747 on_finding : F ,
38- check_for_borrows : bool ,
48+ borrow_check_mode : BorrowCheckMode ,
3949) where
4050 F : Fn (
4151 /* tcx: */ TyCtxt < ' tcx > ,
@@ -72,7 +82,7 @@ pub(crate) fn check_pointers<'a, 'tcx, F>(
7282 local_decls,
7383 typing_env,
7484 excluded_pointees,
75- check_for_borrows ,
85+ borrow_check_mode ,
7686 ) ;
7787 finder. visit_statement ( statement, location) ;
7888
@@ -117,7 +127,7 @@ struct PointerFinder<'a, 'tcx> {
117127 typing_env : ty:: TypingEnv < ' tcx > ,
118128 pointers : Vec < ( Place < ' tcx > , Ty < ' tcx > ) > ,
119129 excluded_pointees : & ' a [ Ty < ' tcx > ] ,
120- check_for_borrows : bool ,
130+ borrow_check_mode : BorrowCheckMode ,
121131}
122132
123133impl < ' a , ' tcx > PointerFinder < ' a , ' tcx > {
@@ -126,15 +136,15 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
126136 local_decls : & ' a mut LocalDecls < ' tcx > ,
127137 typing_env : ty:: TypingEnv < ' tcx > ,
128138 excluded_pointees : & ' a [ Ty < ' tcx > ] ,
129- check_for_borrows : bool ,
139+ borrow_check_mode : BorrowCheckMode ,
130140 ) -> Self {
131141 PointerFinder {
132142 tcx,
133143 local_decls,
134144 typing_env,
135145 excluded_pointees,
136146 pointers : Vec :: new ( ) ,
137- check_for_borrows ,
147+ borrow_check_mode ,
138148 }
139149 }
140150
@@ -145,8 +155,6 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
145155
146156impl < ' a , ' tcx > Visitor < ' tcx > for PointerFinder < ' a , ' tcx > {
147157 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.
150158 match context {
151159 PlaceContext :: MutatingUse (
152160 MutatingUseContext :: Store
@@ -159,7 +167,7 @@ impl<'a, 'tcx> Visitor<'tcx> for PointerFinder<'a, 'tcx> {
159167 ) => { }
160168 PlaceContext :: MutatingUse ( MutatingUseContext :: Borrow )
161169 | PlaceContext :: NonMutatingUse ( NonMutatingUseContext :: SharedBorrow )
162- if self . check_for_borrows => { }
170+ if matches ! ( self . borrow_check_mode , BorrowCheckMode :: IncludeBorrows ) => { }
163171 _ => {
164172 return ;
165173 }
0 commit comments