diff options
| author | Ralf Jung <post@ralfj.de> | 2023-12-02 14:24:17 +0100 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-12-03 08:26:51 +0100 |
| commit | ef15a8182ba0b6b7b5d6abb9d00eeb93f7cf0247 (patch) | |
| tree | f20bc84e992f652ea03aabd550ed580b7702ae40 /compiler/rustc_const_eval/src/interpret | |
| parent | 0919ad18381f6f4fcaddc809e786553e028bbde0 (diff) | |
| download | rust-ef15a8182ba0b6b7b5d6abb9d00eeb93f7cf0247.tar.gz rust-ef15a8182ba0b6b7b5d6abb9d00eeb93f7cf0247.zip | |
codegen, miri: fix computing the offset of an unsized field in a packed struct
Diffstat (limited to 'compiler/rustc_const_eval/src/interpret')
| -rw-r--r-- | compiler/rustc_const_eval/src/interpret/projection.rs | 12 |
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. |
