Skip to content

Commit

Permalink
cgen: fix array decomposing on variadic call (found while working on …
Browse files Browse the repository at this point in the history
…solving #23474) (#23476)
  • Loading branch information
felipensp authored Jan 15, 2025
1 parent 1281957 commit db8d251
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// only v variadic, C variadic args will be appended like normal args
is_variadic := expected_types.len > 0 && expected_types.last().has_flag(.variadic)
&& node.language == .v
mut already_decomposed := false
for i, arg in args {
if is_variadic && i == expected_types.len - 1 {
break
Expand Down Expand Up @@ -2409,6 +2410,7 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
}
d_count++
}
already_decomposed = true
continue
} else if arg.expr is ast.ComptimeSelector && i < node.expected_arg_types.len
&& node.expected_arg_types[i].has_flag(.generic) {
Expand Down Expand Up @@ -2509,7 +2511,9 @@ fn (mut g Gen) call_args(node ast.CallExpr) {
// TODOC2V handle this in a better place
g.expr(args[0].expr)
} else if args.len > 0 && args.last().expr is ast.ArrayDecompose {
g.expr(args.last().expr)
if !already_decomposed {
g.expr(args.last().expr)
}
} else {
if variadic_count > 0 {
if g.pref.translated || g.file.is_translated {
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/fns/decompose_variadic_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import arrays

fn get() []int {
return [1, 2, 3, 4, 5]
}

fn get2() [][]int {
return [[0], [1, 2, 3, 4, 5]]
}

fn receive(a ...int) {
}

fn test_main() {
assert arrays.concat([0], ...get().map(it)) == [0, 1, 2, 3, 4, 5]
assert arrays.concat[int]([], ...get().map(it)) == [1, 2, 3, 4, 5]
assert arrays.concat[[]int]([[0]], ...[get().map(it)]) == [
[0],
[1, 2, 3, 4, 5],
]
assert arrays.concat[int](...get2()) == [0, 1, 2, 3, 4, 5]
}

0 comments on commit db8d251

Please sign in to comment.