Skip to content

Commit 7833133

Browse files
authored
Unrolled build for rust-lang#124908
Rollup merge of rust-lang#124908 - saethlin:ref-casting_bigger_place_projection, r=fee1-dead Handle field projections like slice indexing in invalid_reference_casting r? `@Urgau` I saw the implementation in rust-lang#124761, and I was wondering if we also need to handle field access. We do. Without this PR, we get this errant diagnostic: ``` error: casting references to a bigger memory layout than the backing allocation is undefined behavior, even if the reference is unused --> /home/ben/rust/tests/ui/lint/reference_casting.rs:262:18 | LL | let r = &mut v.0; | --- backing allocation comes from here LL | let ptr = r as *mut i32 as *mut Vec3<i32>; | ------------------------------- casting happend here LL | unsafe { *ptr = Vec3(0, 0, 0) } | ^^^^^^^^^^^^^^^^^^^^ | = note: casting from `i32` (4 bytes) to `Vec3<i32>` (12 bytes) ```
2 parents 37dc766 + 0ca1a94 commit 7833133

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

compiler/rustc_lint/src/reference_casting.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
202202

203203
// if the current expr looks like this `&mut expr[index]` then just looking
204204
// at `expr[index]` won't give us the underlying allocation, so we just skip it
205-
if let ExprKind::Index(..) = e_alloc.kind {
205+
// the same logic applies field access like `&mut expr.field`
206+
if let ExprKind::Index(..) | ExprKind::Field(..) = e_alloc.kind {
206207
return None;
207208
}
208209

tests/ui/lint/reference_casting.rs

+6
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ unsafe fn bigger_layout() {
255255
let a3 = a2 as *mut u64;
256256
unsafe { *a3 = 3 };
257257
}
258+
259+
unsafe fn field_access(v: &mut Vec3<i32>) {
260+
let r = &mut v.0;
261+
let ptr = r as *mut i32 as *mut Vec3<i32>;
262+
unsafe { *ptr = Vec3(0, 0, 0) }
263+
}
258264
}
259265

260266
const RAW_PTR: *mut u8 = 1 as *mut u8;

0 commit comments

Comments
 (0)