checker: check fn_variadic with array_decompose (#8894)

pull/8904/head
yuyi 2021-02-22 21:26:54 +08:00 committed by GitHub
parent 0029d3ca76
commit 7a6fd359d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View File

@ -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

View File

@ -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 | }

View File

@ -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))
}