1
0
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:
Swastik Baranwal 2022-12-30 04:36:41 +05:30 committed by GitHub
parent c7f1db2b8a
commit 828cd4fe79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 1 deletions

View File

@ -2273,7 +2273,11 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
ast.ArrayDecompose { ast.ArrayDecompose {
typ := c.expr(node.expr) typ := c.expr(node.expr)
type_sym := c.table.sym(typ) 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()) c.error('decomposition can only be used on arrays', node.expr.pos())
return ast.void_type return ast.void_type
} }

View 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 | }

View 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)
}

View 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
}