diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-06-30 23:24:52 -0400 |
|---|---|---|
| committer | Jacob Young <jacobly0@users.noreply.github.com> | 2023-06-30 23:24:52 -0400 |
| commit | f571438fc0010c0c45f79a2f709161a324c5a164 (patch) | |
| tree | 894900ec24a7b37b437ee106564d9776d4217138 /src | |
| parent | 2282c278850e29af81e463d55c81fa08855823f8 (diff) | |
| download | zig-f571438fc0010c0c45f79a2f709161a324c5a164.tar.gz zig-f571438fc0010c0c45f79a2f709161a324c5a164.zip | |
Unrevert "Sema: preserve extern struct field alignment"
This unreverts commit 1a2468abfcd8b539193d943c1eefb71319cc7b88.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Sema.zig | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index c1ec7d1f01..0681de4ad5 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -25926,9 +25926,13 @@ fn structFieldPtrByIndex( ptr_ty_data.packed_offset = .{ .host_size = 0, .bit_offset = 0 }; } } - } else if (struct_obj.layout == .Extern and field_index == 0) { - // This is the first field in memory, so can inherit the struct alignment - ptr_ty_data.flags.alignment = Alignment.fromByteUnits(parent_align); + } else if (struct_obj.layout == .Extern) { + // For extern structs, field aligment might be bigger than type's natural alignment. Eg, in + // `extern struct { x: u32, y: u16 }` the second field is aligned as u32. + const field_offset = struct_ty.structFieldOffset(field_index, mod); + ptr_ty_data.flags.alignment = Alignment.fromByteUnits( + if (parent_align == 0) 0 else std.math.gcd(field_offset, parent_align), + ); } else { // Our alignment is capped at the field alignment const field_align = try sema.structFieldAlignment(field, struct_obj.layout); |
