Skip to content

Commit 07d5971

Browse files
committed
move borrow-of-packed-field unsafety check out of loop
1 parent 8453936 commit 07d5971

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/librustc_mir/transform/check_unsafety.rs

+19-19
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
184184
}
185185

186186
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
187-
// prevent
187+
// On types with `scalar_valid_range`, prevent
188188
// * `&mut x.field`
189189
// * `x.field = y;`
190190
// * `&x.field` if `field`'s type has interior mutability
@@ -194,26 +194,26 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
194194
self.check_mut_borrowing_layout_constrained_field(*place, context.is_mutating_use());
195195
}
196196

197+
if context.is_borrow() {
198+
if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
199+
let source_info = self.source_info;
200+
let lint_root = self.body.source_scopes[source_info.scope]
201+
.local_data
202+
.as_ref()
203+
.assert_crate_local()
204+
.lint_root;
205+
self.require_unsafe(
206+
"borrow of packed field",
207+
"fields of packed structs might be misaligned: dereferencing a \
208+
misaligned pointer or even just creating a misaligned reference \
209+
is undefined behavior",
210+
UnsafetyViolationKind::BorrowPacked(lint_root),
211+
);
212+
}
213+
}
214+
197215
for (i, elem) in place.projection.iter().enumerate() {
198216
let proj_base = &place.projection[..i];
199-
200-
if context.is_borrow() {
201-
if util::is_disaligned(self.tcx, self.body, self.param_env, *place) {
202-
let source_info = self.source_info;
203-
let lint_root = self.body.source_scopes[source_info.scope]
204-
.local_data
205-
.as_ref()
206-
.assert_crate_local()
207-
.lint_root;
208-
self.require_unsafe(
209-
"borrow of packed field",
210-
"fields of packed structs might be misaligned: dereferencing a \
211-
misaligned pointer or even just creating a misaligned reference \
212-
is undefined behavior",
213-
UnsafetyViolationKind::BorrowPacked(lint_root),
214-
);
215-
}
216-
}
217217
let old_source_info = self.source_info;
218218
if let [] = proj_base {
219219
let decl = &self.body.local_decls[place.local];

0 commit comments

Comments
 (0)