@@ -297,8 +297,25 @@ impl<'db> UnsafeVisitor<'db> {
297
297
return ;
298
298
}
299
299
Expr :: Field { .. } => {
300
- if self . is_union_field_access ( * expr) {
301
- self . walk_union_field_for_raw_borrow ( * expr) ;
300
+ if matches ! (
301
+ self . infer. field_resolution( * expr) ,
302
+ Some ( Either :: Left ( FieldId { parent: VariantId :: UnionId ( _) , .. } ) )
303
+ ) {
304
+ match & self . body . exprs [ * expr] {
305
+ Expr :: Field { expr, .. } => {
306
+ // Visit the base expression (e.g., `self` in `self.field`) for safety,
307
+ // but don't trigger the union field access error since we're just
308
+ // creating a raw pointer, not actually reading the field
309
+ self . walk_expr ( * expr) ;
310
+ }
311
+ _ => {
312
+ self . body . walk_child_exprs_without_pats ( * expr, |child| {
313
+ // If it's not a field access for some reason, fall back to normal walking
314
+ // This shouldn't happen based on how this function is called
315
+ self . walk_expr ( child)
316
+ } ) ;
317
+ }
318
+ }
302
319
return ;
303
320
}
304
321
}
@@ -407,31 +424,4 @@ impl<'db> UnsafeVisitor<'db> {
407
424
}
408
425
}
409
426
}
410
-
411
- /// Check if an expression is accessing a union field
412
- fn is_union_field_access ( & self , expr_id : ExprId ) -> bool {
413
- matches ! (
414
- self . infer. field_resolution( expr_id) ,
415
- Some ( Either :: Left ( FieldId { parent: VariantId :: UnionId ( _) , .. } ) )
416
- )
417
- }
418
-
419
- /// Visit a union field access in the context of creating a raw reference
420
- /// This ensures we check safety of the base expression while allowing
421
- /// the raw pointer creation to the union field itself
422
- fn walk_union_field_for_raw_borrow ( & mut self , expr_id : ExprId ) {
423
- match & self . body . exprs [ expr_id] {
424
- Expr :: Field { expr, .. } => {
425
- // Visit the base expression (e.g., `self` in `self.field`) for safety,
426
- // but don't trigger the union field access error since we're just
427
- // creating a raw pointer, not actually reading the field
428
- self . walk_expr ( * expr) ;
429
- }
430
- _ => {
431
- // If it's not a field access for some reason, fall back to normal walking
432
- // This shouldn't happen based on how this function is called
433
- self . body . walk_child_exprs_without_pats ( expr_id, |child| self . walk_expr ( child) ) ;
434
- }
435
- }
436
- }
437
427
}
0 commit comments