aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-11-04 18:47:52 +0200
committerVeikka Tuominen <git@vexu.eu>2022-11-05 12:54:38 +0200
commitf92e7bed7b6b83b2cdfe2bfed047e3a7bcdd2116 (patch)
tree1e88680f7a1ff2b488f41a7ce89439183456acb2
parent51b1083d66b29d110c8cf60b59052170dd34a95f (diff)
downloadzig-f92e7bed7b6b83b2cdfe2bfed047e3a7bcdd2116.tar.gz
zig-f92e7bed7b6b83b2cdfe2bfed047e3a7bcdd2116.zip
stage2: bitsize of packed struct should trigger backing int ty check
Closes #13398
-rw-r--r--src/type.zig12
-rw-r--r--test/cases/compile_errors/bitsize_of_packed_struct_checks_backing_int_ty.zig13
2 files changed, 18 insertions, 7 deletions
diff --git a/src/type.zig b/src/type.zig
index 605e4396c0..3dc9202917 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -3574,15 +3574,13 @@ pub const Type = extern union {
.u128, .i128, .f128 => return 128,
.@"struct" => {
- if (sema_kit) |sk| _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty);
- if (ty.containerLayout() != .Packed) {
+ const struct_obj = ty.castTag(.@"struct").?.data;
+ if (struct_obj.layout != .Packed) {
return (try ty.abiSizeAdvanced(target, if (sema_kit) |sk| .{ .sema_kit = sk } else .eager)).scalar * 8;
}
- var total: u64 = 0;
- for (ty.structFields().values()) |field| {
- total += try bitSizeAdvanced(field.ty, target, sema_kit);
- }
- return total;
+ if (sema_kit) |sk| _ = try sk.sema.resolveTypeLayout(sk.block, sk.src, ty);
+ assert(struct_obj.haveLayout());
+ return try struct_obj.backing_int_ty.bitSizeAdvanced(target, sema_kit);
},
.tuple, .anon_struct => {
diff --git a/test/cases/compile_errors/bitsize_of_packed_struct_checks_backing_int_ty.zig b/test/cases/compile_errors/bitsize_of_packed_struct_checks_backing_int_ty.zig
new file mode 100644
index 0000000000..774a9c3376
--- /dev/null
+++ b/test/cases/compile_errors/bitsize_of_packed_struct_checks_backing_int_ty.zig
@@ -0,0 +1,13 @@
+const Foo = packed struct(u32) {
+ x: u1,
+};
+fn bar(_: Foo) callconv(.C) void {}
+pub export fn entry() void {
+ bar(.{ .x = 0 });
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :1:27: error: backing integer type 'u32' has bit size 32 but the struct fields have a total bit size of 1