authorgravatar for yujiri@disroot.orgyujiri8 <yujiri@disroot.org> 2023-05-31 07:15:52-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-31 11:15:52+00:00
logcd1417dbdf098634641e87dba4c3be2806d76250
tree86f37e2015cb84fd08ec260772b7bb6b40686811
parent32e719e070067333efa9d4888927b425d118fc97
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

don't crash when can't evaluate comptime expression with inferred type

Closes #15911.

3 files changed, 24 insertions(+), 3 deletions(-)

src/Module.zig+4
...@@ -6021,6 +6021,10 @@ pub fn errNoteNonLazy(...@@ -6021,6 +6021,10 @@ pub fn errNoteNonLazy(
6021 comptime format: []const u8,6021 comptime format: []const u8,
6022 args: anytype,6022 args: anytype,
6023) error{OutOfMemory}!void {6023) error{OutOfMemory}!void {
6024 if (src_loc.lazy == .unneeded) {
6025 assert(parent.src_loc.lazy == .unneeded);
6026 return;
6027 }
6024 const msg = try std.fmt.allocPrint(mod.gpa, format, args);6028 const msg = try std.fmt.allocPrint(mod.gpa, format, args);
6025 errdefer mod.gpa.free(msg);6029 errdefer mod.gpa.free(msg);
60266030
src/Sema.zig+3-3
...@@ -882,7 +882,7 @@ fn analyzeBodyInner(...@@ -882,7 +882,7 @@ fn analyzeBodyInner(
882882
883 var dbg_block_begins: u32 = 0;883 var dbg_block_begins: u32 = 0;
884884
885 // We use a while(true) loop here to avoid a redundant way of breaking out of885 // We use a while (true) loop here to avoid a redundant way of breaking out of
886 // the loop. The only way to break out of the loop is with a `noreturn`886 // the loop. The only way to break out of the loop is with a `noreturn`
887 // instruction.887 // instruction.
888 var i: usize = 0;888 var i: usize = 0;
...@@ -18073,7 +18073,7 @@ fn zirStructInitAnon(...@@ -18073,7 +18073,7 @@ fn zirStructInitAnon(
18073 return sema.addConstantMaybeRef(block, tuple_ty, tuple_val, is_ref);18073 return sema.addConstantMaybeRef(block, tuple_ty, tuple_val, is_ref);
18074 };18074 };
1807518075
18076 sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {18076 sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
18077 error.NeededSourceLocation => {18077 error.NeededSourceLocation => {
18078 const decl = sema.mod.declPtr(block.src_decl);18078 const decl = sema.mod.declPtr(block.src_decl);
18079 const field_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);18079 const field_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);
...@@ -18179,7 +18179,7 @@ fn zirArrayInit(...@@ -18179,7 +18179,7 @@ fn zirArrayInit(
18179 return sema.addConstantMaybeRef(block, array_ty, array_val, is_ref);18179 return sema.addConstantMaybeRef(block, array_ty, array_val, is_ref);
18180 };18180 };
1818118181
18182 sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {18182 sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
18183 error.NeededSourceLocation => {18183 error.NeededSourceLocation => {
18184 const decl = sema.mod.declPtr(block.src_decl);18184 const decl = sema.mod.declPtr(block.src_decl);
18185 const elem_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);18185 const elem_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);
test/cases/compile_errors/unable_to_evaluate_comptime_expr_with_inferred_type.zig created+17
...@@ -0,0 +1,17 @@
1const A = struct {
2 a: u8,
3};
4
5var n: u8 = 5;
6var a: A = .{ .a = n };
7
8pub export fn entry() void {
9 _ = a;
10}
11
12// error
13// backend=stage2
14// target=native
15//
16// :6:13: error: unable to evaluate comptime expression
17// :6:16: note: operation is runtime due to this operand