Skip to content

Commit 71937f7

Browse files
committed
Sema: correctly detect union target in zirSwitchBlock
Closes #13655
1 parent fe38898 commit 71937f7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Sema.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

12931300
test "union and enum field order doesn't match" {

0 commit comments

Comments
 (0)