Commit a040ccb42f

Veikka Tuominen <git@vexu.eu>
2022-06-04 11:20:28
Sema: fix coerce result ptr outside of functions
1 parent 33826a6
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -1875,7 +1875,7 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
 
     const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
     const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value);
-    try sema.storePtr(&trash_block, src, dummy_ptr, dummy_operand);
+    try sema.storePtr2(&trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast);
 
     {
         const air_tags = sema.air_instructions.items(.tag);
@@ -20187,6 +20187,13 @@ fn storePtr2(
 
     // TODO handle if the element type requires comptime
 
+    if (air_tag == .bitcast) {
+        // `air_tag == .bitcast` is used as a special case for `zirCoerceResultPtr`
+        // to avoid calling `requireRuntimeBlock` for the dummy block.
+        _ = try block.addBinOp(.store, ptr, operand);
+        return;
+    }
+
     try sema.requireRuntimeBlock(block, runtime_src);
     try sema.queueFullTypeResolution(elem_ty);
     _ = try block.addBinOp(air_tag, ptr, operand);
test/behavior/basic.zig
@@ -1,5 +1,6 @@
 const std = @import("std");
 const builtin = @import("builtin");
+const assert = std.debug.assert;
 const mem = std.mem;
 const expect = std.testing.expect;
 const expectEqualStrings = std.testing.expectEqualStrings;
@@ -1053,3 +1054,11 @@ test "const alloc with comptime known initializer is made comptime known" {
         if (u.a == 0) @compileError("bad");
     }
 }
+
+comptime {
+    // coerce result ptr outside a function
+    const S = struct { a: comptime_int };
+    var s: S = undefined;
+    s = S{ .a = 1 };
+    assert(s.a == 1);
+}