Skip to content

Commit c3d638a

Browse files
committed
value: fix bitcasting packed structs with u0 fields
Closes #13942
1 parent 2aab84a commit c3d638a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/value.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,7 @@ pub const Value = extern union {
13781378
var enum_buffer: Payload.U64 = undefined;
13791379
const int_val = val.enumToInt(ty, &enum_buffer);
13801380

1381+
if (abi_size == 0) return;
13811382
if (abi_size <= @sizeOf(u64)) {
13821383
const int: u64 = switch (int_val.tag()) {
13831384
.zero => 0,
@@ -1571,6 +1572,7 @@ pub const Value = extern union {
15711572
const abi_size = @intCast(usize, ty.abiSize(target));
15721573

15731574
const bits = int_info.bits;
1575+
if (bits == 0) return Value.zero;
15741576
if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64
15751577
.signed => return Value.Tag.int_i64.create(arena, std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, .signed)),
15761578
.unsigned => return Value.Tag.int_u64.create(arena, std.mem.readVarPackedInt(u64, buffer, bit_offset, bits, endian, .unsigned)),

test/behavior/cast.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1505,3 +1505,16 @@ test "implicit cast from [:0]T to [*c]T" {
15051505
try expect(c.len == a.len);
15061506
try expect(c.ptr == a.ptr);
15071507
}
1508+
1509+
test "bitcast packed struct with u0" {
1510+
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1511+
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1512+
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1513+
1514+
const S = packed struct(u2) { a: u0, b: u2 };
1515+
const s = @bitCast(S, @as(u2, 2));
1516+
try expect(s.a == 0);
1517+
try expect(s.b == 2);
1518+
const i = @bitCast(u2, s);
1519+
try expect(i == 2);
1520+
}

0 commit comments

Comments
 (0)