authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-26 18:04:07+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-26 18:05:27+02:00
log71937f75d83d960e0ec61eb697e4e3420685f9ca
tree41b7b3eb8c30a6f2531d0e889b593f802f5982a2
parentfe388982462db067bdb697ec3e89c2c13ce2ffa8

Sema: correctly detect union target in `zirSwitchBlock`

Closes #13655

2 files changed, 11 insertions(+), 2 deletions(-)

src/Sema.zig+3-1
......@@ -9698,10 +9698,12 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
96989698 };
96999699
97009700 const maybe_union_ty = blk: {
9701 const zir_tags = sema.code.instructions.items(.tag);
97019702 const zir_data = sema.code.instructions.items(.data);
97029703 const cond_index = Zir.refToIndex(extra.data.operand).?;
97039704 const raw_operand = sema.resolveInst(zir_data[cond_index].un_node.operand) catch unreachable;
9704 break :blk sema.typeOf(raw_operand);
9705 const target_ty = sema.typeOf(raw_operand);
9706 break :blk if (zir_tags[cond_index] == .switch_cond_ref) target_ty.elemType() else target_ty;
97059707 };
97069708 const union_originally = maybe_union_ty.zigTypeTag() == .Union;
97079709
test/behavior/union.zig+8-1
......@@ -1287,7 +1287,14 @@ test "noreturn field in union" {
12871287 try expect(a == .a);
12881288 },
12891289 }
1290 try expect(count == 5);
1290 switch (a) {
1291 .a => count += 1,
1292 .b, .c => |*val| {
1293 _ = val;
1294 @compileError("bad");
1295 },
1296 }
1297 try expect(count == 6);
12911298}
12921299
12931300test "union and enum field order doesn't match" {