]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: better error message for erroneous method declaration
authorRobert Griesemer <gri@golang.org>
Tue, 4 Oct 2022 17:24:05 +0000 (10:24 -0700)
committerRobert Griesemer <gri@google.com>
Wed, 5 Oct 2022 01:05:29 +0000 (01:05 +0000)
Also make error recovery slightly more robust in this case.

Fixes #56022.

Change-Id: I1c01c1465adb48c71367d037b6f0e3fe56f68ec9
Reviewed-on: https://go-review.googlesource.com/c/go/+/438540
Run-TryBot: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/testdata/issue56022.go [new file with mode: 0644]

index b31b712f1d3da69903a8270cd0d324f3d2e07a0b..d80b26955713f7850f260bc3b5cea8e195c120f2 100644 (file)
@@ -767,7 +767,9 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
        f.pos = p.pos()
        f.Pragma = p.takePragma()
 
+       var context string
        if p.got(_Lparen) {
+               context = "method"
                rcvr := p.paramList(nil, nil, _Rparen, false)
                switch len(rcvr) {
                case 0:
@@ -780,19 +782,17 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
                }
        }
 
-       if p.tok != _Name {
-               p.syntaxError("expected name or (")
+       if p.tok == _Name {
+               f.Name = p.name()
+               f.TParamList, f.Type = p.funcType(context)
+       } else {
+               msg := "expected name or ("
+               if context != "" {
+                       msg = "expected name"
+               }
+               p.syntaxError(msg)
                p.advance(_Lbrace, _Semi)
-               return nil
-       }
-
-       f.Name = p.name()
-
-       context := ""
-       if f.Recv != nil {
-               context = "method" // don't permit (method) type parameters in funcType
        }
-       f.TParamList, f.Type = p.funcType(context)
 
        if p.tok == _Lbrace {
                f.Body = p.funcBody()
diff --git a/src/cmd/compile/internal/syntax/testdata/issue56022.go b/src/cmd/compile/internal/syntax/testdata/issue56022.go
new file mode 100644 (file)
index 0000000..d28d35c
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2022 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.
+
+package p
+
+func /* ERROR unexpected {, expected name or \($ */ {}
+func (T) /* ERROR unexpected {, expected name$ */ {}
+func (T) /* ERROR unexpected \(, expected name$ */ () {}
+func (T) /* ERROR unexpected \(, expected name$ */ ()