From cde946110a213eee6d476792fb91ef7a627eaf0b Mon Sep 17 00:00:00 2001 From: Jackson Wambolt Date: Sat, 28 Jun 2025 19:33:38 -0500 Subject: [PATCH 1/2] add test cases --- .../runtime_value_in_comptime_array.zig | 16 ++++++++++++++++ .../runtime_value_in_comptime_struct.zig | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/cases/compile_errors/runtime_value_in_comptime_array.zig create mode 100644 test/cases/compile_errors/runtime_value_in_comptime_struct.zig diff --git a/test/cases/compile_errors/runtime_value_in_comptime_array.zig b/test/cases/compile_errors/runtime_value_in_comptime_array.zig new file mode 100644 index 000000000000..37455d5a04dc --- /dev/null +++ b/test/cases/compile_errors/runtime_value_in_comptime_array.zig @@ -0,0 +1,16 @@ +fn comptimeArray(comptime _: []const u8) void {} +fn bar() u8 { + return 123; +} + +pub fn main() void { + const y = bar(); + comptimeArray(&.{y}); +} + +// error +// +// :8:21: error: unable to evaluate comptime expression +// :8:22: note: operation is runtime due to this operand +// :8:19: note: argument to comptime parameter must be comptime-known +// :1:18: note: parameter declared comptime here diff --git a/test/cases/compile_errors/runtime_value_in_comptime_struct.zig b/test/cases/compile_errors/runtime_value_in_comptime_struct.zig new file mode 100644 index 000000000000..70f8edba81f0 --- /dev/null +++ b/test/cases/compile_errors/runtime_value_in_comptime_struct.zig @@ -0,0 +1,16 @@ +fn comptimeStruct(comptime _: anytype) void {} +fn bar() u8 { + return 123; +} + +pub fn main() void { + const y = bar(); + comptimeStruct(.{ .foo = y }); +} + +// error +// +// :8:21: error: unable to evaluate comptime expression +// :8:24: note: operation is runtime due to this operand +// :8:21: note: argument to comptime parameter must be comptime-known +// :1:19: note: parameter declared comptime here From 7549ad2456867401b92fd78596d62b36d1f3d167 Mon Sep 17 00:00:00 2001 From: Jackson Wambolt Date: Sat, 28 Jun 2025 19:56:38 -0500 Subject: [PATCH 2/2] pass `src` to `requireRuntimeBlock` Passing `.unneeded` for `src` asserts that `requireRuntimeBlock` will never have to use it, but using `src` is the entire point. --- src/Sema.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 20be9e705274..b4080b5280f1 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -20134,7 +20134,7 @@ fn structInitAnon( return sema.addConstantMaybeRef(struct_val, is_ref); }; - try sema.requireRuntimeBlock(block, LazySrcLoc.unneeded, block.src(.{ .init_elem = .{ + try sema.requireRuntimeBlock(block, src, block.src(.{ .init_elem = .{ .init_node_offset = src.offset.node_offset.x, .elem_index = @intCast(runtime_index), } })); @@ -20282,7 +20282,7 @@ fn zirArrayInit( return sema.addConstantMaybeRef(result_val.toIntern(), is_ref); }; - try sema.requireRuntimeBlock(block, LazySrcLoc.unneeded, block.src(.{ .init_elem = .{ + try sema.requireRuntimeBlock(block, src, block.src(.{ .init_elem = .{ .init_node_offset = src.offset.node_offset.x, .elem_index = runtime_index, } }));