cgen: fix array decomposing on variadic call (found while working on solving #23474) (#23476)

This commit is contained in:
Felipe Pena 2025-01-15 14:32:03 -03:00 committed by GitHub
parent 128195741e
commit db8d2510a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View File

@ -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
@ -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) {
@ -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 {

View File

@ -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]
}