authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-05 21:10:47-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-05 21:10:47-05:00
log2715f6fdb8bca1c88c58dac047889711359e193b
tree52fcb1d48f7a3198d7718e57a6df2c3bc742a157
parent960914a073c367883c9fdf54e900890a6aefc05f

allow implicit cast from union to its enum tag type

closes #642

2 files changed, 19 insertions(+), 0 deletions(-)

src/ir.cpp+11
...@@ -7468,6 +7468,17 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,...@@ -7468,6 +7468,17 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
7468 }7468 }
7469 }7469 }
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
7471 // implicit enum to union which has the enum as the tag type7482 // implicit enum to union which has the enum as the tag type
7472 if (expected_type->id == TypeTableEntryIdUnion && actual_type->id == TypeTableEntryIdEnum &&7483 if (expected_type->id == TypeTableEntryIdUnion && actual_type->id == TypeTableEntryIdEnum &&
7473 (expected_type->data.unionation.decl_node->data.container_decl.auto_enum ||7484 (expected_type->data.unionation.decl_node->data.container_decl.auto_enum ||
test/cases/union.zig+8
...@@ -197,3 +197,11 @@ test "cast tag type of union to union" {...@@ -197,3 +197,11 @@ test "cast tag type of union to union" {
197}197}
198const Letter2 = enum { A, B, C };198const Letter2 = enum { A, B, C };
199const Value2 = union(Letter2) { A: i32, B, C, };199const Value2 = union(Letter2) { A: i32, B, C, };
200
201test "implicit cast union to its tag type" {
202 var x: Value2 = Letter2.B;
203 giveMeLetterB(x);
204}
205fn giveMeLetterB(x: Letter2) {
206 assert(x == Value2.B);
207}