diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 259df6e1f9..62e0465f3f 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -390,7 +390,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl { no_body := p.tok.kind != .lcbr body_start_pos := p.peek_tok.position() if p.tok.kind == .lcbr { + p.inside_fn = true stmts = p.parse_block_no_scope(true) + p.inside_fn = false } if !no_body && are_args_type_only { p.error_with_pos('functions with type only args can not have bodies', body_start_pos) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2e79e1563a..84d2ab92d1 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -41,7 +41,7 @@ mut: inside_ct_if_expr bool inside_or_expr bool inside_for bool - inside_fn bool + inside_fn bool // true even with implicit main inside_str_interp bool or_is_handled bool // ignore `or` in this expression builtin_mod bool // are we in the `builtin` module? @@ -532,6 +532,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt { return p.comment_stmt() } else { + p.inside_fn = true if p.pref.is_script && !p.pref.is_test { mut stmts := []ast.Stmt{} for p.tok.kind != .eof { diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 4c0ec7ab69..80e4465d48 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -286,7 +286,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 == .lsbr { + } else if p.tok.kind == .lsbr && (p.inside_fn || p.tok.line_nr == p.prev_tok.line_nr) { node = p.index_expr(node) p.is_stmt_ident = is_stmt_ident if p.tok.kind == .lpar && p.tok.line_nr == p.prev_tok.line_nr && node is ast.IndexExpr { diff --git a/vlib/v/parser/tests/const_index.out b/vlib/v/parser/tests/const_index.out new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/vlib/v/parser/tests/const_index.out @@ -0,0 +1 @@ + diff --git a/vlib/v/parser/tests/const_index.vv b/vlib/v/parser/tests/const_index.vv new file mode 100644 index 0000000000..c629502340 --- /dev/null +++ b/vlib/v/parser/tests/const_index.vv @@ -0,0 +1,22 @@ +// must not parse 4[deprecated] as index expression +const x = 4 +[deprecated] +fn g() { + a := [3] + // indexing is currently allowed on next line + _ = a + [0] +} + +const y = 5 +[deprecated] +fn h() {} + +const z = 6 +[typedef] +struct C.Foo{} + +// test implicit main allows indexing on next line +a := [3] +_ := a +[0]