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

Commit 68302ec

Browse files
committed
Add requested changes
1 parent db68def commit 68302ec

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

compiler/rustc_mir_transform/src/check_alignment.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ 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+
24+
// We have to exclude borrows here: in `&x.field`, the exact
25+
// requirement is that the final reference must be aligned, but
26+
// `check_pointers` would check that `x` is aligned, which would be wrong.
2327
check_pointers(
2428
tcx,
2529
body,

compiler/rustc_mir_transform/src/check_pointers.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,29 +151,34 @@ impl<'a, 'tcx> PointerFinder<'a, 'tcx> {
151151
fn into_found_pointers(self) -> Vec<(Place<'tcx>, Ty<'tcx>)> {
152152
self.pointers
153153
}
154-
}
155154

156-
impl<'a, 'tcx> Visitor<'tcx> for PointerFinder<'a, 'tcx> {
157-
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
155+
/// Whether or not we should visit a [Place] with [PlaceContext].
156+
///
157+
/// We generally only visit Reads/Writes to a place and only Borrows if
158+
/// requested.
159+
fn should_visit_place(&self, context: PlaceContext) -> bool {
158160
match context {
159161
PlaceContext::MutatingUse(
160162
MutatingUseContext::Store
161163
| MutatingUseContext::Call
162164
| MutatingUseContext::Yield
163165
| MutatingUseContext::Drop,
164-
) => {}
166+
) => true,
165167
PlaceContext::NonMutatingUse(
166168
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
167-
) => {}
169+
) => true,
168170
PlaceContext::MutatingUse(MutatingUseContext::Borrow)
169-
| PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow)
170-
if matches!(self.borrow_check_mode, BorrowCheckMode::IncludeBorrows) => {}
171-
_ => {
172-
return;
171+
| PlaceContext::NonMutatingUse(NonMutatingUseContext::SharedBorrow) => {
172+
matches!(self.borrow_check_mode, BorrowCheckMode::IncludeBorrows)
173173
}
174+
_ => false,
174175
}
176+
}
177+
}
175178

176-
if !place.is_indirect() {
179+
impl<'a, 'tcx> Visitor<'tcx> for PointerFinder<'a, 'tcx> {
180+
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
181+
if !self.should_visit_place(context) && !place.is_indirect() {
177182
return;
178183
}
179184

0 commit comments

Comments
 (0)