Skip to content

Commit 1a6bb3d

Browse files
authored
checker: correctly detect variadic arg passed to struct fn (fix #25504) (#25509)
1 parent e5a5f02 commit 1a6bb3d

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

‎vlib/v/checker/fn.v‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
15301530
if i == args_len - 1 {
15311531
c.check_variadic_arg(call_arg.expr, typ, expected_type, param.typ, i + 1,
15321532
func.name, func.is_method, func.is_variadic, args_len == 1 && i == 0,
1533-
node.pos, call_arg.pos)
1533+
func.generic_names.len > 0, node.pos, call_arg.pos)
15341534
}
15351535
} else {
15361536
c.expected_type = param.typ
@@ -2542,8 +2542,8 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
25422542
typ := c.expr(mut arg.expr)
25432543
if i == node.args.len - 1 {
25442544
c.check_variadic_arg(arg.expr, typ, expected_type, param.typ, i + 1, method.name,
2545-
true, method.is_variadic, node.args.len == 1 && i == 0, node.pos,
2546-
arg.pos)
2545+
true, method.is_variadic, node.args.len == 1 && i == 0, method.generic_names.len > 0,
2546+
node.pos, arg.pos)
25472547
}
25482548
} else {
25492549
c.expected_type = param.typ
@@ -3980,11 +3980,11 @@ fn (mut c Checker) has_veb_context(typ ast.Type) bool {
39803980

39813981
fn (mut c Checker) check_variadic_arg(arg_expr ast.Expr, typ ast.Type, expected_type ast.Type, param_typ ast.Type,
39823982
arg_num int, fn_name string, is_method bool, fn_is_variadic bool, is_single_array_arg bool,
3983-
call_pos token.Pos, arg_pos token.Pos) {
3983+
is_generic_fn bool, call_pos token.Pos, arg_pos token.Pos) {
39843984
kind := c.table.sym(typ).kind
39853985
is_decompose := arg_expr is ast.ArrayDecompose
39863986
mut allow_variadic_pass := false
3987-
if arg_expr is ast.Ident && !is_method {
3987+
if arg_expr is ast.Ident && !(is_method && is_generic_fn) {
39883988
ident := arg_expr as ast.Ident
39893989
if ident.obj is ast.Var {
39903990
var_obj := ident.obj as ast.Var
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct Foo {
2+
}
3+
4+
type Type = string | int
5+
6+
fn (f Foo) b(t ...Type) int {
7+
return 0
8+
}
9+
10+
fn (f Foo) a(f2 &Foo, t ...Type) int {
11+
a := f2.b(t)
12+
return a
13+
}
14+
15+
fn test_main() {
16+
f := Foo{}
17+
t := []Type{}
18+
assert f.a(&f, ...t) == 0
19+
}

0 commit comments

Comments
 (0)