authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 16:30:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-16 16:30:24-04:00
logbf7b6fbbdb7d28c0d7dba3e17c46ce156712cfc8
tree50ad2616948cf255982fb4138ee8757ff92b5c80
parentcbca6586e72a8adefb3d8923d0c0f4590f54bfd8
signaturelock-open Commit is signed but in an unrecognized format.

add missing compile error for fn call bad implicit cast

when the function's return type handle is a pointer but the result location's result value type handle is not a pointer closes #3055

2 files changed, 55 insertions(+), 7 deletions(-)

src/ir.cpp+21-7
...@@ -9615,6 +9615,10 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -9615,6 +9615,10 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
9615 return cur_type;9615 return cur_type;
9616 }9616 }
96179617
9618 if (prev_type == cur_type) {
9619 continue;
9620 }
9621
9618 if (prev_type->id == ZigTypeIdUnreachable) {9622 if (prev_type->id == ZigTypeIdUnreachable) {
9619 prev_inst = cur_inst;9623 prev_inst = cur_inst;
9620 continue;9624 continue;
...@@ -14921,7 +14925,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc...@@ -14921,7 +14925,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
14921 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);14925 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);
14922 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,14926 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
14923 frame_type, nullptr, true, true, false);14927 frame_type, nullptr, true, true, false);
14924 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {14928 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
14925 return result_loc;14929 return result_loc;
14926 }14930 }
14927 result_loc = ir_implicit_cast(ira, result_loc, get_pointer_to_type(ira->codegen, frame_type, false));14931 result_loc = ir_implicit_cast(ira, result_loc, get_pointer_to_type(ira->codegen, frame_type, false));
...@@ -15638,10 +15642,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c...@@ -15638,10 +15642,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
15638 if (handle_is_ptr(impl_fn_type_id->return_type)) {15642 if (handle_is_ptr(impl_fn_type_id->return_type)) {
15639 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,15643 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
15640 impl_fn_type_id->return_type, nullptr, true, true, false);15644 impl_fn_type_id->return_type, nullptr, true, true, false);
15641 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) ||15645 if (result_loc != nullptr) {
15642 instr_is_unreachable(result_loc)))15646 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
15643 {15647 return result_loc;
15644 return result_loc;15648 }
15649 if (!handle_is_ptr(result_loc->value.type->data.pointer.child_type)) {
15650 ir_reset_result(call_instruction->result_loc);
15651 result_loc = nullptr;
15652 }
15645 }15653 }
15646 } else {15654 } else {
15647 result_loc = nullptr;15655 result_loc = nullptr;
...@@ -15791,8 +15799,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c...@@ -15791,8 +15799,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
15791 if (handle_is_ptr(return_type)) {15799 if (handle_is_ptr(return_type)) {
15792 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,15800 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
15793 return_type, nullptr, true, true, false);15801 return_type, nullptr, true, true, false);
15794 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {15802 if (result_loc != nullptr) {
15795 return result_loc;15803 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
15804 return result_loc;
15805 }
15806 if (!handle_is_ptr(result_loc->value.type->data.pointer.child_type)) {
15807 ir_reset_result(call_instruction->result_loc);
15808 result_loc = nullptr;
15809 }
15796 }15810 }
15797 } else {15811 } else {
15798 result_loc = nullptr;15812 result_loc = nullptr;
test/compile_errors.zig+34
...@@ -2,6 +2,40 @@ const tests = @import("tests.zig");...@@ -2,6 +2,40 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "result location incompatibility mismatching handle_is_ptr (generic call)",
7 \\export fn entry() void {
8 \\ var damn = Container{
9 \\ .not_optional = getOptional(i32),
10 \\ };
11 \\}
12 \\pub fn getOptional(comptime T: type) ?T {
13 \\ return 0;
14 \\}
15 \\pub const Container = struct {
16 \\ not_optional: i32,
17 \\};
18 ,
19 "tmp.zig:3:36: error: expected type 'i32', found '?i32'",
20 );
21
22 cases.add(
23 "result location incompatibility mismatching handle_is_ptr",
24 \\export fn entry() void {
25 \\ var damn = Container{
26 \\ .not_optional = getOptional(),
27 \\ };
28 \\}
29 \\pub fn getOptional() ?i32 {
30 \\ return 0;
31 \\}
32 \\pub const Container = struct {
33 \\ not_optional: i32,
34 \\};
35 ,
36 "tmp.zig:3:36: error: expected type 'i32', found '?i32'",
37 );
38
5 cases.add(39 cases.add(
6 "const frame cast to anyframe",40 "const frame cast to anyframe",
7 \\export fn a() void {41 \\export fn a() void {