about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/util/alignment.rs
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-09-05 23:06:35 +0200
committerRalf Jung <post@ralfj.de>2023-09-06 08:32:30 +0200
commitad7045e160b77a6cedea61e85dfbfd3497da05fd (patch)
tree748dda8087cf42b6eb8e33766e43fb5d278c96a6 /compiler/rustc_const_eval/src/util/alignment.rs
parent8b3435c10f458b840d9cba31f2f35586e9b31189 (diff)
downloadrust-ad7045e160b77a6cedea61e85dfbfd3497da05fd.tar.gz
rust-ad7045e160b77a6cedea61e85dfbfd3497da05fd.zip
still accept references to u8 slices and str in packed fields
Diffstat (limited to 'compiler/rustc_const_eval/src/util/alignment.rs')
-rw-r--r--compiler/rustc_const_eval/src/util/alignment.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/compiler/rustc_const_eval/src/util/alignment.rs b/compiler/rustc_const_eval/src/util/alignment.rs
index bdb08a9828e..8642dfccd78 100644
--- a/compiler/rustc_const_eval/src/util/alignment.rs
+++ b/compiler/rustc_const_eval/src/util/alignment.rs
@@ -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,