authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-05 15:32:25-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-12-05 15:32:25-05:00
log1b0f4d59763e273901717199e47764745aed2631
tree5288ec6ed5493cc216bca92c8c700ce3ca5650e8
parent518ff33e64f3970e6fc053d2cffe0751e21b0700
signaturelock-open Commit is signed but in an unrecognized format.

implement compile error note for function parameter type mismatch


2 files changed, 29 insertions(+), 5 deletions(-)

src/ir.cpp+13-1
......@@ -67,6 +67,8 @@ enum ConstCastResultId {
6767struct ConstCastOnly;
6868struct ConstCastArg {
6969 size_t arg_index;
70 ZigType *actual_param_type;
71 ZigType *expected_param_type;
7072 ConstCastOnly *child;
7173};
7274
......@@ -8638,6 +8640,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
86388640 if (arg_child.id != ConstCastResultIdOk) {
86398641 result.id = ConstCastResultIdFnArg;
86408642 result.data.fn_arg.arg_index = i;
8643 result.data.fn_arg.actual_param_type = actual_param_info->type;
8644 result.data.fn_arg.expected_param_type = expected_param_info->type;
86418645 result.data.fn_arg.child = allocate_nonzero<ConstCastOnly>(1);
86428646 *result.data.fn_arg.child = arg_child;
86438647 return result;
......@@ -10483,6 +10487,15 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1048310487 }
1048410488 break;
1048510489 }
10490 case ConstCastResultIdFnArg: {
10491 ErrorMsg *msg = add_error_note(ira->codegen, parent_msg, source_node,
10492 buf_sprintf("parameter %" ZIG_PRI_usize ": '%s' cannot cast into '%s'",
10493 cast_result->data.fn_arg.arg_index,
10494 buf_ptr(&cast_result->data.fn_arg.actual_param_type->name),
10495 buf_ptr(&cast_result->data.fn_arg.expected_param_type->name)));
10496 report_recursive_error(ira, source_node, cast_result->data.fn_arg.child, msg);
10497 break;
10498 }
1048610499 case ConstCastResultIdFnAlign: // TODO
1048710500 case ConstCastResultIdFnCC: // TODO
1048810501 case ConstCastResultIdFnVarArgs: // TODO
......@@ -10490,7 +10503,6 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1049010503 case ConstCastResultIdFnReturnType: // TODO
1049110504 case ConstCastResultIdFnArgCount: // TODO
1049210505 case ConstCastResultIdFnGenericArgCount: // TODO
10493 case ConstCastResultIdFnArg: // TODO
1049410506 case ConstCastResultIdFnArgNoAlias: // TODO
1049510507 case ConstCastResultIdUnresolvedInferredErrSet: // TODO
1049610508 case ConstCastResultIdAsyncAllocatorType: // TODO
test/compile_errors.zig+16-4
......@@ -1,6 +1,18 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "error note for function parameter incompatibility",
6 \\fn do_the_thing(func: fn (arg: i32) void) void {}
7 \\fn bar(arg: bool) void {}
8 \\export fn entry() void {
9 \\ do_the_thing(bar);
10 \\}
11 ,
12 ".tmp_source.zig:4:18: error: expected type 'fn(i32) void', found 'fn(bool) void",
13 ".tmp_source.zig:4:18: note: parameter 0: 'bool' cannot cast into 'i32'",
14 );
15
416 cases.add(
517 "cast negative value to unsigned integer",
618 \\comptime {
......@@ -5248,8 +5260,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52485260 \\export fn foo() void {
52495261 \\ asm volatile ("" : : [bar]"r"(3) : "");
52505262 \\}
5251 ,
5252 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_int",
5263 ,
5264 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_int",
52535265 );
52545266
52555267 cases.add(
......@@ -5257,7 +5269,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
52575269 \\export fn foo() void {
52585270 \\ asm volatile ("" : : [bar]"r"(3.17) : "");
52595271 \\}
5260 ,
5261 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",
5272 ,
5273 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",
52625274 );
52635275}