Commit 379d547603

Vexu <15308111+Vexu@users.noreply.github.com>
2019-11-20 06:54:47
add missing cast to generic function call result
1 parent 0e405c5
Changed files (2)
src/ir.cpp
@@ -17520,6 +17520,11 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
                 if (!handle_is_ptr(result_loc->value->type->data.pointer.child_type)) {
                     ir_reset_result(call_instruction->result_loc);
                     result_loc = nullptr;
+                } else {
+                    call_instruction->base.value.type = impl_fn_type_id->return_type;
+                    IrInstruction *casted_value = ir_implicit_cast(ira, &call_instruction->base, result_loc->value.type->data.pointer.child_type);
+                    if (type_is_invalid(casted_value->value.type))
+                        return casted_value;
                 }
             }
         } else if (call_instruction->is_async_call_builtin) {
test/compile_errors.zig
@@ -109,6 +109,19 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         "tmp.zig:3:17: error: expected type '[4]f32', found '[16]f32'"
     );
 
+    cases.add(
+        "generic function call assigned to incorrect type",
+        \\pub export fn entry() void {
+        \\    var res: []i32 = undefined;
+        \\    res = myAlloc(i32);
+        \\}
+        \\fn myAlloc(comptime arg: type) anyerror!arg{
+        \\    unreachable;
+        \\}
+    ,
+        "tmp.zig:3:18: error: expected type '[]i32', found 'anyerror!i32"
+    );
+
     cases.add(
         "asigning to struct or union fields that are not optionals with a function that returns an optional",
         \\fn maybe(is: bool) ?u8 {