From 83ea97b1a394971981281154f5107e94cf53fe14 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 25 Feb 2022 20:54:12 +0800 Subject: [PATCH] parser: fix optional expr with array value (#13599) --- vlib/v/parser/expr.v | 4 +--- vlib/v/parser/tests/const_index.vv | 8 ++----- .../tests/option_expr_with_array_value_test.v | 23 +++++++++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 vlib/v/tests/option_expr_with_array_value_test.v diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index c1b14e79f7..bcc3d75607 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -376,9 +376,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden return node } p.is_stmt_ident = is_stmt_ident - } else if p.tok.kind in [.lsbr, .nilsbr] - && (p.inside_fn || p.tok.line_nr == p.prev_tok.line_nr) { - // node = p.index_expr(node) + } else if p.tok.kind in [.lsbr, .nilsbr] && p.tok.line_nr == p.prev_tok.line_nr { if p.tok.kind == .nilsbr { node = p.index_expr(node, true) } else { diff --git a/vlib/v/parser/tests/const_index.vv b/vlib/v/parser/tests/const_index.vv index c629502340..26b021582d 100644 --- a/vlib/v/parser/tests/const_index.vv +++ b/vlib/v/parser/tests/const_index.vv @@ -3,9 +3,7 @@ const x = 4 [deprecated] fn g() { a := [3] - // indexing is currently allowed on next line - _ = a - [0] + _ = a[0] } const y = 5 @@ -16,7 +14,5 @@ const z = 6 [typedef] struct C.Foo{} -// test implicit main allows indexing on next line a := [3] -_ := a -[0] +_ := a[0] diff --git a/vlib/v/tests/option_expr_with_array_value_test.v b/vlib/v/tests/option_expr_with_array_value_test.v new file mode 100644 index 0000000000..b56e9d0028 --- /dev/null +++ b/vlib/v/tests/option_expr_with_array_value_test.v @@ -0,0 +1,23 @@ +struct Empty { + empty string +} + +fn print_error() ?[]Empty { + mut test := []Empty{} + test << Empty{ + empty: 'Test' + } + if test[0].empty != '' { + return error('Not empty') + } + return test +} + +fn test_option_expr_with_array_value() { + test_error := print_error() or { + eprintln(err) + []Empty{} + } + println(test_error) + assert '$test_error' == '[]' +}