Allow borrowing array elements from packed structs with ABI align <= packed align by hns1971 · Pull Request #145419 · rust-lang/rust (original) (raw)

Thanks for the detailed feedback!

I agree with the soundness concern and with using the term “misaligned”.
I will:

To still address the motivating case without introducing false negatives, I’ll only add a very small, layout-free special-case:
when ty.kind() is Array(elem, _) and elem is u8 or i8 (which have ABI alignment 1 by definition, independent of the array length), we treat the borrow as not potentially misaligned. All other cases remain conservative and return true when layout_of(ty) fails.

This fixes the [u8; CAP] packed-struct case while keeping the function strictly conservative for any type whose ABI alignment could exceed the packed alignment.

If you’d prefer the even stricter variant (no special-casing at all), I can drop the exception and keep returning true on layout_of failure across the board.