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

checker: check mismatch of the fn array decompose argument (#15929)

This commit is contained in:
yuyi 2022-10-01 11:50:28 +08:00 committed by GitHub
parent 43d0d0f322
commit be7b0f1dc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 7 deletions

View File

@ -906,13 +906,20 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
c.expected_type = info.elem_type c.expected_type = info.elem_type
} }
typ := c.expr(call_arg.expr) typ := c.expr(call_arg.expr)
if i == node.args.len - 1 && c.table.sym(typ).kind == .array if i == node.args.len - 1 {
&& call_arg.expr !is ast.ArrayDecompose && !param.typ.has_flag(.generic) if c.table.sym(typ).kind == .array && call_arg.expr !is ast.ArrayDecompose
&& c.expected_type != typ { && !param.typ.has_flag(.generic) && c.expected_type != typ {
styp := c.table.type_to_str(typ) styp := c.table.type_to_str(typ)
elem_styp := c.table.type_to_str(c.expected_type) 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`', c.error('to pass `$call_arg.expr` ($styp) to `$func.name` (which accepts type `...$elem_styp`), use `...$call_arg.expr`',
node.pos) node.pos)
} else if call_arg.expr is ast.ArrayDecompose
&& c.table.sym(c.expected_type).kind == .sum_type && c.expected_type != typ {
expected_type := c.table.type_to_str(c.expected_type)
got_type := c.table.type_to_str(typ)
c.error('cannot use `...$got_type` as `...$expected_type` in argument ${i + 1} to `$fn_name`',
call_arg.pos)
}
} }
} else { } else {
c.expected_type = param.typ c.expected_type = param.typ

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err.vv:12:7: error: cannot use `...string` as `...Any` in argument 1 to `test`
10 | mut args := []string{cap: 3}
11 | args << ['test', 'test1', 'test2']
12 | test(...args)
| ~~~~~~~
13 | }

View File

@ -0,0 +1,13 @@
module main
type Any = string | u8
fn test(args ...Any) {
println('args $args')
}
fn main() {
mut args := []string{cap: 3}
args << ['test', 'test1', 'test2']
test(...args)
}