parser: disallow indexing on next line at top-level (#8128)
parent
4044abef0e
commit
8ee67d1c1c
|
@ -390,7 +390,9 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
||||||
no_body := p.tok.kind != .lcbr
|
no_body := p.tok.kind != .lcbr
|
||||||
body_start_pos := p.peek_tok.position()
|
body_start_pos := p.peek_tok.position()
|
||||||
if p.tok.kind == .lcbr {
|
if p.tok.kind == .lcbr {
|
||||||
|
p.inside_fn = true
|
||||||
stmts = p.parse_block_no_scope(true)
|
stmts = p.parse_block_no_scope(true)
|
||||||
|
p.inside_fn = false
|
||||||
}
|
}
|
||||||
if !no_body && are_args_type_only {
|
if !no_body && are_args_type_only {
|
||||||
p.error_with_pos('functions with type only args can not have bodies', body_start_pos)
|
p.error_with_pos('functions with type only args can not have bodies', body_start_pos)
|
||||||
|
|
|
@ -41,7 +41,7 @@ mut:
|
||||||
inside_ct_if_expr bool
|
inside_ct_if_expr bool
|
||||||
inside_or_expr bool
|
inside_or_expr bool
|
||||||
inside_for bool
|
inside_for bool
|
||||||
inside_fn bool
|
inside_fn bool // true even with implicit main
|
||||||
inside_str_interp bool
|
inside_str_interp bool
|
||||||
or_is_handled bool // ignore `or` in this expression
|
or_is_handled bool // ignore `or` in this expression
|
||||||
builtin_mod bool // are we in the `builtin` module?
|
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()
|
return p.comment_stmt()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
p.inside_fn = true
|
||||||
if p.pref.is_script && !p.pref.is_test {
|
if p.pref.is_script && !p.pref.is_test {
|
||||||
mut stmts := []ast.Stmt{}
|
mut stmts := []ast.Stmt{}
|
||||||
for p.tok.kind != .eof {
|
for p.tok.kind != .eof {
|
||||||
|
|
|
@ -286,7 +286,7 @@ pub fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_iden
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
p.is_stmt_ident = is_stmt_ident
|
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)
|
node = p.index_expr(node)
|
||||||
p.is_stmt_ident = is_stmt_ident
|
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 {
|
if p.tok.kind == .lpar && p.tok.line_nr == p.prev_tok.line_nr && node is ast.IndexExpr {
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
|
|
@ -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]
|
Loading…
Reference in New Issue