From 7a6fd359d08474adeb36925bdcee31daac7d9e28 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 22 Feb 2021 21:26:54 +0800 Subject: [PATCH] checker: check fn_variadic with array_decompose (#8894) --- vlib/v/checker/checker.v | 5 +++++ .../tests/function_variadic_arg_array_decompose.out | 6 ++++++ .../tests/function_variadic_arg_array_decompose.vv | 12 ++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 vlib/v/checker/tests/function_variadic_arg_array_decompose.out create mode 100644 vlib/v/checker/tests/function_variadic_arg_array_decompose.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e308a8683f..6abf09dc0f 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1921,6 +1921,11 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { } else { f.params[i] } + if f.is_variadic && call_arg.expr is ast.ArrayDecompose { + if i > f.params.len - 1 { + c.error('too many arguments in call to `$f.name`', call_expr.pos) + } + } c.expected_type = arg.typ typ := c.expr(call_arg.expr) call_expr.args[i].typ = typ diff --git a/vlib/v/checker/tests/function_variadic_arg_array_decompose.out b/vlib/v/checker/tests/function_variadic_arg_array_decompose.out new file mode 100644 index 0000000000..f72b6e0fe4 --- /dev/null +++ b/vlib/v/checker/tests/function_variadic_arg_array_decompose.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/function_variadic_arg_array_decompose.vv:11:10: error: too many arguments in call to `sum` + 9 | fn main() { + 10 | b := [5, 6, 7] + 11 | println(sum(1, 2, ...b)) + | ~~~~~~~~~~~~~~~ + 12 | } diff --git a/vlib/v/checker/tests/function_variadic_arg_array_decompose.vv b/vlib/v/checker/tests/function_variadic_arg_array_decompose.vv new file mode 100644 index 0000000000..27fa0c2570 --- /dev/null +++ b/vlib/v/checker/tests/function_variadic_arg_array_decompose.vv @@ -0,0 +1,12 @@ +fn sum(a ...int) int { + mut total := 0 + for x in a { + total += x + } + return total +} + +fn main() { + b := [5, 6, 7] + println(sum(1, 2, ...b)) +}