From a8dd13f0865499349a445e98fb23e9d25fc25cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Sat, 9 Jan 2021 22:48:23 +0100 Subject: [PATCH] parser: change array decompose syntax (#7965) --- CHANGELOG.md | 1 + doc/docs.md | 4 ++-- vlib/v/checker/tests/decompose_type_err.out | 8 ++++---- vlib/v/checker/tests/decompose_type_err.vv | 2 +- vlib/v/fmt/fmt.v | 2 +- vlib/v/fmt/tests/array_decomposition_keep.vv | 2 +- vlib/v/parser/fn.v | 6 +++++- vlib/v/tests/fn_variadic_test.v | 4 ++-- 8 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfaf0461cc..7966072dee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - `byte.str()` has been fixed and works like with all other numbers. `byte.ascii_str()` has been added. - Smart cast in for loops: `for mut x is string {}`. - `[noinit]` struct attribute to disallow direct struct initialization with `Foo{}`. +- Array decompose: `[1, 2, 3]...` is now `...[1, 2, 3]` - Treating `enum` as `int` is removed for strict type checking. - Support `[manualfree] fn f1(){}` and `[manualfree] module m1`, for functions doing their own memory management. diff --git a/doc/docs.md b/doc/docs.md index 48556f96ba..6912c4d5bd 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -245,9 +245,9 @@ println(sum(2,3)) // 5 // using array decomposition a := [2,3,4] -println(sum(a...)) // <-- using postfix ... here. output: 9 +println(sum(...a)) // <-- using prefix ... here. output: 9 b := [5, 6, 7] -println(sum(b...)) // output: 18 +println(sum(...b)) // output: 18 ``` ## Symbol visibility diff --git a/vlib/v/checker/tests/decompose_type_err.out b/vlib/v/checker/tests/decompose_type_err.out index 03e6154fc1..95d56f66c0 100644 --- a/vlib/v/checker/tests/decompose_type_err.out +++ b/vlib/v/checker/tests/decompose_type_err.out @@ -1,6 +1,6 @@ -vlib/v/checker/tests/decompose_type_err.vv:4:10: error: decomposition can only be used on arrays +vlib/v/checker/tests/decompose_type_err.vv:4:13: error: decomposition can only be used on arrays 2 | 3 | fn main() { - 4 | varargs(123...) - | ~~~ - 5 | } + 4 | varargs(...123) + | ~~~ + 5 | } \ No newline at end of file diff --git a/vlib/v/checker/tests/decompose_type_err.vv b/vlib/v/checker/tests/decompose_type_err.vv index 9e4de49855..d5b43bae26 100644 --- a/vlib/v/checker/tests/decompose_type_err.vv +++ b/vlib/v/checker/tests/decompose_type_err.vv @@ -1,5 +1,5 @@ fn varargs(a ...int) { println(a) } fn main() { - varargs(123...) + varargs(...123) } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 24710e744d..928882d1d5 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1237,8 +1237,8 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.write('}') } ast.ArrayDecompose { - f.expr(node.expr) f.write('...') + f.expr(node.expr) } } } diff --git a/vlib/v/fmt/tests/array_decomposition_keep.vv b/vlib/v/fmt/tests/array_decomposition_keep.vv index 2822cd955a..387997f8b9 100644 --- a/vlib/v/fmt/tests/array_decomposition_keep.vv +++ b/vlib/v/fmt/tests/array_decomposition_keep.vv @@ -4,5 +4,5 @@ fn varargs(a ...int) { fn main() { a := [1, 2, 3] - varargs(a...) + varargs(...a) } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index a53469d60c..8032327809 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -127,9 +127,13 @@ pub fn (mut p Parser) call_args() []ast.CallArg { } mut comments := p.eat_comments() arg_start_pos := p.tok.position() - mut e := p.expr(0) + mut array_decompose := false if p.tok.kind == .ellipsis { p.next() + array_decompose = true + } + mut e := p.expr(0) + if array_decompose { e = ast.ArrayDecompose{ expr: e pos: p.tok.position() diff --git a/vlib/v/tests/fn_variadic_test.v b/vlib/v/tests/fn_variadic_test.v index 391284a070..ac610e1063 100644 --- a/vlib/v/tests/fn_variadic_test.v +++ b/vlib/v/tests/fn_variadic_test.v @@ -34,7 +34,7 @@ fn test_fn_variadic_generic() { */ // forwarding fn variadic_forward_a(a ...string) string { - return variadic_fn_a(a...) + return variadic_fn_a(...a) } fn variadic_fn_a(a ...string) string { @@ -66,7 +66,7 @@ fn test_variadic_only_with_no_vargs() { fn test_array_decomposition_to_vargs() { a := ['a', 'b', 'c'] - assert variadic_fn_a(a...) == 'abc' + assert variadic_fn_a(...a) == 'abc' } struct VaTestStruct {