Commit cd1417dbdf

yujiri8 <yujiri@disroot.org>
2023-05-31 13:15:52
don't crash when can't evaluate comptime expression with inferred type
Closes #15911.
1 parent 32e719e
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);
 
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);
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