| ... | @@ -11579,6 +11579,22 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst | ... | @@ -11579,6 +11579,22 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst |
| 11579 | return ir_analyze_number_to_literal(ira, source_instr, value, wanted_type); | 11579 | return ir_analyze_number_to_literal(ira, source_instr, value, wanted_type); |
| 11580 | } | 11580 | } |
| 11581 | | 11581 | |
| | 11582 | // cast from enum literal to enum with matching field name |
| | 11583 | if (actual_type->id == ZigTypeIdEnumLiteral && wanted_type->id == ZigTypeIdEnum) { |
| | 11584 | if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusZeroBitsKnown))) |
| | 11585 | return ira->codegen->invalid_instruction; |
| | 11586 | |
| | 11587 | TypeEnumField *field = find_enum_type_field(wanted_type, value->value.data.x_enum_literal); |
| | 11588 | if (field == nullptr) { |
| | 11589 | ir_add_error(ira, source_instr, buf_sprintf("enum '%s' has no field named '%s'", |
| | 11590 | buf_ptr(&wanted_type->name), buf_ptr(value->value.data.x_enum_literal))); |
| | 11591 | return ira->codegen->invalid_instruction; |
| | 11592 | } |
| | 11593 | IrInstruction *result = ir_const(ira, source_instr, wanted_type); |
| | 11594 | bigint_init_bigint(&result->value.data.x_enum_tag, &field->value); |
| | 11595 | return result; |
| | 11596 | } |
| | 11597 | |
| 11582 | // cast from union to the enum type of the union | 11598 | // cast from union to the enum type of the union |
| 11583 | if (actual_type->id == ZigTypeIdUnion && wanted_type->id == ZigTypeIdEnum) { | 11599 | if (actual_type->id == ZigTypeIdUnion && wanted_type->id == ZigTypeIdEnum) { |
| 11584 | if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) | 11600 | if ((err = type_resolve(ira->codegen, actual_type, ResolveStatusZeroBitsKnown))) |