From 3ac8bc2dab8ccdbd777fa1a240c3d4ece6360c4f Mon Sep 17 00:00:00 2001 From: David Rubin Date: Sun, 15 Dec 2024 01:48:56 -0800 Subject: [PATCH] disallow stores to element types with no comptime size --- src/Sema.zig | 8 ++++++++ .../compile_errors/store_no_comptime_size.zig | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 test/cases/compile_errors/store_no_comptime_size.zig diff --git a/src/Sema.zig b/src/Sema.zig index f31e71047fa3..ccd124085291 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -31958,6 +31958,14 @@ fn storePtr2( }); } + // Don't allow stores to element types that have no comptime size. + switch (elem_ty.zigTypeTag(zcu)) { + .@"fn", + .@"opaque", + => return sema.fail(block, ptr_src, "pointer element type must have a comptime-known size", .{}), + else => std.debug.print("{} {}", .{ elem_ty.fmt(pt), elem_ty.zigTypeTag(zcu) }), + } + const store_inst = if (is_ret) try block.addBinOp(.store, ptr, operand) else diff --git a/test/cases/compile_errors/store_no_comptime_size.zig b/test/cases/compile_errors/store_no_comptime_size.zig new file mode 100644 index 000000000000..73b4bc51da08 --- /dev/null +++ b/test/cases/compile_errors/store_no_comptime_size.zig @@ -0,0 +1,15 @@ +export fn a() void { + const x: *fn () void = @ptrFromInt(4); + x.* = undefined; +} + +export fn b(x: *anyopaque) void { + x.* = undefined; +} + +// error +// backend=stage2 +// target=native +// +// :3:6: error: pointer element type must have a comptime-known size +// :7:6: error: pointer element type must have a comptime-known size