1
0
mirror of https://github.com/golang/go synced 2024-09-30 10:28:33 -06:00

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/+/202484
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Robert Griesemer 2019-10-21 15:29:41 -07:00
parent 6b3bb4ba3b
commit a59808ed01
2 changed files with 33 additions and 1 deletions

View File

@ -2439,8 +2439,18 @@ func (p *parser) parseFuncDecl() *ast.FuncDecl {
var body *ast.BlockStmt
if p.tok == token.LBRACE {
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{
Doc: doc,

22
src/go/parser/testdata/issue34946.src vendored Normal file
View File

@ -0,0 +1,22 @@
// 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 _() {}