mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: add check for implicit fixed array decomp in function varargs (#16806)
This commit is contained in:
parent
c7f1db2b8a
commit
828cd4fe79
@ -2273,7 +2273,11 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
|
||||
ast.ArrayDecompose {
|
||||
typ := c.expr(node.expr)
|
||||
type_sym := c.table.sym(typ)
|
||||
if type_sym.kind != .array {
|
||||
if type_sym.kind == .array_fixed {
|
||||
c.error('direct decomposition of fixed array is not allowed, convert the fixed array to normal array via ${node.expr}[..]',
|
||||
node.expr.pos())
|
||||
return ast.void_type
|
||||
} else if type_sym.kind != .array {
|
||||
c.error('decomposition can only be used on arrays', node.expr.pos())
|
||||
return ast.void_type
|
||||
}
|
||||
|
6
vlib/v/checker/tests/fixed_array_decompose_err.out
Normal file
6
vlib/v/checker/tests/fixed_array_decompose_err.out
Normal file
@ -0,0 +1,6 @@
|
||||
vlib/v/checker/tests/fixed_array_decompose_err.vv:11:9: error: direct decomposition of fixed array is not allowed, convert the fixed array to normal array via arr[..]
|
||||
9 | fn main() {
|
||||
10 | arr := [1, 2, 3, 4]!
|
||||
11 | sum(...arr)
|
||||
| ~~~
|
||||
12 | }
|
12
vlib/v/checker/tests/fixed_array_decompose_err.vv
Normal file
12
vlib/v/checker/tests/fixed_array_decompose_err.vv
Normal file
@ -0,0 +1,12 @@
|
||||
fn sum(a ...int) int {
|
||||
mut total := 0
|
||||
for x in a {
|
||||
total += x
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
fn main() {
|
||||
arr := [1, 2, 3, 4]!
|
||||
sum(...arr)
|
||||
}
|
12
vlib/v/tests/fixed_array_explicit_decompose_test.v
Normal file
12
vlib/v/tests/fixed_array_explicit_decompose_test.v
Normal file
@ -0,0 +1,12 @@
|
||||
fn sum(a ...int) int {
|
||||
mut total := 0
|
||||
for x in a {
|
||||
total += x
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
fn test_fixed_array_explicit_decompose() {
|
||||
arr := [1, 2, 3, 4]!
|
||||
assert sum(...arr[..]) == 10
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user