diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 237d799e80..76643866f1 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 } diff --git a/vlib/v/checker/tests/fixed_array_decompose_err.out b/vlib/v/checker/tests/fixed_array_decompose_err.out new file mode 100644 index 0000000000..5c4a87c89d --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_decompose_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/fixed_array_decompose_err.vv b/vlib/v/checker/tests/fixed_array_decompose_err.vv new file mode 100644 index 0000000000..6648468131 --- /dev/null +++ b/vlib/v/checker/tests/fixed_array_decompose_err.vv @@ -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) +} diff --git a/vlib/v/tests/fixed_array_explicit_decompose_test.v b/vlib/v/tests/fixed_array_explicit_decompose_test.v new file mode 100644 index 0000000000..2ba630d428 --- /dev/null +++ b/vlib/v/tests/fixed_array_explicit_decompose_test.v @@ -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 +}