From 2a32dac40d922f24891a72d2ab7cbc05ffa21f91 Mon Sep 17 00:00:00 2001 From: zakuro Date: Sat, 30 Jan 2021 03:14:22 +0900 Subject: [PATCH] checker: fix panic when calling generic function with too few args (#8416) --- vlib/v/checker/check_types.v | 9 +++---- .../function_count_of_args_mismatch_err.out | 27 +++++++++++++++++++ .../function_count_of_args_mismatch_err.vv | 12 +++++++++ .../tests/function_too_many_args_err.out | 6 ----- .../tests/function_too_many_args_err.vv | 6 ----- 5 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 vlib/v/checker/tests/function_count_of_args_mismatch_err.out create mode 100644 vlib/v/checker/tests/function_count_of_args_mismatch_err.vv delete mode 100644 vlib/v/checker/tests/function_too_many_args_err.out delete mode 100644 vlib/v/checker/tests/function_too_many_args_err.vv diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 0f786acb1e..0bc1f3a6c8 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -421,14 +421,11 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) { } mut typ := table.void_type for i, param in f.params { - if call_expr.args.len == 0 { + arg_i := if i != 0 && call_expr.is_method { i - 1 } else { i } + if call_expr.args.len <= arg_i { break } - arg := if i != 0 && call_expr.is_method { - call_expr.args[i - 1] - } else { - call_expr.args[i] - } + arg := call_expr.args[arg_i] param_type_sym := c.table.get_type_symbol(param.typ) if param.typ.has_flag(.generic) && param_type_sym.name == gt_name { typ = arg.typ diff --git a/vlib/v/checker/tests/function_count_of_args_mismatch_err.out b/vlib/v/checker/tests/function_count_of_args_mismatch_err.out new file mode 100644 index 0000000000..d9ad12eea0 --- /dev/null +++ b/vlib/v/checker/tests/function_count_of_args_mismatch_err.out @@ -0,0 +1,27 @@ +vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:8:13: error: expected 1 arguments, but got 3 + 6 | + 7 | fn main() { + 8 | test(true, false, 1) + | ~~~~~~~~ + 9 | test() + 10 | test2(true, false, 1) +vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:9:2: error: expected 1 arguments, but got 0 + 7 | fn main() { + 8 | test(true, false, 1) + 9 | test() + | ~~~~~~ + 10 | test2(true, false, 1) + 11 | test2(true) +vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:10:21: error: expected 2 arguments, but got 3 + 8 | test(true, false, 1) + 9 | test() + 10 | test2(true, false, 1) + | ^ + 11 | test2(true) + 12 | } +vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:11:2: error: expected 2 arguments, but got 1 + 9 | test() + 10 | test2(true, false, 1) + 11 | test2(true) + | ~~~~~~~~~~~ + 12 | } diff --git a/vlib/v/checker/tests/function_count_of_args_mismatch_err.vv b/vlib/v/checker/tests/function_count_of_args_mismatch_err.vv new file mode 100644 index 0000000000..00fe2122cd --- /dev/null +++ b/vlib/v/checker/tests/function_count_of_args_mismatch_err.vv @@ -0,0 +1,12 @@ +fn test(b bool) { +} + +fn test2(b bool, v T) { +} + +fn main() { + test(true, false, 1) + test() + test2(true, false, 1) + test2(true) +} diff --git a/vlib/v/checker/tests/function_too_many_args_err.out b/vlib/v/checker/tests/function_too_many_args_err.out deleted file mode 100644 index 3836d7542a..0000000000 --- a/vlib/v/checker/tests/function_too_many_args_err.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/function_too_many_args_err.vv:5:16: error: expected 1 arguments, but got 3 - 3 | - 4 | fn main() { - 5 | test(true, false, 1) - | ~~~~~~~~ - 6 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/function_too_many_args_err.vv b/vlib/v/checker/tests/function_too_many_args_err.vv deleted file mode 100644 index ff9759bd97..0000000000 --- a/vlib/v/checker/tests/function_too_many_args_err.vv +++ /dev/null @@ -1,6 +0,0 @@ -fn test(b bool) { -} - -fn main() { - test(true, false, 1) -}