@@ -2805,6 +2805,17 @@ static IrInstruction *ir_build_result_optional_payload(IrBuilder *irb, Scope *sc
2805
2805
return &instruction->base;
2806
2806
}
2807
2807
2808
+ static IrInstruction *ir_build_result_error_union_payload(IrBuilder *irb, Scope *scope, AstNode *source_node,
2809
+ IrInstruction *prev_result_loc)
2810
+ {
2811
+ IrInstructionResultErrorUnionPayload *instruction = ir_build_instruction<IrInstructionResultErrorUnionPayload>(irb, scope, source_node);
2812
+ instruction->prev_result_loc = prev_result_loc;
2813
+
2814
+ ir_ref_instruction(prev_result_loc, irb->current_basic_block);
2815
+
2816
+ return &instruction->base;
2817
+ }
2818
+
2808
2819
static IrInstruction *ir_build_result_return(IrBuilder *irb, Scope *scope, AstNode *source_node) {
2809
2820
IrInstructionResultReturn *instruction = ir_build_instruction<IrInstructionResultReturn>(irb, scope, source_node);
2810
2821
return &instruction->base;
@@ -9812,17 +9823,31 @@ static IrInstruction *ir_analyze_result_optional_payload(IrAnalyze *ira, IrInstr
9812
9823
ZigType *old_ptr_type = result_loc->value.type;
9813
9824
assert(old_ptr_type->id == ZigTypeIdPointer);
9814
9825
ZigType *new_ptr_type = get_pointer_to_type_extra(ira->codegen, needed_child_type,
9815
- old_ptr_type->data.pointer.is_const,
9816
- old_ptr_type->data.pointer.is_volatile,
9817
- old_ptr_type->data.pointer.ptr_len,
9818
- 0, 0, 0);
9826
+ false, old_ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0);
9819
9827
9820
9828
IrInstruction *result = ir_build_result_optional_payload(&ira->new_irb, result_loc->scope,
9821
9829
result_loc->source_node, result_loc);
9822
9830
result->value.type = new_ptr_type;
9823
9831
return result;
9824
9832
}
9825
9833
9834
+ static IrInstruction *ir_analyze_result_error_union_payload(IrAnalyze *ira, IrInstruction *result_loc,
9835
+ ZigType *needed_child_type)
9836
+ {
9837
+ if (instr_is_comptime(result_loc)) {
9838
+ zig_panic("TODO comptime ir_analyze_result_error_union_payload");
9839
+ }
9840
+ ZigType *old_ptr_type = result_loc->value.type;
9841
+ assert(old_ptr_type->id == ZigTypeIdPointer);
9842
+ ZigType *new_ptr_type = get_pointer_to_type_extra(ira->codegen, needed_child_type,
9843
+ false, old_ptr_type->data.pointer.is_volatile, PtrLenSingle, 0, 0, 0);
9844
+
9845
+ IrInstruction *result = ir_build_result_error_union_payload(&ira->new_irb, result_loc->scope,
9846
+ result_loc->source_node, result_loc);
9847
+ result->value.type = new_ptr_type;
9848
+ return result;
9849
+ }
9850
+
9826
9851
static IrInstruction *ir_analyze_optional_wrap(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value,
9827
9852
ZigType *wanted_type)
9828
9853
{
@@ -10787,7 +10812,7 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
10787
10812
return ir_analyze_null_to_maybe(ira, source_instr, value, wanted_type);
10788
10813
}
10789
10814
10790
- // cast from child type of error type to error type
10815
+ // cast from T to E!T
10791
10816
if (wanted_type->id == ZigTypeIdErrorUnion) {
10792
10817
if (types_match_const_cast_only(ira, wanted_type->data.error_union.payload_type, actual_type,
10793
10818
source_node, false).id == ConstCastResultIdOk)
@@ -11127,6 +11152,15 @@ static IrInstruction *ir_implicit_cast_result(IrAnalyze *ira, IrInstruction *res
11127
11152
}
11128
11153
}
11129
11154
11155
+ // cast from T to E!T
11156
+ if (have_child_type->id == ZigTypeIdErrorUnion) {
11157
+ if (types_match_const_cast_only(ira, have_child_type->data.error_union.payload_type, needed_child_type,
11158
+ source_node, false).id == ConstCastResultIdOk)
11159
+ {
11160
+ return ir_analyze_result_error_union_payload(ira, result_loc, needed_child_type);
11161
+ }
11162
+ }
11163
+
11130
11164
ErrorMsg *parent_msg = ir_add_error_node(ira, source_node,
11131
11165
buf_sprintf("expected type '%s', found '%s'",
11132
11166
buf_ptr(&have_child_type->name),
0 commit comments