authorgravatar for kkhaike@gmail.comkkHAIKE <kkhaike@gmail.com> 2022-09-17 20:16:40+08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-09-21 20:21:02+03:00
log4961044ce8d06f43bf6f43c103433d70a1fbd79a
tree8a80cdc1f4bd24380b511aaf425da697ca910933
parent183127733c8ab51c05f4d4b5cee2a1497272172a

AstGen: store void to ptr result loc when there is no else branch


2 files changed, 28 insertions(+), 3 deletions(-)

src/AstGen.zig+5-1
......@@ -5570,7 +5570,11 @@ fn ifExpr(
55705570 };
55715571 } else .{
55725572 .src = if_full.ast.then_expr,
5573 .result = .none,
5573 .result = switch (rl) {
5574 // Explicitly store void to ptr result loc if there is no else branch
5575 .ptr, .block_ptr => try rvalue(&else_scope, rl, .void_value, node),
5576 else => .none,
5577 },
55745578 };
55755579
55765580 const break_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .break_inline else .@"break";
test/cases/compile_errors/missing_else_clause.zig+23-2
......@@ -6,11 +6,32 @@ fn g(b: bool) void {
66 const y = if (b) h: { break :h @as(i32, 1); };
77 _ = y;
88}
9export fn entry() void { f(true); g(true); }
10
9fn h() void {
10 // https://github.com/ziglang/zig/issues/12743
11 const T = struct { oh_no: *u32 };
12 var x: T = if (false) {};
13 _ = x;
14}
15fn k(b: bool) void {
16 // block_ptr case
17 const T = struct { oh_no: u32 };
18 var x = if (b) blk: {
19 break :blk if (false) T{ .oh_no = 2 };
20 } else T{ .oh_no = 1 };
21 _ = x;
22}
23export fn entry() void {
24 f(true);
25 g(true);
26 h();
27 k(true);
28}
1129// error
1230// backend=stage2
1331// target=native
1432//
1533// :2:21: error: incompatible types: 'i32' and 'void'
1634// :6:15: error: incompatible types: 'i32' and 'void'
35// :12:16: error: expected type 'tmp.h.T', found 'void'
36// :11:15: note: struct declared here
37// :18:9: error: incompatible types: 'void' and 'tmp.k.T'