Skip to content

Commit 2715f6f

Browse files
committedDec 6, 2017
allow implicit cast from union to its enum tag type
closes #642
1 parent 960914a commit 2715f6f

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
 

‎src/ir.cpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7468,6 +7468,17 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
74687468
}
74697469
}
74707470

7471+
// implicit union to its enum tag type
7472+
if (expected_type->id == TypeTableEntryIdEnum && actual_type->id == TypeTableEntryIdUnion &&
7473+
(actual_type->data.unionation.decl_node->data.container_decl.auto_enum ||
7474+
actual_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
7475+
{
7476+
type_ensure_zero_bits_known(ira->codegen, actual_type);
7477+
if (actual_type->data.unionation.tag_type == expected_type) {
7478+
return ImplicitCastMatchResultYes;
7479+
}
7480+
}
7481+
74717482
// implicit enum to union which has the enum as the tag type
74727483
if (expected_type->id == TypeTableEntryIdUnion && actual_type->id == TypeTableEntryIdEnum &&
74737484
(expected_type->data.unionation.decl_node->data.container_decl.auto_enum ||

‎test/cases/union.zig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,11 @@ test "cast tag type of union to union" {
197197
}
198198
const Letter2 = enum { A, B, C };
199199
const Value2 = union(Letter2) { A: i32, B, C, };
200+
201+
test "implicit cast union to its tag type" {
202+
var x: Value2 = Letter2.B;
203+
giveMeLetterB(x);
204+
}
205+
fn giveMeLetterB(x: Letter2) {
206+
assert(x == Value2.B);
207+
}

0 commit comments

Comments
 (0)