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(
60216021 comptime format: []const u8,
60226022 args: anytype,
60236023) error{OutOfMemory}!void {
6024 if (src_loc.lazy == .unneeded) {
6025 assert(parent.src_loc.lazy == .unneeded);
6026 return;
6027 }
60246028 const msg = try std.fmt.allocPrint(mod.gpa, format, args);
60256029 errdefer mod.gpa.free(msg);
60266030
src/Sema.zig+3-3
......@@ -882,7 +882,7 @@ fn analyzeBodyInner(
882882
883883 var dbg_block_begins: u32 = 0;
884884
885 // We use a while(true) loop here to avoid a redundant way of breaking out of
885 // We use a while (true) loop here to avoid a redundant way of breaking out of
886886 // the loop. The only way to break out of the loop is with a `noreturn`
887887 // instruction.
888888 var i: usize = 0;
......@@ -18073,7 +18073,7 @@ fn zirStructInitAnon(
1807318073 return sema.addConstantMaybeRef(block, tuple_ty, tuple_val, is_ref);
1807418074 };
1807518075
18076 sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {
18076 sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
1807718077 error.NeededSourceLocation => {
1807818078 const decl = sema.mod.declPtr(block.src_decl);
1807918079 const field_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index);
......@@ -18179,7 +18179,7 @@ fn zirArrayInit(
1817918179 return sema.addConstantMaybeRef(block, array_ty, array_val, is_ref);
1818018180 };
1818118181
18182 sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) {
18182 sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
1818318183 error.NeededSourceLocation => {
1818418184 const decl = sema.mod.declPtr(block.src_decl);
1818518185 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