Skip to content

Commit

Permalink
cgen: fix variadic arg var passed to another call which expects varia…
Browse files Browse the repository at this point in the history
…dic (fix #22315) (#22317)
  • Loading branch information
felipensp committed Sep 26, 2024
1 parent 08eef34 commit e3084ba
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
21 changes: 14 additions & 7 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2607,16 +2607,23 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
}
}
} else {
noscan := g.check_noscan(arr_info.elem_type)
g.write('new_array_from_c_array${noscan}(${variadic_count}, ${variadic_count}, sizeof(${elem_type}), _MOV((${elem_type}[${variadic_count}]){')
for j in arg_nr .. args.len {
g.ref_or_deref_arg(args[j], arr_info.elem_type, node.language,
// passing variadic arg to another call which expects same array type
if args.len == 1 && args[arg_nr].typ.has_flag(.variadic)
&& args[arg_nr].typ == varg_type {
g.ref_or_deref_arg(args[arg_nr], arr_info.elem_type, node.language,
false)
if j < args.len - 1 {
g.write(', ')
} else {
noscan := g.check_noscan(arr_info.elem_type)
g.write('new_array_from_c_array${noscan}(${variadic_count}, ${variadic_count}, sizeof(${elem_type}), _MOV((${elem_type}[${variadic_count}]){')
for j in arg_nr .. args.len {
g.ref_or_deref_arg(args[j], arr_info.elem_type, node.language,
false)
if j < args.len - 1 {
g.write(', ')
}
}
g.write('}))')
}
g.write('}))')
}
} else {
g.write('__new_array(0, 0, sizeof(${elem_type}))')
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/fns/variadic_sumtype_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Either = string | int

fn tag(params ...Either) int {
mut c := 0
for p in params {
if p is int {
c += p
}
}
return c
}

fn div(params ...Either) int {
return tag(params)
}

fn test_main() {
assert dump(div('foo', 1, 2)) == 3
}

0 comments on commit e3084ba

Please sign in to comment.