1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: change array decompose syntax (#7965)

This commit is contained in:
Daniel Däschle
2021-01-09 22:48:23 +01:00
committed by GitHub
parent 362c21de06
commit a8dd13f086
8 changed files with 17 additions and 12 deletions

View File

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

View File

@@ -1,5 +1,5 @@
fn varargs(a ...int) { println(a) }
fn main() {
varargs(123...)
varargs(...123)
}

View File

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

View File

@@ -4,5 +4,5 @@ fn varargs(a ...int) {
fn main() {
a := [1, 2, 3]
varargs(a...)
varargs(...a)
}

View File

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

View File

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