zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit cd1417dbdf098634641e87dba4c3be2806d76250 (tree)
parent 32e719e070067333efa9d4888927b425d118fc97
Author: yujiri8 <yujiri@disroot.org>
Date:   Wed, 31 May 2023 07:15:52 -0400

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

Closes #15911.
Diffstat:
Msrc/Module.zig | 4++++
Msrc/Sema.zig | 6+++---
Atest/cases/compile_errors/unable_to_evaluate_comptime_expr_with_inferred_type.zig | 17+++++++++++++++++
3 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/Module.zig b/src/Module.zig @@ -6021,6 +6021,10 @@ pub fn errNoteNonLazy( comptime format: []const u8, args: anytype, ) error{OutOfMemory}!void { + if (src_loc.lazy == .unneeded) { + assert(parent.src_loc.lazy == .unneeded); + return; + } const msg = try std.fmt.allocPrint(mod.gpa, format, args); errdefer mod.gpa.free(msg); diff --git a/src/Sema.zig b/src/Sema.zig @@ -882,7 +882,7 @@ fn analyzeBodyInner( var dbg_block_begins: u32 = 0; - // We use a while(true) loop here to avoid a redundant way of breaking out of + // We use a while (true) loop here to avoid a redundant way of breaking out of // the loop. The only way to break out of the loop is with a `noreturn` // instruction. var i: usize = 0; @@ -18073,7 +18073,7 @@ fn zirStructInitAnon( return sema.addConstantMaybeRef(block, tuple_ty, tuple_val, is_ref); }; - sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) { + sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) { error.NeededSourceLocation => { const decl = sema.mod.declPtr(block.src_decl); const field_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index); @@ -18179,7 +18179,7 @@ fn zirArrayInit( return sema.addConstantMaybeRef(block, array_ty, array_val, is_ref); }; - sema.requireRuntimeBlock(block, src, .unneeded) catch |err| switch (err) { + sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) { error.NeededSourceLocation => { const decl = sema.mod.declPtr(block.src_decl); const elem_src = Module.initSrc(src.node_offset.x, sema.gpa, decl, runtime_index); diff --git a/test/cases/compile_errors/unable_to_evaluate_comptime_expr_with_inferred_type.zig b/test/cases/compile_errors/unable_to_evaluate_comptime_expr_with_inferred_type.zig @@ -0,0 +1,17 @@ +const A = struct { + a: u8, +}; + +var n: u8 = 5; +var a: A = .{ .a = n }; + +pub export fn entry() void { + _ = a; +} + +// error +// backend=stage2 +// target=native +// +// :6:13: error: unable to evaluate comptime expression +// :6:16: note: operation is runtime due to this operand