about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2023-07-31 17:29:30 +0200
committerRalf Jung <post@ralfj.de>2023-07-31 19:22:14 +0200
commitb169ee7c1a0a72576df06d7b01699703f6ed65a8 (patch)
tree8e0d1884edab48749d5fe475cea76c49a1f679eb
parent4c9ac1e93b43389890d0d77eeb479b79da62da25 (diff)
downloadrust-b169ee7c1a0a72576df06d7b01699703f6ed65a8.tar.gz
rust-b169ee7c1a0a72576df06d7b01699703f6ed65a8.zip
fix alignment handling for Repeat expressions
-rw-r--r--compiler/rustc_const_eval/src/interpret/step.rs8
-rw-r--r--src/tools/miri/tests/pass/align_repeat_into_packed_field.rs22
-rw-r--r--src/tools/miri/tests/pass/align_repeat_into_well_aligned_array.rs (renamed from src/tools/miri/tests/pass/issues/issue-miri-1925.rs)2
3 files changed, 28 insertions, 4 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/step.rs b/compiler/rustc_const_eval/src/interpret/step.rs
index 91341ddacd1..ead172f04a3 100644
--- a/compiler/rustc_const_eval/src/interpret/step.rs
+++ b/compiler/rustc_const_eval/src/interpret/step.rs
@@ -208,13 +208,13 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
                     let rest_ptr = first_ptr.offset(elem_size, self)?;
                     // For the alignment of `rest_ptr`, we crucially do *not* use `first.align` as
                     // that place might be more aligned than its type mandates (a `u8` array could
-                    // be 4-aligned if it sits at the right spot in a struct). Instead we use
-                    // `first.layout.align`, i.e., the alignment given by the type.
+                    // be 4-aligned if it sits at the right spot in a struct). We have to also factor
+                    // in element size.
                     self.mem_copy_repeatedly(
                         first_ptr,
-                        first.align,
+                        dest.align,
                         rest_ptr,
-                        first.layout.align.abi,
+                        dest.align.restrict_for_offset(elem_size),
                         elem_size,
                         length - 1,
                         /*nonoverlapping:*/ true,
diff --git a/src/tools/miri/tests/pass/align_repeat_into_packed_field.rs b/src/tools/miri/tests/pass/align_repeat_into_packed_field.rs
new file mode 100644
index 00000000000..3affb204205
--- /dev/null
+++ b/src/tools/miri/tests/pass/align_repeat_into_packed_field.rs
@@ -0,0 +1,22 @@
+#![feature(custom_mir, core_intrinsics)]
+use std::intrinsics::mir::*;
+
+#[repr(packed)]
+struct S { field: [u32; 2] }
+
+#[custom_mir(dialect = "runtime", phase = "optimized")]
+fn test() { mir! {
+    let s: S;
+    {
+        // Store a repeat expression directly into a field of a packed struct.
+        s.field = [0; 2];
+        Return()
+    }
+} }
+
+fn main() {
+    // Run this a bunch of time to make sure it doesn't pass by chance.
+    for _ in 0..20 {
+        test();
+    }
+}
diff --git a/src/tools/miri/tests/pass/issues/issue-miri-1925.rs b/src/tools/miri/tests/pass/align_repeat_into_well_aligned_array.rs
index 86556813491..735251039f7 100644
--- a/src/tools/miri/tests/pass/issues/issue-miri-1925.rs
+++ b/src/tools/miri/tests/pass/align_repeat_into_well_aligned_array.rs
@@ -4,6 +4,8 @@ use std::mem::size_of;
 
 fn main() {
     let mut a = Params::new();
+    // The array itself here happens to be quite well-aligned, but not all its elements have that
+    // large alignment and we better make sure that is still accepted by Miri.
     a.key_block = [0; BLOCKBYTES];
 }