1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check fn variadic passing arg error (fix #15629) (#15632)

This commit is contained in:
yuyi 2022-09-02 02:24:24 +08:00 committed by GitHub
parent bfdd6f1cf8
commit 42e582804e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 0 deletions

View File

@ -872,6 +872,15 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
info := param_sym.array_info()
c.expected_type = info.elem_type
}
typ := c.expr(call_arg.expr)
if i == node.args.len - 1 && c.table.sym(typ).kind == .array
&& call_arg.expr !is ast.ArrayDecompose && !param.typ.has_flag(.generic)
&& c.expected_type != typ {
styp := c.table.type_to_str(typ)
elem_styp := c.table.type_to_str(c.expected_type)
c.error('to pass `$call_arg.expr` ($styp) to `$func.name` (which accepts type `...$elem_styp`), use `...$call_arg.expr`',
node.pos)
}
} else {
c.expected_type = param.typ
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/fn_variadic_arg_err.vv:5:2: error: to pass `a` ([]u8) to `varg_fn_a` (which accepts type `...u8`), use `...a`
3 | fn main() {
4 | a := [u8(1), 2, 3, 4]
5 | varg_fn_a(a)
| ~~~~~~~~~~~~
6 | }
7 |

View File

@ -0,0 +1,10 @@
module main
fn main() {
a := [u8(1), 2, 3, 4]
varg_fn_a(a)
}
fn varg_fn_a(b ...u8) {
println(b)
}