about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2023-12-04 21:19:44 +0900
committerGitHub <noreply@github.com>2023-12-04 21:19:44 +0900
commit87625dbf2b233be1105524d418a9628fc71adeef (patch)
tree6544ecab81601a63c22440a4d77c22556092f285 /compiler/rustc_const_eval/src/interpret
parentda30882eb3f33813656e3079a144a2a5a1f5cee2 (diff)
parentef15a8182ba0b6b7b5d6abb9d00eeb93f7cf0247 (diff)
downloadrust-87625dbf2b233be1105524d418a9628fc71adeef.tar.gz
rust-87625dbf2b233be1105524d418a9628fc71adeef.zip
Rollup merge of #118540 - RalfJung:unsized-packed-offset, r=TaKO8Ki
codegen, miri: fix computing the offset of an unsized field in a packed struct

`#[repr(packed)]`  strikes again.

Fixes https://github.com/rust-lang/rust/issues/118537
Fixes https://github.com/rust-lang/miri/issues/3200

`@bjorn3` I assume cranelift needs the same fix.
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret')
-rw-r--r--compiler/rustc_const_eval/src/interpret/projection.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_const_eval/src/interpret/projection.rs b/compiler/rustc_const_eval/src/interpret/projection.rs
index c8977aac0fc..4d9e296d544 100644
--- a/compiler/rustc_const_eval/src/interpret/projection.rs
+++ b/compiler/rustc_const_eval/src/interpret/projection.rs
@@ -163,7 +163,17 @@ where
             // With custom DSTS, this *will* execute user-defined code, but the same
             // happens at run-time so that's okay.
             match self.size_and_align_of(&base_meta, &field_layout)? {
-                Some((_, align)) => (base_meta, offset.align_to(align)),
+                Some((_, align)) => {
+                    // For packed types, we need to cap alignment.
+                    let align = if let ty::Adt(def, _) = base.layout().ty.kind()
+                        && let Some(packed) = def.repr().pack
+                    {
+                        align.min(packed)
+                    } else {
+                        align
+                    };
+                    (base_meta, offset.align_to(align))
+                }
                 None => {
                     // For unsized types with an extern type tail we perform no adjustments.
                     // NOTE: keep this in sync with `PlaceRef::project_field` in the codegen backend.