authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-09 22:34:34-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-09 22:34:34-05:00
log5ea79bfc4a0a1269930d98faee36a8f6cb0b0401
treee69f01abb631fb64fa95556a70d300f41d5b3521
parent04ee3b01a159a25894f93d8448fb766ae545ab53
signature Commit is signed but in an unrecognized format.

fix not checking type of return pointer

Thanks to Vexu for the test cases. Closes #3422 Closes #3646 Closes #3224 Closes #3327 Closes #3269

2 files changed, 36 insertions(+), 0 deletions(-)

src/ir.cpp+12
......@@ -19591,6 +19591,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1959119591 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1959219592 return result_loc;
1959319593 }
19594 IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type);
19595 dummy_value->value->special = ConstValSpecialRuntime;
19596 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
19597 dummy_value, result_loc->value->type->data.pointer.child_type);
19598 if (type_is_invalid(dummy_result->value->type))
19599 return ira->codegen->invalid_inst_gen;
1959419600 ZigType *res_child_type = result_loc->value->type->data.pointer.child_type;
1959519601 if (res_child_type == ira->codegen->builtin_types.entry_var) {
1959619602 res_child_type = impl_fn_type_id->return_type;
......@@ -19723,6 +19729,12 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1972319729 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
1972419730 return result_loc;
1972519731 }
19732 IrInstGen *dummy_value = ir_const(ira, source_instr, return_type);
19733 dummy_value->value->special = ConstValSpecialRuntime;
19734 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
19735 dummy_value, result_loc->value->type->data.pointer.child_type);
19736 if (type_is_invalid(dummy_result->value->type))
19737 return ira->codegen->invalid_inst_gen;
1972619738 ZigType *res_child_type = result_loc->value->type->data.pointer.child_type;
1972719739 if (res_child_type == ira->codegen->builtin_types.entry_var) {
1972819740 res_child_type = return_type;
test/compile_errors.zig+24
......@@ -3,6 +3,30 @@ const builtin = @import("builtin");
33const Target = @import("std").Target;
44
55pub fn addCases(cases: *tests.CompileErrorContext) void {
6 cases.add("function call assigned to incorrect type",
7 \\export fn entry() void {
8 \\ var arr: [4]f32 = undefined;
9 \\ arr = concat();
10 \\}
11 \\fn concat() [16]f32 {
12 \\ return [1]f32{0}**16;
13 \\}
14 , &[_][]const u8{
15 "tmp.zig:3:17: error: expected type '[4]f32', found '[16]f32'",
16 });
17
18 cases.add("generic function call assigned to incorrect type",
19 \\pub export fn entry() void {
20 \\ var res: []i32 = undefined;
21 \\ res = myAlloc(i32);
22 \\}
23 \\fn myAlloc(comptime arg: type) anyerror!arg{
24 \\ unreachable;
25 \\}
26 , &[_][]const u8{
27 "tmp.zig:3:18: error: expected type '[]i32', found 'anyerror!i32",
28 });
29
630 cases.addTest("dependency loop in top-level decl with @TypeInfo",
731 \\export const foo = @typeInfo(@This());
832 , &[_][]const u8{