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
signature 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 {...@@ -67,6 +67,8 @@ enum ConstCastResultId {
67struct ConstCastOnly;67struct ConstCastOnly;
68struct ConstCastArg {68struct ConstCastArg {
69 size_t arg_index;69 size_t arg_index;
70 ZigType *actual_param_type;
71 ZigType *expected_param_type;
70 ConstCastOnly *child;72 ConstCastOnly *child;
71};73};
7274
...@@ -8638,6 +8640,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted...@@ -8638,6 +8640,8 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
8638 if (arg_child.id != ConstCastResultIdOk) {8640 if (arg_child.id != ConstCastResultIdOk) {
8639 result.id = ConstCastResultIdFnArg;8641 result.id = ConstCastResultIdFnArg;
8640 result.data.fn_arg.arg_index = i;8642 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;
8641 result.data.fn_arg.child = allocate_nonzero<ConstCastOnly>(1);8645 result.data.fn_arg.child = allocate_nonzero<ConstCastOnly>(1);
8642 *result.data.fn_arg.child = arg_child;8646 *result.data.fn_arg.child = arg_child;
8643 return result;8647 return result;
...@@ -10483,6 +10487,15 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa...@@ -10483,6 +10487,15 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
10483 }10487 }
10484 break;10488 break;
10485 }10489 }
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 }
10486 case ConstCastResultIdFnAlign: // TODO10499 case ConstCastResultIdFnAlign: // TODO
10487 case ConstCastResultIdFnCC: // TODO10500 case ConstCastResultIdFnCC: // TODO
10488 case ConstCastResultIdFnVarArgs: // TODO10501 case ConstCastResultIdFnVarArgs: // TODO
...@@ -10490,7 +10503,6 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa...@@ -10490,7 +10503,6 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
10490 case ConstCastResultIdFnReturnType: // TODO10503 case ConstCastResultIdFnReturnType: // TODO
10491 case ConstCastResultIdFnArgCount: // TODO10504 case ConstCastResultIdFnArgCount: // TODO
10492 case ConstCastResultIdFnGenericArgCount: // TODO10505 case ConstCastResultIdFnGenericArgCount: // TODO
10493 case ConstCastResultIdFnArg: // TODO
10494 case ConstCastResultIdFnArgNoAlias: // TODO10506 case ConstCastResultIdFnArgNoAlias: // TODO
10495 case ConstCastResultIdUnresolvedInferredErrSet: // TODO10507 case ConstCastResultIdUnresolvedInferredErrSet: // TODO
10496 case ConstCastResultIdAsyncAllocatorType: // TODO10508 case ConstCastResultIdAsyncAllocatorType: // TODO
test/compile_errors.zig+16-4
...@@ -1,6 +1,18 @@...@@ -1,6 +1,18 @@
1const tests = @import("tests.zig");1const tests = @import("tests.zig");
22
3pub fn addCases(cases: *tests.CompileErrorContext) void {3pub 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
4 cases.add(16 cases.add(
5 "cast negative value to unsigned integer",17 "cast negative value to unsigned integer",
6 \\comptime {18 \\comptime {
...@@ -5248,8 +5260,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5248,8 +5260,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5248 \\export fn foo() void {5260 \\export fn foo() void {
5249 \\ asm volatile ("" : : [bar]"r"(3) : "");5261 \\ asm volatile ("" : : [bar]"r"(3) : "");
5250 \\}5262 \\}
5251 ,5263 ,
5252 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_int",5264 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_int",
5253 );5265 );
52545266
5255 cases.add(5267 cases.add(
...@@ -5257,7 +5269,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5257,7 +5269,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5257 \\export fn foo() void {5269 \\export fn foo() void {
5258 \\ asm volatile ("" : : [bar]"r"(3.17) : "");5270 \\ asm volatile ("" : : [bar]"r"(3.17) : "");
5259 \\}5271 \\}
5260 ,5272 ,
5261 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",5273 ".tmp_source.zig:2:35: error: expected sized integer or sized float, found comptime_float",
5262 );5274 );
5263}5275}