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:
parent
43d0d0f322
commit
be7b0f1dc5
@ -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
|
||||||
|
@ -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 | }
|
13
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err.vv
Normal file
13
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err.vv
Normal 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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user