Skip to content

Commit 6daa37d

Browse files
jacobly0mlugg
authored andcommitted
x86_64: fix packed struct equality
Closes #22990
1 parent 9edfccb commit 6daa37d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/arch/x86_64/CodeGen.zig

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77309,11 +77309,27 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
7730977309
},
7731077310
}
7731177311
},
77312-
.int => res[0] = ops[0].cmpInts(cmp_op, &ops[1], cg) catch |err| break :err err,
77312+
.int => {
77313+
switch (ty.zigTypeTag(zcu)) {
77314+
else => {},
77315+
.@"struct", .@"union" => {
77316+
assert(ty.containerLayout(zcu) == .@"packed");
77317+
for (&ops) |*op| op.wrapInt(cg) catch |err| switch (err) {
77318+
error.SelectFailed => return cg.fail("failed to select {s} wrap {} {}", .{
77319+
@tagName(air_tag),
77320+
ty.fmt(pt),
77321+
op.tracking(cg),
77322+
}),
77323+
else => |e| return e,
77324+
};
77325+
},
77326+
}
77327+
res[0] = ops[0].cmpInts(cmp_op, &ops[1], cg) catch |err| break :err err;
77328+
},
7731377329
}) catch |err| switch (err) {
7731477330
error.SelectFailed => return cg.fail("failed to select {s} {} {} {}", .{
7731577331
@tagName(air_tag),
77316-
cg.typeOf(bin_op.lhs).fmt(pt),
77332+
ty.fmt(pt),
7731777333
ops[0].tracking(cg),
7731877334
ops[1].tracking(cg),
7731977335
}),

test/behavior/packed-struct.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,17 @@ test "packed struct equality" {
13071307
comptime try S.doTest(x, y);
13081308
}
13091309

1310+
test "packed struct equality ignores padding bits" {
1311+
if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
1312+
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1313+
1314+
const S = packed struct { b: bool };
1315+
var s: S = undefined;
1316+
s.b = true;
1317+
try std.testing.expect(s != S{ .b = false });
1318+
try std.testing.expect(s == S{ .b = true });
1319+
}
1320+
13101321
test "packed struct with signed field" {
13111322
var s: packed struct {
13121323
a: i2,

0 commit comments

Comments
 (0)