allow implicit cast from union to its enum tag type

closes #642
master
Andrew Kelley 2017-12-05 21:10:47 -05:00
parent 960914a073
commit 2715f6fdb8
2 changed files with 19 additions and 0 deletions

View File

@ -7468,6 +7468,17 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
} }
} }
// implicit union to its enum tag type
if (expected_type->id == TypeTableEntryIdEnum && actual_type->id == TypeTableEntryIdUnion &&
(actual_type->data.unionation.decl_node->data.container_decl.auto_enum ||
actual_type->data.unionation.decl_node->data.container_decl.init_arg_expr != nullptr))
{
type_ensure_zero_bits_known(ira->codegen, actual_type);
if (actual_type->data.unionation.tag_type == expected_type) {
return ImplicitCastMatchResultYes;
}
}
// implicit enum to union which has the enum as the tag type // implicit enum to union which has the enum as the tag type
if (expected_type->id == TypeTableEntryIdUnion && actual_type->id == TypeTableEntryIdEnum && if (expected_type->id == TypeTableEntryIdUnion && actual_type->id == TypeTableEntryIdEnum &&
(expected_type->data.unionation.decl_node->data.container_decl.auto_enum || (expected_type->data.unionation.decl_node->data.container_decl.auto_enum ||

View File

@ -197,3 +197,11 @@ test "cast tag type of union to union" {
} }
const Letter2 = enum { A, B, C }; const Letter2 = enum { A, B, C };
const Value2 = union(Letter2) { A: i32, B, C, }; const Value2 = union(Letter2) { A: i32, B, C, };
test "implicit cast union to its tag type" {
var x: Value2 = Letter2.B;
giveMeLetterB(x);
}
fn giveMeLetterB(x: Letter2) {
assert(x == Value2.B);
}