Skip to content

Commit aa02a9c

Browse files
authored
checker: fix checking mismatch of the fn array decompose argument (#15934)
1 parent f6a6800 commit aa02a9c

5 files changed

+33
-9
lines changed

vlib/v/checker/fn.v

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -901,24 +901,27 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
901901
}
902902
if func.is_variadic && i >= func.params.len - 1 {
903903
param_sym := c.table.sym(param.typ)
904+
mut expected_type := param.typ
904905
if param_sym.kind == .array {
905906
info := param_sym.array_info()
906-
c.expected_type = info.elem_type
907+
expected_type = info.elem_type
908+
c.expected_type = expected_type
907909
}
908910
typ := c.expr(call_arg.expr)
909911
if i == node.args.len - 1 {
910912
if c.table.sym(typ).kind == .array && call_arg.expr !is ast.ArrayDecompose
911-
&& !param.typ.has_flag(.generic) && c.expected_type != typ {
913+
&& !param.typ.has_flag(.generic) && expected_type != typ {
912914
styp := c.table.type_to_str(typ)
913-
elem_styp := c.table.type_to_str(c.expected_type)
915+
elem_styp := c.table.type_to_str(expected_type)
914916
c.error('to pass `$call_arg.expr` ($styp) to `$func.name` (which accepts type `...$elem_styp`), use `...$call_arg.expr`',
915917
node.pos)
916918
} else if call_arg.expr is ast.ArrayDecompose
917-
&& c.table.sym(c.expected_type).kind == .sum_type && c.expected_type != typ {
918-
expected_type := c.table.type_to_str(c.expected_type)
919-
got_type := c.table.type_to_str(typ)
920-
c.error('cannot use `...$got_type` as `...$expected_type` in argument ${i + 1} to `$fn_name`',
921-
call_arg.pos)
919+
&& c.table.sym(expected_type).kind == .sum_type
920+
&& expected_type.idx() != typ.idx() {
921+
expected_type_str := c.table.type_to_str(expected_type)
922+
got_type_str := c.table.type_to_str(typ)
923+
c.error('cannot use `...$got_type_str` as `...$expected_type_str` in argument ${
924+
i + 1} to `$fn_name`', call_arg.pos)
922925
}
923926
}
924927
} else {

vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err.out renamed to vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err_a.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err.vv:12:7: error: cannot use `...string` as `...Any` in argument 1 to `test`
1+
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err_a.vv:12:7: error: cannot use `...string` as `...Any` in argument 1 to `test`
22
10 | mut args := []string{cap: 3}
33
11 | args << ['test', 'test1', 'test2']
44
12 | test(...args)
File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/checker/tests/fn_array_decompose_arg_mismatch_err_b.vv:14:4: error: cannot use `...Foo1` as `...Foo` in argument 1 to `f`
2+
12 |
3+
13 | fn main() {
4+
14 | f(...[Foo1{}, Foo1{}])
5+
| ~~~~~~~~~~~~~~~~~~~
6+
15 | }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct Foo1 {
2+
}
3+
4+
struct Foo2 {
5+
}
6+
7+
type Foo = Foo1 | Foo2
8+
9+
fn f(v ...Foo) {
10+
println(v)
11+
}
12+
13+
fn main() {
14+
f(...[Foo1{}, Foo1{}])
15+
}

0 commit comments

Comments
 (0)