Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit db68def

Browse files
committed
Make the borrow check mode an enum and update documentation
1 parent ddbb885 commit db68def

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::mir::*;
44
use rustc_middle::ty::{Ty, TyCtxt};
55
use rustc_session::Session;
66

7-
use crate::check_pointers::{PointerCheck, check_pointers};
7+
use crate::check_pointers::{BorrowCheckMode, PointerCheck, check_pointers};
88

99
pub(super) struct CheckAlignment;
1010

@@ -20,7 +20,13 @@ impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
2020
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
2121
// Skip trivially aligned place types.
2222
let excluded_pointees = [tcx.types.bool, tcx.types.i8, tcx.types.u8];
23-
check_pointers(tcx, body, &excluded_pointees, insert_alignment_check, false);
23+
check_pointers(
24+
tcx,
25+
body,
26+
&excluded_pointees,
27+
insert_alignment_check,
28+
BorrowCheckMode::ExcludeBorrows,
29+
);
2430
}
2531
}
2632

compiler/rustc_mir_transform/src/check_null.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::mir::*;
44
use rustc_middle::ty::{Ty, TyCtxt};
55
use rustc_session::Session;
66

7-
use crate::check_pointers::{PointerCheck, check_pointers};
7+
use crate::check_pointers::{BorrowCheckMode, PointerCheck, check_pointers};
88

99
pub(super) struct CheckNull;
1010

@@ -14,7 +14,7 @@ impl<'tcx> crate::MirPass<'tcx> for CheckNull {
1414
}
1515

1616
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
17-
check_pointers(tcx, body, &[], insert_null_check, true);
17+
check_pointers(tcx, body, &[], insert_null_check, BorrowCheckMode::IncludeBorrows);
1818
}
1919
}
2020

compiler/rustc_mir_transform/src/check_pointers.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

123133
impl<'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

146156
impl<'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

Comments
 (0)