authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-11 00:27:41-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-06-11 00:27:41-04:00
logc1eb6c30e84b0b161b634f7410088f44c80caa90
tree915141c29947e320175cd2c5ed73afaa88ddf259
parent9b05474d797ea4600dbae36ae95d9eb042040bb2
parent6bf529dc381cb2f5a83c5f9e303ffdab7779ac4d
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11835 from ziglang/stage2-behavior

stage2: fix handling of aggregates with mixed comptime-only fields

6 files changed, 153 insertions(+), 119 deletions(-)

src/Sema.zig+4-6
......@@ -11747,11 +11747,11 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
1174711747
1174811748fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
1174911749 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
11750 const src = inst_data.src();
1175011751 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
11751 const unresolved_operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
11752 const operand_ty = try sema.resolveTypeFields(block, operand_src, unresolved_operand_ty);
11752 const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand);
1175311753 const target = sema.mod.getTarget();
11754 const bit_size = operand_ty.bitSize(target);
11754 const bit_size = try operand_ty.bitSizeAdvanced(target, sema.kit(block, src));
1175511755 return sema.addIntUnsigned(Type.comptime_int, bit_size);
1175611756}
1175711757
......@@ -25047,9 +25047,7 @@ pub fn typeRequiresComptime(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Typ
2504725047}
2504825048
2504925049pub fn typeHasRuntimeBits(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool {
25050 if ((try sema.typeHasOnePossibleValue(block, src, ty)) != null) return false;
25051 if (try sema.typeRequiresComptime(block, src, ty)) return false;
25052 return true;
25050 return ty.hasRuntimeBitsAdvanced(false, sema.kit(block, src));
2505325051}
2505425052
2505525053fn typeAbiSize(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !u64 {
src/codegen/llvm.zig+3-3
......@@ -9185,7 +9185,7 @@ fn isByRef(ty: Type) bool {
91859185 .AnyFrame,
91869186 => return false,
91879187
9188 .Array, .Frame => return ty.hasRuntimeBitsIgnoreComptime(),
9188 .Array, .Frame => return ty.hasRuntimeBits(),
91899189 .Struct => {
91909190 // Packed structs are represented to LLVM as integers.
91919191 if (ty.containerLayout() == .Packed) return false;
......@@ -9204,7 +9204,7 @@ fn isByRef(ty: Type) bool {
92049204 var count: usize = 0;
92059205 const fields = ty.structFields();
92069206 for (fields.values()) |field| {
9207 if (field.is_comptime or !field.ty.hasRuntimeBitsIgnoreComptime()) continue;
9207 if (field.is_comptime or !field.ty.hasRuntimeBits()) continue;
92089208
92099209 count += 1;
92109210 if (count > max_fields_byval) return true;
......@@ -9212,7 +9212,7 @@ fn isByRef(ty: Type) bool {
92129212 }
92139213 return false;
92149214 },
9215 .Union => return ty.hasRuntimeBitsIgnoreComptime(),
9215 .Union => return ty.hasRuntimeBits(),
92169216 .ErrorUnion => return isByRef(ty.errorUnionPayload()),
92179217 .Optional => {
92189218 var buf: Type.Payload.ElemType = undefined;
src/link/Dwarf.zig+28-26
......@@ -352,6 +352,7 @@ pub const DeclState = struct {
352352 const fields = ty.structFields();
353353 for (fields.keys()) |field_name, field_index| {
354354 const field = fields.get(field_name).?;
355 if (!field.ty.hasRuntimeBits()) continue;
355356 // DW.AT.member
356357 try dbg_info_buffer.ensureUnusedCapacity(field_name.len + 2);
357358 dbg_info_buffer.appendAssumeCapacity(@enumToInt(AbbrevKind.struct_member));
......@@ -1037,6 +1038,7 @@ pub fn commitDeclState(
10371038 }
10381039 }
10391040
1041 log.debug("updateDeclDebugInfoAllocation for '{s}'", .{decl.name});
10401042 try self.updateDeclDebugInfoAllocation(file, atom, @intCast(u32, dbg_info_buffer.items.len));
10411043
10421044 while (decl_state.abbrev_relocs.popOrNull()) |reloc| {
......@@ -1098,6 +1100,7 @@ pub fn commitDeclState(
10981100 }
10991101 }
11001102
1103 log.debug("writeDeclDebugInfo for '{s}", .{decl.name});
11011104 try self.writeDeclDebugInfo(file, atom, dbg_info_buffer.items);
11021105}
11031106
......@@ -1141,7 +1144,10 @@ fn updateDeclDebugInfoAllocation(self: *Dwarf, file: *File, atom: *Atom, len: u3
11411144 },
11421145 .wasm => {
11431146 const wasm_file = file.cast(File.Wasm).?;
1144 writeDbgInfoNopsBuffered(wasm_file.debug_info.items, atom.off, 0, &.{0}, atom.len, false);
1147 const segment_index = try wasm_file.getDebugInfoIndex();
1148 const segment = &wasm_file.segments.items[segment_index];
1149 const offset = segment.offset + atom.off;
1150 try writeDbgInfoNopsToArrayList(gpa, &wasm_file.debug_info, offset, 0, &.{0}, atom.len, false);
11451151 },
11461152 else => unreachable,
11471153 }
......@@ -1283,8 +1289,12 @@ fn writeDeclDebugInfo(self: *Dwarf, file: *File, atom: *Atom, dbg_info_buf: []co
12831289 debug_info.items.len = needed_size;
12841290 }
12851291 const offset = segment.offset + atom.off;
1286 writeDbgInfoNopsBuffered(
1287 debug_info.items,
1292 log.debug(" writeDbgInfoNopsToArrayList debug_info_len={d} offset={d} content_len={d} next_padding_size={d}", .{
1293 debug_info.items.len, offset, dbg_info_buf.len, next_padding_size,
1294 });
1295 try writeDbgInfoNopsToArrayList(
1296 gpa,
1297 debug_info,
12881298 offset,
12891299 prev_padding_size,
12901300 dbg_info_buf,
......@@ -1678,7 +1688,7 @@ pub fn writeDbgInfoHeader(self: *Dwarf, file: *File, module: *Module, low_pc: u6
16781688 },
16791689 .wasm => {
16801690 const wasm_file = file.cast(File.Wasm).?;
1681 writeDbgInfoNopsBuffered(wasm_file.debug_info.items, 0, 0, di_buf.items, jmp_amt, false);
1691 try writeDbgInfoNopsToArrayList(self.allocator, &wasm_file.debug_info, 0, 0, di_buf.items, jmp_amt, false);
16821692 },
16831693 else => unreachable,
16841694 }
......@@ -1884,35 +1894,25 @@ fn pwriteDbgInfoNops(
18841894 try file.pwritevAll(vecs[0..vec_index], offset - prev_padding_size);
18851895}
18861896
1887fn writeDbgInfoNopsBuffered(
1888 buf: []u8,
1897fn writeDbgInfoNopsToArrayList(
1898 gpa: Allocator,
1899 buffer: *std.ArrayListUnmanaged(u8),
18891900 offset: u32,
18901901 prev_padding_size: usize,
18911902 content: []const u8,
18921903 next_padding_size: usize,
18931904 trailing_zero: bool,
1894) void {
1895 assert(buf.len >= content.len + prev_padding_size + next_padding_size + @boolToInt(trailing_zero));
1896 const tracy = trace(@src());
1897 defer tracy.end();
1898
1899 {
1900 var padding_left = prev_padding_size;
1901 while (padding_left > 0) : (padding_left -= 1) {
1902 buf[offset - padding_left] = @enumToInt(AbbrevKind.pad1);
1903 }
1904 }
1905
1906 mem.copy(u8, buf[offset..], content);
1907 {
1908 var padding_left = next_padding_size;
1909 while (padding_left > 0) : (padding_left -= 1) {
1910 buf[offset + content.len + padding_left] = @enumToInt(AbbrevKind.pad1);
1911 }
1912 }
1905) Allocator.Error!void {
1906 try buffer.resize(gpa, @maximum(
1907 buffer.items.len,
1908 offset + content.len + next_padding_size + 1,
1909 ));
1910 mem.set(u8, buffer.items[offset - prev_padding_size .. offset], @enumToInt(AbbrevKind.pad1));
1911 mem.copy(u8, buffer.items[offset..], content);
1912 mem.set(u8, buffer.items[offset + content.len ..][0..next_padding_size], @enumToInt(AbbrevKind.pad1));
19131913
19141914 if (trailing_zero) {
1915 buf[offset + content.len + next_padding_size] = 0;
1915 buffer.items[offset + content.len + next_padding_size] = 0;
19161916 }
19171917}
19181918
......@@ -2249,7 +2249,9 @@ pub fn flushModule(self: *Dwarf, file: *File, module: *Module) !void {
22492249 try addDbgInfoErrorSet(arena, module, error_ty, self.target, &dbg_info_buffer);
22502250
22512251 try self.managed_atoms.append(gpa, atom);
2252 log.debug("updateDeclDebugInfoAllocation in flushModule", .{});
22522253 try self.updateDeclDebugInfoAllocation(file, atom, @intCast(u32, dbg_info_buffer.items.len));
2254 log.debug("writeDeclDebugInfo in flushModule", .{});
22532255 try self.writeDeclDebugInfo(file, atom, dbg_info_buffer.items);
22542256
22552257 const file_pos = blk: {
src/type.zig+53-60
......@@ -2365,6 +2365,7 @@ pub const Type = extern union {
23652365 .@"anyframe",
23662366 .anyopaque,
23672367 .@"opaque",
2368 .type_info,
23682369 => return true,
23692370
23702371 // These are false because they are comptime-only types.
......@@ -2379,7 +2380,6 @@ pub const Type = extern union {
23792380 .enum_literal,
23802381 .empty_struct,
23812382 .empty_struct_literal,
2382 .type_info,
23832383 .bound_fn,
23842384 // These are function *bodies*, not pointers.
23852385 // Special exceptions have to be made when emitting functions due to
......@@ -2464,14 +2464,6 @@ pub const Type = extern union {
24642464
24652465 .@"struct" => {
24662466 const struct_obj = ty.castTag(.@"struct").?.data;
2467 if (sema_kit) |sk| {
2468 _ = try sk.sema.typeRequiresComptime(sk.block, sk.src, ty);
2469 }
2470 switch (struct_obj.requires_comptime) {
2471 .yes => return false,
2472 .wip, .no => if (struct_obj.known_non_opv) return true,
2473 .unknown => {},
2474 }
24752467 if (struct_obj.status == .field_types_wip) {
24762468 // In this case, we guess that hasRuntimeBits() for this type is true,
24772469 // and then later if our guess was incorrect, we emit a compile error.
......@@ -3550,9 +3542,19 @@ pub const Type = extern union {
35503542 );
35513543 }
35523544
3553 /// Asserts the type has the bit size already resolved.
35543545 pub fn bitSize(ty: Type, target: Target) u64 {
3555 return switch (ty.tag()) {
3546 return bitSizeAdvanced(ty, target, null) catch unreachable;
3547 }
3548
3549 /// If you pass `sema_kit`, any recursive type resolutions will happen if
3550 /// necessary, possibly returning a CompileError. Passing `null` instead asserts
3551 /// the type is fully resolved, and there will be no error, guaranteed.
3552 pub fn bitSizeAdvanced(
3553 ty: Type,
3554 target: Target,
3555 sema_kit: ?Module.WipAnalysis,
3556 ) Module.CompileError!u64 {
3557 switch (ty.tag()) {
35563558 .fn_noreturn_no_args => unreachable, // represents machine code; not a pointer
35573559 .fn_void_no_args => unreachable, // represents machine code; not a pointer
35583560 .fn_naked_noreturn_no_args => unreachable, // represents machine code; not a pointer
......@@ -3576,40 +3578,30 @@ pub const Type = extern union {
35763578 .generic_poison => unreachable,
35773579 .bound_fn => unreachable,
35783580
3579 .void => 0,
3580 .bool, .u1 => 1,
3581 .u8, .i8 => 8,
3582 .i16, .u16, .f16 => 16,
3583 .u29 => 29,
3584 .i32, .u32, .f32 => 32,
3585 .i64, .u64, .f64 => 64,
3586 .f80 => 80,
3587 .u128, .i128, .f128 => 128,
3581 .void => return 0,
3582 .bool, .u1 => return 1,
3583 .u8, .i8 => return 8,
3584 .i16, .u16, .f16 => return 16,
3585 .u29 => return 29,
3586 .i32, .u32, .f32 => return 32,
3587 .i64, .u64, .f64 => return 64,
3588 .f80 => return 80,
3589 .u128, .i128, .f128 => return 128,
35883590
35893591 .@"struct" => {
3590 const field_count = ty.structFieldCount();
3591 if (field_count == 0) return 0;
3592
3593 const struct_obj = ty.castTag(.@"struct").?.data;
3594 assert(struct_obj.haveFieldTypes());
3595
3596 switch (struct_obj.layout) {
3597 .Auto, .Extern => {
3598 var total: u64 = 0;
3599 for (struct_obj.fields.values()) |field| {
3600 total += field.ty.bitSize(target);
3601 }
3602 return total;
3603 },
3604 .Packed => return struct_obj.packedIntegerBits(target),
3592 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
3593 var total: u64 = 0;
3594 for (ty.structFields().values()) |field| {
3595 total += try bitSizeAdvanced(field.ty, target, sema_kit);
36053596 }
3597 return total;
36063598 },
36073599
36083600 .tuple, .anon_struct => {
3609 const tuple = ty.tupleFields();
3601 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
36103602 var total: u64 = 0;
3611 for (tuple.types) |field_ty| {
3612 total += field_ty.bitSize(target);
3603 for (ty.tupleFields().types) |field_ty| {
3604 total += try bitSizeAdvanced(field_ty, target, sema_kit);
36133605 }
36143606 return total;
36153607 },
......@@ -3617,37 +3609,35 @@ pub const Type = extern union {
36173609 .enum_simple, .enum_full, .enum_nonexhaustive, .enum_numbered => {
36183610 var buffer: Payload.Bits = undefined;
36193611 const int_tag_ty = ty.intTagType(&buffer);
3620 return int_tag_ty.bitSize(target);
3612 return try bitSizeAdvanced(int_tag_ty, target, sema_kit);
36213613 },
36223614
36233615 .@"union", .union_tagged => {
3616 if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
36243617 const union_obj = ty.cast(Payload.Union).?.data;
3625
3626 const fields = union_obj.fields;
3627 if (fields.count() == 0) return 0;
3628
36293618 assert(union_obj.haveFieldTypes());
36303619
36313620 var size: u64 = 0;
3632 for (fields.values()) |field| {
3633 size = @maximum(size, field.ty.bitSize(target));
3621 for (union_obj.fields.values()) |field| {
3622 size = @maximum(size, try bitSizeAdvanced(field.ty, target, sema_kit));
36343623 }
36353624 return size;
36363625 },
36373626
36383627 .vector => {
36393628 const payload = ty.castTag(.vector).?.data;
3640 const elem_bit_size = payload.elem_type.bitSize(target);
3629 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
36413630 return elem_bit_size * payload.len;
36423631 },
3643 .array_u8 => 8 * ty.castTag(.array_u8).?.data,
3644 .array_u8_sentinel_0 => 8 * (ty.castTag(.array_u8_sentinel_0).?.data + 1),
3632 .array_u8 => return 8 * ty.castTag(.array_u8).?.data,
3633 .array_u8_sentinel_0 => return 8 * (ty.castTag(.array_u8_sentinel_0).?.data + 1),
36453634 .array => {
36463635 const payload = ty.castTag(.array).?.data;
36473636 const elem_size = std.math.max(payload.elem_type.abiAlignment(target), payload.elem_type.abiSize(target));
36483637 if (elem_size == 0 or payload.len == 0)
3649 return 0;
3650 return (payload.len - 1) * 8 * elem_size + payload.elem_type.bitSize(target);
3638 return @as(u64, 0);
3639 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
3640 return (payload.len - 1) * 8 * elem_size + elem_bit_size;
36513641 },
36523642 .array_sentinel => {
36533643 const payload = ty.castTag(.array_sentinel).?.data;
......@@ -3655,14 +3645,15 @@ pub const Type = extern union {
36553645 payload.elem_type.abiAlignment(target),
36563646 payload.elem_type.abiSize(target),
36573647 );
3658 return payload.len * 8 * elem_size + payload.elem_type.bitSize(target);
3648 const elem_bit_size = try bitSizeAdvanced(payload.elem_type, target, sema_kit);
3649 return payload.len * 8 * elem_size + elem_bit_size;
36593650 },
36603651
36613652 .isize,
36623653 .usize,
36633654 .@"anyframe",
36643655 .anyframe_T,
3665 => target.cpu.arch.ptrBitWidth(),
3656 => return target.cpu.arch.ptrBitWidth(),
36663657
36673658 .const_slice,
36683659 .mut_slice,
......@@ -3670,7 +3661,7 @@ pub const Type = extern union {
36703661
36713662 .const_slice_u8,
36723663 .const_slice_u8_sentinel_0,
3673 => target.cpu.arch.ptrBitWidth() * 2,
3664 => return target.cpu.arch.ptrBitWidth() * 2,
36743665
36753666 .optional_single_const_pointer,
36763667 .optional_single_mut_pointer,
......@@ -3689,8 +3680,8 @@ pub const Type = extern union {
36893680 },
36903681
36913682 .pointer => switch (ty.castTag(.pointer).?.data.size) {
3692 .Slice => target.cpu.arch.ptrBitWidth() * 2,
3693 else => target.cpu.arch.ptrBitWidth(),
3683 .Slice => return target.cpu.arch.ptrBitWidth() * 2,
3684 else => return target.cpu.arch.ptrBitWidth(),
36943685 },
36953686
36963687 .manyptr_u8,
......@@ -3716,7 +3707,7 @@ pub const Type = extern union {
37163707 .error_set_merged,
37173708 => return 16, // TODO revisit this when we have the concept of the error tag type
37183709
3719 .int_signed, .int_unsigned => ty.cast(Payload.Bits).?.data,
3710 .int_signed, .int_unsigned => return ty.cast(Payload.Bits).?.data,
37203711
37213712 .optional => {
37223713 var buf: Payload.ElemType = undefined;
......@@ -3730,7 +3721,8 @@ pub const Type = extern union {
37303721 // field and a boolean as the second. Since the child type's abi alignment is
37313722 // guaranteed to be >= that of bool's (1 byte) the added size is exactly equal
37323723 // to the child type's ABI alignment.
3733 return child_type.bitSize(target) + 1;
3724 const child_bit_size = try bitSizeAdvanced(child_type, target, sema_kit);
3725 return child_bit_size + 1;
37343726 },
37353727
37363728 .error_union => {
......@@ -3738,9 +3730,9 @@ pub const Type = extern union {
37383730 if (!payload.error_set.hasRuntimeBits() and !payload.payload.hasRuntimeBits()) {
37393731 return 0;
37403732 } else if (!payload.error_set.hasRuntimeBits()) {
3741 return payload.payload.bitSize(target);
3733 return payload.payload.bitSizeAdvanced(target, sema_kit);
37423734 } else if (!payload.payload.hasRuntimeBits()) {
3743 return payload.error_set.bitSize(target);
3735 return payload.error_set.bitSizeAdvanced(target, sema_kit);
37443736 }
37453737 @panic("TODO bitSize error union");
37463738 },
......@@ -3757,7 +3749,7 @@ pub const Type = extern union {
37573749 .extern_options,
37583750 .type_info,
37593751 => @panic("TODO at some point we gotta resolve builtin types"),
3760 };
3752 }
37613753 }
37623754
37633755 pub fn isSinglePointer(self: Type) bool {
......@@ -5514,6 +5506,7 @@ pub const Type = extern union {
55145506 switch (ty.tag()) {
55155507 .@"struct" => {
55165508 const struct_obj = ty.castTag(.@"struct").?.data;
5509 assert(struct_obj.haveFieldTypes());
55175510 return struct_obj.fields.count();
55185511 },
55195512 .empty_struct, .empty_struct_literal => return 0,
test/behavior/eval.zig+32-1
......@@ -1196,7 +1196,9 @@ test "equality of pointers to comptime const" {
11961196}
11971197
11981198test "storing an array of type in a field" {
1199 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1199 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1200 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1201 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
12001202
12011203 const S = struct {
12021204 fn doTheTest() void {
......@@ -1221,3 +1223,32 @@ test "storing an array of type in a field" {
12211223
12221224 S.doTheTest();
12231225}
1226
1227test "pass pointer to field of comptime-only type as a runtime parameter" {
1228 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
1229 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
1230 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1231
1232 const S = struct {
1233 const Mixed = struct {
1234 T: type,
1235 x: i32,
1236 };
1237 const bag: Mixed = .{
1238 .T = bool,
1239 .x = 1234,
1240 };
1241
1242 var ok = false;
1243
1244 fn doTheTest() !void {
1245 foo(&bag.x);
1246 try expect(ok);
1247 }
1248
1249 fn foo(ptr: *const i32) void {
1250 ok = ptr.* == 1234;
1251 }
1252 };
1253 try S.doTheTest();
1254}
test/behavior/packed-struct.zig+33-23
......@@ -33,10 +33,10 @@ test "correct size of packed structs" {
3333}
3434
3535test "flags in packed structs" {
36 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
36 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
3737
3838 const Flags1 = packed struct {
39 // byte 0
39 // first 8 bits
4040 b0_0: u1,
4141 b0_1: u1,
4242 b0_2: u1,
......@@ -46,7 +46,7 @@ test "flags in packed structs" {
4646 b0_6: u1,
4747 b0_7: u1,
4848
49 // partial byte 1 (but not 8 bits)
49 // 7 more bits
5050 b1_0: u1,
5151 b1_1: u1,
5252 b1_2: u1,
......@@ -55,12 +55,12 @@ test "flags in packed structs" {
5555 b1_5: u1,
5656 b1_6: u1,
5757
58 // some padding to fill to size 3
58 // some padding to fill to 24 bits
5959 _: u9,
6060 };
6161
62 try expectEqual(3, @sizeOf(Flags1));
63 try expectEqual(3 * 8, @bitSizeOf(Flags1));
62 try expectEqual(@sizeOf(u24), @sizeOf(Flags1));
63 try expectEqual(24, @bitSizeOf(Flags1));
6464
6565 const Flags2 = packed struct {
6666 // byte 0
......@@ -86,8 +86,8 @@ test "flags in packed structs" {
8686 _: u10,
8787 };
8888
89 try expectEqual(4, @sizeOf(Flags2));
90 try expectEqual(8 + 7 + 10, @bitSizeOf(Flags2));
89 try expectEqual(@sizeOf(u25), @sizeOf(Flags2));
90 try expectEqual(25, @bitSizeOf(Flags2));
9191
9292 const Flags3 = packed struct {
9393 // byte 0
......@@ -114,30 +114,30 @@ test "flags in packed structs" {
114114 _: u16, // it works, if the padding is 8-based
115115 };
116116
117 try expectEqual(4, @sizeOf(Flags3));
118 try expectEqual(4 * 8, @bitSizeOf(Flags3));
117 try expectEqual(@sizeOf(u32), @sizeOf(Flags3));
118 try expectEqual(32, @bitSizeOf(Flags3));
119119}
120120
121121test "arrays in packed structs" {
122 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
122 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
123123
124124 const T1 = packed struct { array: [3][3]u8 };
125125 const T2 = packed struct { array: [9]u8 };
126126
127 try expectEqual(9, @sizeOf(T1));
128 try expectEqual(9 * 8, @bitSizeOf(T1));
129 try expectEqual(9, @sizeOf(T2));
130 try expectEqual(9 * 8, @bitSizeOf(T2));
127 try expectEqual(@sizeOf(u72), @sizeOf(T1));
128 try expectEqual(72, @bitSizeOf(T1));
129 try expectEqual(@sizeOf(u72), @sizeOf(T2));
130 try expectEqual(72, @bitSizeOf(T2));
131131}
132132
133133test "consistent size of packed structs" {
134 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
134 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
135135
136136 const TxData1 = packed struct { data: u8, _23: u23, full: bool = false };
137137 const TxData2 = packed struct { data: u9, _22: u22, full: bool = false };
138138
139139 const register_size_bits = 32;
140 const register_size_bytes = register_size_bits / 8;
140 const register_size_bytes = @sizeOf(u32);
141141
142142 try expectEqual(register_size_bits, @bitSizeOf(TxData1));
143143 try expectEqual(register_size_bytes, @sizeOf(TxData1));
......@@ -151,7 +151,7 @@ test "consistent size of packed structs" {
151151 const TxData6 = packed struct { a: u24, b: u32 };
152152
153153 const expectedBitSize = 56;
154 const expectedByteSize = expectedBitSize / 8;
154 const expectedByteSize = @sizeOf(u56);
155155
156156 try expectEqual(expectedBitSize, @bitSizeOf(TxData3));
157157 try expectEqual(expectedByteSize, @sizeOf(TxData3));
......@@ -167,7 +167,12 @@ test "consistent size of packed structs" {
167167}
168168
169169test "correct sizeOf and offsets in packed structs" {
170 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
170 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
171 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
172 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
173 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
174 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
175 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
171176
172177 const PStruct = packed struct {
173178 bool_a: bool,
......@@ -234,11 +239,16 @@ test "correct sizeOf and offsets in packed structs" {
234239
235240 try expectEqual(16, @offsetOf(S, "b"));
236241 try expectEqual(128, @bitOffsetOf(S, "b"));
237 try expectEqual(20, @sizeOf(S));
242 try expectEqual(@sizeOf(u160), @sizeOf(S));
238243}
239244
240245test "nested packed structs" {
241 if (builtin.zig_backend != .stage1) return error.SkipZigTest;
246 if (builtin.zig_backend == .stage1) return error.SkipZigTest;
247 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
248 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
249 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
250 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
251 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
242252
243253 const S1 = packed struct { a: u8, b: u8, c: u8 };
244254
......@@ -248,7 +258,7 @@ test "nested packed structs" {
248258 const S3Padded = packed struct { s3: S3, pad: u16 };
249259
250260 try expectEqual(48, @bitSizeOf(S3));
251 try expectEqual(6, @sizeOf(S3));
261 try expectEqual(@sizeOf(u48), @sizeOf(S3));
252262
253263 try expectEqual(3, @offsetOf(S3, "y"));
254264 try expectEqual(24, @bitOffsetOf(S3, "y"));
......@@ -268,7 +278,7 @@ test "nested packed structs" {
268278 const S6 = packed struct { a: i32, b: S4, c: i8 };
269279
270280 const expectedBitSize = 80;
271 const expectedByteSize = expectedBitSize / 8;
281 const expectedByteSize = @sizeOf(u80);
272282 try expectEqual(expectedBitSize, @bitSizeOf(S5));
273283 try expectEqual(expectedByteSize, @sizeOf(S5));
274284 try expectEqual(expectedBitSize, @bitSizeOf(S6));