Commit 71937f75d8

Veikka Tuominen <git@vexu.eu>
2022-11-26 17:04:07
Sema: correctly detect union target in `zirSwitchBlock`
Closes #13655
1 parent fe38898
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -9698,10 +9698,12 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
     };
 
     const maybe_union_ty = blk: {
+        const zir_tags = sema.code.instructions.items(.tag);
         const zir_data = sema.code.instructions.items(.data);
         const cond_index = Zir.refToIndex(extra.data.operand).?;
         const raw_operand = sema.resolveInst(zir_data[cond_index].un_node.operand) catch unreachable;
-        break :blk sema.typeOf(raw_operand);
+        const target_ty = sema.typeOf(raw_operand);
+        break :blk if (zir_tags[cond_index] == .switch_cond_ref) target_ty.elemType() else target_ty;
     };
     const union_originally = maybe_union_ty.zigTypeTag() == .Union;
 
test/behavior/union.zig
@@ -1287,7 +1287,14 @@ test "noreturn field in union" {
             try expect(a == .a);
         },
     }
-    try expect(count == 5);
+    switch (a) {
+        .a => count += 1,
+        .b, .c => |*val| {
+            _ = val;
+            @compileError("bad");
+        },
+    }
+    try expect(count == 6);
 }
 
 test "union and enum field order doesn't match" {