1
Fork 0

still accept references to u8 slices and str in packed fields

This commit is contained in:
Ralf Jung 2023-09-05 23:06:35 +02:00
parent 8b3435c10f
commit ad7045e160
3 changed files with 49 additions and 13 deletions

View file

@ -21,12 +21,18 @@ where
};
let ty = place.ty(local_decls, tcx).ty;
let unsized_tail = || tcx.struct_tail_with_normalize(ty, |ty| ty, || {});
match tcx.layout_of(param_env.and(ty)) {
Ok(layout) if layout.align.abi <= pack && layout.is_sized() => {
Ok(layout)
if layout.align.abi <= pack
&& (layout.is_sized()
|| matches!(unsized_tail().kind(), ty::Slice(..) | ty::Str)) =>
{
// If the packed alignment is greater or equal to the field alignment, the type won't be
// further disaligned.
// However we need to ensure the field is sized; for unsized fields, `layout.align` is
// just an approximation.
// just an approximation -- except when the unsized tail is a slice, where the alignment
// is fully determined by the type.
debug!(
"is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
place,