parser: disallow indexing on next line at top-level (#8128)

pull/8159/head
Nick Treleaven 2021-01-17 04:30:41 +00:00 committed by GitHub
parent 4044abef0e
commit 8ee67d1c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1 @@

View File

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