Skip to content

Commit 1a2468a

Browse files
committed
Revert "Sema: preserve extern struct field alignment"
This reverts commit 4620972.
1 parent 77dcd90 commit 1a2468a

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

lib/std/net.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ pub const Ip6Address = extern struct {
316316
.addr = undefined,
317317
},
318318
};
319-
var ip_slice: *[16]u8 = result.sa.addr[0..];
319+
var ip_slice = result.sa.addr[0..];
320320

321321
var tail: [16]u8 = undefined;
322322

@@ -431,7 +431,7 @@ pub const Ip6Address = extern struct {
431431
.addr = undefined,
432432
},
433433
};
434-
var ip_slice: *[16]u8 = result.sa.addr[0..];
434+
var ip_slice = result.sa.addr[0..];
435435

436436
var tail: [16]u8 = undefined;
437437

src/Sema.zig

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25926,13 +25926,9 @@ fn structFieldPtrByIndex(
2592625926
ptr_ty_data.packed_offset = .{ .host_size = 0, .bit_offset = 0 };
2592725927
}
2592825928
}
25929-
} else if (struct_obj.layout == .Extern) {
25930-
// For extern structs, field aligment might be bigger than type's natural alignment. Eg, in
25931-
// `extern struct { x: u32, y: u16 }` the second field is aligned as u32.
25932-
const field_offset = struct_ty.structFieldOffset(field_index, mod);
25933-
ptr_ty_data.flags.alignment = Alignment.fromByteUnits(
25934-
if (parent_align == 0) 0 else std.math.gcd(field_offset, parent_align),
25935-
);
25929+
} else if (struct_obj.layout == .Extern and field_index == 0) {
25930+
// This is the first field in memory, so can inherit the struct alignment
25931+
ptr_ty_data.flags.alignment = Alignment.fromByteUnits(parent_align);
2593625932
} else {
2593725933
// Our alignment is capped at the field alignment
2593825934
const field_align = try sema.structFieldAlignment(field, struct_obj.layout);

test/behavior/struct.zig

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,9 +1680,9 @@ test "extern struct field pointer has correct alignment" {
16801680

16811681
const S = struct {
16821682
fn doTheTest() !void {
1683-
var a: extern struct { x: u32, y: u16 } = .{ .x = 1, .y = 2 };
1684-
var b: extern struct { x: u32, y: u16 } align(1) = .{ .x = 3, .y = 4 };
1685-
var c: extern struct { x: u32, y: u16 } align(64) = .{ .x = 5, .y = 6 };
1683+
var a: extern struct { x: u32, y: u32 } = .{ .x = 1, .y = 2 };
1684+
var b: extern struct { x: u32, y: u32 } align(1) = .{ .x = 3, .y = 4 };
1685+
var c: extern struct { x: u32, y: u32 } align(64) = .{ .x = 5, .y = 6 };
16861686

16871687
const axp = &a.x;
16881688
const bxp = &b.x;
@@ -1693,19 +1693,18 @@ test "extern struct field pointer has correct alignment" {
16931693

16941694
comptime assert(@TypeOf(axp) == *u32);
16951695
comptime assert(@TypeOf(bxp) == *align(1) u32);
1696-
comptime assert(@TypeOf(cxp) == *align(64) u32);
1697-
1698-
comptime assert(@TypeOf(ayp) == *align(@alignOf(u32)) u16);
1699-
comptime assert(@TypeOf(byp) == *align(1) u16);
1700-
comptime assert(@TypeOf(cyp) == *align(@alignOf(u32)) u16);
1696+
comptime assert(@TypeOf(cxp) == *align(64) u32); // first field, inherits larger alignment
1697+
comptime assert(@TypeOf(ayp) == *u32);
1698+
comptime assert(@TypeOf(byp) == *align(1) u32);
1699+
comptime assert(@TypeOf(cyp) == *u32);
17011700

17021701
try expectEqual(@as(u32, 1), axp.*);
17031702
try expectEqual(@as(u32, 3), bxp.*);
17041703
try expectEqual(@as(u32, 5), cxp.*);
17051704

1706-
try expectEqual(@as(u16, 2), ayp.*);
1707-
try expectEqual(@as(u16, 4), byp.*);
1708-
try expectEqual(@as(u16, 6), cyp.*);
1705+
try expectEqual(@as(u32, 2), ayp.*);
1706+
try expectEqual(@as(u32, 4), byp.*);
1707+
try expectEqual(@as(u32, 6), cyp.*);
17091708
}
17101709
};
17111710

0 commit comments

Comments
 (0)