authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-06-04 12:20:28+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-06-06 13:11:50-07:00
loga040ccb42f1b34ba612c975b7030ccdcbb3f8086
treeb25a7f6f1bdf32e234e928914f14bfd36c5de97b
parent33826a6a2e035d2a2be65314ed80a6b7abaf7f12

Sema: fix coerce result ptr outside of functions


2 files changed, 17 insertions(+), 1 deletions(-)

src/Sema.zig+8-1
......@@ -1875,7 +1875,7 @@ fn zirCoerceResultPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE
18751875
18761876 const dummy_ptr = try trash_block.addTy(.alloc, sema.typeOf(ptr));
18771877 const dummy_operand = try trash_block.addBitCast(pointee_ty, .void_value);
1878 try sema.storePtr(&trash_block, src, dummy_ptr, dummy_operand);
1878 try sema.storePtr2(&trash_block, src, dummy_ptr, src, dummy_operand, src, .bitcast);
18791879
18801880 {
18811881 const air_tags = sema.air_instructions.items(.tag);
......@@ -20187,6 +20187,13 @@ fn storePtr2(
2018720187
2018820188 // TODO handle if the element type requires comptime
2018920189
20190 if (air_tag == .bitcast) {
20191 // `air_tag == .bitcast` is used as a special case for `zirCoerceResultPtr`
20192 // to avoid calling `requireRuntimeBlock` for the dummy block.
20193 _ = try block.addBinOp(.store, ptr, operand);
20194 return;
20195 }
20196
2019020197 try sema.requireRuntimeBlock(block, runtime_src);
2019120198 try sema.queueFullTypeResolution(elem_ty);
2019220199 _ = try block.addBinOp(air_tag, ptr, operand);
test/behavior/basic.zig+9
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const builtin = @import("builtin");
3const assert = std.debug.assert;
34const mem = std.mem;
45const expect = std.testing.expect;
56const expectEqualStrings = std.testing.expectEqualStrings;
......@@ -1053,3 +1054,11 @@ test "const alloc with comptime known initializer is made comptime known" {
10531054 if (u.a == 0) @compileError("bad");
10541055 }
10551056}
1057
1058comptime {
1059 // coerce result ptr outside a function
1060 const S = struct { a: comptime_int };
1061 var s: S = undefined;
1062 s = S{ .a = 1 };
1063 assert(s.a == 1);
1064}