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:
parent
6b3bb4ba3b
commit
a59808ed01
@ -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
22
src/go/parser/testdata/issue34946.src
vendored
Normal 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 _() {}
|
Loading…
Reference in New Issue
Block a user