Commit a59808ed authored by Robert Griesemer's avatar Robert Griesemer

go/parser: better error (recovery) for Allman/BSD-style func decls

This matches the behavior and error of cmd/compile.

Fixes #34946.

Change-Id: I329ef358deea63d8425f76f1d54c95749b96c365
Reviewed-on: https://go-review.googlesource.com/c/go/+/202484Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 6b3bb4ba
...@@ -2439,8 +2439,18 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl { ...@@ -2439,8 +2439,18 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
var body *ast.BlockStmt var body *ast.BlockStmt
if p.tok == token.LBRACE { if p.tok == token.LBRACE {
body = p.parseBody(scope) body = p.parseBody(scope)
p.expectSemi()
} else if p.tok == token.SEMICOLON {
p.next()
if p.tok == token.LBRACE {
// opening { of function declaration on next line
p.error(p.pos, "unexpected semicolon or newline before {")
body = p.parseBody(scope)
p.expectSemi()
} }
} else {
p.expectSemi() p.expectSemi()
}
decl := &ast.FuncDecl{ decl := &ast.FuncDecl{
Doc: doc, Doc: doc,
......
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test case for issue 34946: Better synchronization of
// parser for function declarations that start their
// body's opening { on a new line.
package p
// accept Allman/BSD-style declaration but complain
// (implicit semicolon between signature and body)
func _() int
{ /* ERROR "unexpected semicolon or newline before {" */
{ return 0 }
}
func _() {}
func _(); { /* ERROR "unexpected semicolon or newline before {" */ }
func _() {}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment