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
signature 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
96159615 return cur_type;
96169616 }
96179617
9618 if (prev_type == cur_type) {
9619 continue;
9620 }
9621
96189622 if (prev_type->id == ZigTypeIdUnreachable) {
96199623 prev_inst = cur_inst;
96209624 continue;
......@@ -14921,7 +14925,7 @@ static IrInstruction *ir_analyze_async_call(IrAnalyze *ira, IrInstructionCallSrc
1492114925 ZigType *frame_type = get_fn_frame_type(ira->codegen, fn_entry);
1492214926 IrInstruction *result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
1492314927 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)) {
1492514929 return result_loc;
1492614930 }
1492714931 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
1563815642 if (handle_is_ptr(impl_fn_type_id->return_type)) {
1563915643 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
1564015644 impl_fn_type_id->return_type, nullptr, true, true, false);
15641 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) ||
15642 instr_is_unreachable(result_loc)))
15643 {
15644 return result_loc;
15645 if (result_loc != nullptr) {
15646 if (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc)) {
15647 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 }
1564515653 }
1564615654 } else {
1564715655 result_loc = nullptr;
......@@ -15791,8 +15799,14 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCallSrc *c
1579115799 if (handle_is_ptr(return_type)) {
1579215800 result_loc = ir_resolve_result(ira, &call_instruction->base, call_instruction->result_loc,
1579315801 return_type, nullptr, true, true, false);
15794 if (result_loc != nullptr && (type_is_invalid(result_loc->value.type) || instr_is_unreachable(result_loc))) {
15795 return result_loc;
15802 if (result_loc != nullptr) {
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 }
1579615810 }
1579715811 } else {
1579815812 result_loc = nullptr;
test/compile_errors.zig+34
......@@ -2,6 +2,40 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub 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
539 cases.add(
640 "const frame cast to anyframe",
741 \\export fn a() void {