authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-10 20:53:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-09-10 20:53:57-04:00
log28811234bb46824a55596a516f763a39bcbd508a
treeee8f7785b14c18fb363f61aebe4414f596edcc17
parent1fa4d2a5afd35ebb58c84ea030046fc557ffad01

fix compile error message for wrong arg count on method


2 files changed, 25 insertions(+), 9 deletions(-)

src/analyze.cpp+15-9
......@@ -1250,7 +1250,7 @@ static void resolve_function_proto(CodeGen *g, AstNode *node, FnTableEntry *fn_t
12501250 if (!fn_table_entry->is_extern) {
12511251 LLVMAddFunctionAttr(fn_table_entry->fn_value, LLVMNoUnwindAttribute);
12521252 }
1253 if (!g->is_release_build && !fn_proto->is_inline) {
1253 if (!g->is_release_build && fn_table_entry->fn_inline != FnInlineAlways) {
12541254 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim", "true");
12551255 ZigLLVMAddFunctionAttr(fn_table_entry->fn_value, "no-frame-pointer-elim-non-leaf", nullptr);
12561256 }
......@@ -5453,19 +5453,20 @@ static TypeTableEntry *analyze_fn_call_ptr(CodeGen *g, ImportTableEntry *import,
54535453 int src_param_count = fn_type->data.fn.fn_type_id.param_count +
54545454 (generic_proto_node ? generic_proto_node->data.fn_proto.inline_arg_count : 0);
54555455 int call_param_count = node->data.fn_call_expr.params.length;
5456 int expect_arg_count = src_param_count - struct_node_1_or_0;
54565457
54575458 bool ok_invocation = true;
54585459
54595460 if (fn_type->data.fn.fn_type_id.is_var_args) {
5460 if (call_param_count < src_param_count - struct_node_1_or_0) {
5461 if (call_param_count < expect_arg_count) {
54615462 ok_invocation = false;
54625463 add_node_error(g, node,
54635464 buf_sprintf("expected at least %d arguments, got %d", src_param_count, call_param_count));
54645465 }
5465 } else if (src_param_count - struct_node_1_or_0 != call_param_count) {
5466 } else if (expect_arg_count != call_param_count) {
54665467 ok_invocation = false;
54675468 add_node_error(g, node,
5468 buf_sprintf("expected %d arguments, got %d", src_param_count, call_param_count));
5469 buf_sprintf("expected %d arguments, got %d", expect_arg_count, call_param_count));
54695470 }
54705471
54715472 bool all_args_const_expr = true;
......@@ -5567,7 +5568,7 @@ static TypeTableEntry *analyze_fn_call_with_inline_args(CodeGen *g, ImportTableE
55675568
55685569 if (src_param_count != call_param_count + struct_node_1_or_0) {
55695570 add_node_error(g, call_node,
5570 buf_sprintf("expected %d arguments, got %d", src_param_count, call_param_count));
5571 buf_sprintf("expected %d arguments, got %d", src_param_count - struct_node_1_or_0, call_param_count));
55715572 return g->builtin_types.entry_invalid;
55725573 }
55735574
......@@ -6821,13 +6822,18 @@ static void count_inline_and_var_args(AstNode *proto_node) {
68216822 *inline_arg_count = 0;
68226823 *inline_or_var_type_arg_count = 0;
68236824
6825 // TODO run these nodes through the type analysis system rather than looking for
6826 // specialized ast nodes. this would get fooled by `{var}` instead of `var` which
6827 // is supposed to be equivalent
68246828 for (int i = 0; i < proto_node->data.fn_proto.params.length; i += 1) {
68256829 AstNode *param_node = proto_node->data.fn_proto.params.at(i);
68266830 assert(param_node->type == NodeTypeParamDecl);
6827 bool is_inline = param_node->data.param_decl.is_inline;
6828 *inline_arg_count += is_inline ? 1 : 0;
6829 *inline_or_var_type_arg_count += (is_inline ||
6830 param_node->data.param_decl.type->type == NodeTypeVarLiteral) ? 1 : 0;
6831 if (param_node->data.param_decl.is_inline) {
6832 *inline_arg_count += 1;
6833 *inline_or_var_type_arg_count += 1;
6834 } else if (param_node->data.param_decl.type->type == NodeTypeVarLiteral) {
6835 *inline_or_var_type_arg_count += 1;
6836 }
68316837 }
68326838}
68336839
test/run_tests.cpp+10
......@@ -1457,6 +1457,16 @@ fn f(m: []const u8) {
14571457 m.copy(u8, self.list.items[old_len...], m);
14581458}
14591459 )SOURCE", 1, ".tmp_source.zig:3:6: error: no member named 'copy' in '[]const u8'");
1460
1461 add_compile_fail_case("wrong number of arguments for method fn call", R"SOURCE(
1462struct Foo {
1463 fn method(self: &const Foo, a: i32) {}
1464}
1465fn f(foo: &const Foo) {
1466
1467 foo.method(1, 2);
1468}
1469 )SOURCE", 1, ".tmp_source.zig:7:15: error: expected 1 arguments, got 2");
14601470}
14611471
14621472//////////////////////////////////////////////////////////////////////////////