@@ -184,7 +184,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
184
184
}
185
185
186
186
fn visit_place ( & mut self , place : & Place < ' tcx > , context : PlaceContext , _location : Location ) {
187
- // prevent
187
+ // On types with `scalar_valid_range`, prevent
188
188
// * `&mut x.field`
189
189
// * `x.field = y;`
190
190
// * `&x.field` if `field`'s type has interior mutability
@@ -194,26 +194,26 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
194
194
self . check_mut_borrowing_layout_constrained_field ( * place, context. is_mutating_use ( ) ) ;
195
195
}
196
196
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
+
197
215
for ( i, elem) in place. projection . iter ( ) . enumerate ( ) {
198
216
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
- }
217
217
let old_source_info = self . source_info ;
218
218
if let [ ] = proj_base {
219
219
let decl = & self . body . local_decls [ place. local ] ;
0 commit comments