diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-03-24 00:55:55 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-03-24 00:55:55 -0400 |
| commit | a736dfe6a1c7a43c9cb9474255ec34261e5f80ab (patch) | |
| tree | a85e8b82f6bfdcb51be6ea4c314a5336f25cc675 /src/ir.cpp | |
| parent | d0551db5cd29e4c7f361ef40a37486138a4e8b1e (diff) | |
| download | zig-a736dfe6a1c7a43c9cb9474255ec34261e5f80ab.tar.gz zig-a736dfe6a1c7a43c9cb9474255ec34261e5f80ab.zip | |
implement implicit cast from enum literal to enum
See #683
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 21c15a7f70..c8514bf8e8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11579,6 +11579,22 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst return ir_analyze_number_to_literal(ira, source_instr, value, wanted_type); } + // cast from enum literal to enum with matching field name + if (actual_type->id == ZigTypeIdEnumLiteral && wanted_type->id == ZigTypeIdEnum) { + if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusZeroBitsKnown))) + return ira->codegen->invalid_instruction; + + TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal); + if (field == nullptr) { + ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'", + buf_ptr(&wanted_type->name), buf_ptr(value->value.data.x_enum_literal))); + return ira->codegen->invalid_instruction; + } + IrInstruction *result = ir_const(ira, source_instr, wanted_type); + bigint_init_bigint(&result->value.data.x_enum_tag, &field->value); + return result; + } + // cast from union to the enum type of the union if (actual_type->id == ZigTypeIdUnion && wanted_type->id == ZigTypeIdEnum) { if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) |
