aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2022-10-24 22:19:24 +0300
committerVeikka Tuominen <git@vexu.eu>2022-10-27 01:31:18 +0300
commitd9fe5ba7f805c82f14c162945ac851ebb570ec89 (patch)
treefb353da646ba9fc9c25e74b60076f7f2b08fc638
parent9dcfc829e650bc9c0a89e9f7778744c774120c09 (diff)
downloadzig-d9fe5ba7f805c82f14c162945ac851ebb570ec89.tar.gz
zig-d9fe5ba7f805c82f14c162945ac851ebb570ec89.zip
Sema: add error for too big packed struct
-rw-r--r--src/Sema.zig27
-rw-r--r--test/cases/compile_errors/too_big_packed_struct.zig13
2 files changed, 40 insertions, 0 deletions
diff --git a/src/Sema.zig b/src/Sema.zig
index 9c52fd91a3..d1a558d15b 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -29075,6 +29075,33 @@ fn semaBackingIntType(mod: *Module, struct_obj: *Module.Struct) CompileError!voi
struct_obj.backing_int_ty = try backing_int_ty.copy(decl_arena_allocator);
try wip_captures.finalize();
} else {
+ if (fields_bit_sum > std.math.maxInt(u16)) {
+ var sema: Sema = .{
+ .mod = mod,
+ .gpa = gpa,
+ .arena = undefined,
+ .perm_arena = decl_arena_allocator,
+ .code = zir,
+ .owner_decl = decl,
+ .owner_decl_index = decl_index,
+ .func = null,
+ .fn_ret_ty = Type.void,
+ .owner_func = null,
+ };
+ defer sema.deinit();
+
+ var block: Block = .{
+ .parent = null,
+ .sema = &sema,
+ .src_decl = decl_index,
+ .namespace = &struct_obj.namespace,
+ .wip_capture_scope = undefined,
+ .instructions = .{},
+ .inlining = null,
+ .is_comptime = true,
+ };
+ return sema.fail(&block, LazySrcLoc.nodeOffset(0), "size of packed struct '{d}' exceeds maximum bit width of 65535", .{fields_bit_sum});
+ }
var buf: Type.Payload.Bits = .{
.base = .{ .tag = .int_unsigned },
.data = @intCast(u16, fields_bit_sum),
diff --git a/test/cases/compile_errors/too_big_packed_struct.zig b/test/cases/compile_errors/too_big_packed_struct.zig
new file mode 100644
index 0000000000..bedc4a72a6
--- /dev/null
+++ b/test/cases/compile_errors/too_big_packed_struct.zig
@@ -0,0 +1,13 @@
+pub export fn entry() void {
+ const T = packed struct {
+ a: u65535,
+ b: u65535,
+ };
+ @compileLog(@sizeOf(T));
+}
+
+// error
+// backend=stage2
+// target=native
+//
+// :2:22: error: size of packed struct '131070' exceeds maximum bit width of 65535