summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2021-08-26 14:52:32 -0700
committerRobert Griesemer <gri@golang.org>2021-08-27 05:13:44 +0000
commit2c60a99f723e779a39664b5f12cb41878fd4a700 (patch)
treeeee72c2e467d94b888fde53f89258f2679baf3f4
parentd350a6653252fe1f698ac00be99964fa31d06e89 (diff)
downloadgo-git-2c60a99f723e779a39664b5f12cb41878fd4a700.tar.gz
cmd/compile/internal/syntax: make valid type parameter list in presence of errors
Make sure the parser fills in names and types for type parameter lists, even in the case of errors. While at it, adjust some of the test functions to accept generic code and report all syntax errors. Added offending source as test for types2. Fixes #47996. Change-Id: I449bcf5e2cb80fa2a24cdd3945f484bfca218a06 Reviewed-on: https://go-review.googlesource.com/c/go/+/345476 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com>
-rw-r--r--src/cmd/compile/internal/syntax/dumper_test.go6
-rw-r--r--src/cmd/compile/internal/syntax/parser.go5
-rw-r--r--src/cmd/compile/internal/syntax/printer_test.go6
-rw-r--r--src/cmd/compile/internal/types2/testdata/fixedbugs/issue47996.go28
4 files changed, 11 insertions, 14 deletions
diff --git a/src/cmd/compile/internal/syntax/dumper_test.go b/src/cmd/compile/internal/syntax/dumper_test.go
index 22680dce78..033283a352 100644
--- a/src/cmd/compile/internal/syntax/dumper_test.go
+++ b/src/cmd/compile/internal/syntax/dumper_test.go
@@ -13,11 +13,7 @@ func TestDump(t *testing.T) {
t.Skip("skipping test in short mode")
}
- // provide a no-op error handler so parsing doesn't stop after first error
- ast, err := ParseFile(*src_, func(error) {}, nil, CheckBranches)
- if err != nil {
- t.Error(err)
- }
+ ast, _ := ParseFile(*src_, func(err error) { t.Error(err) }, nil, CheckBranches|AllowGenerics)
if ast != nil {
Fdump(testOut(), ast)
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index fd97279f9d..3d1ca9d6d4 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -1924,7 +1924,7 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
}
// distribute parameter types (len(list) > 0)
- if named == 0 {
+ if named == 0 && !requireNames {
// all unnamed => found names are named types
for _, par := range list {
if typ := par.Name; typ != nil {
@@ -1932,9 +1932,6 @@ func (p *parser) paramList(name *Name, close token, requireNames bool) (list []*
par.Name = nil
}
}
- if requireNames {
- p.syntaxErrorAt(list[0].Type.Pos(), "type parameters must be named")
- }
} else if named != len(list) {
// some named => all must have names and types
var pos Pos // left-most error position (or unknown)
diff --git a/src/cmd/compile/internal/syntax/printer_test.go b/src/cmd/compile/internal/syntax/printer_test.go
index ec4b1de573..638e6d4a9d 100644
--- a/src/cmd/compile/internal/syntax/printer_test.go
+++ b/src/cmd/compile/internal/syntax/printer_test.go
@@ -18,11 +18,7 @@ func TestPrint(t *testing.T) {
t.Skip("skipping test in short mode")
}
- // provide a no-op error handler so parsing doesn't stop after first error
- ast, err := ParseFile(*src_, func(error) {}, nil, 0)
- if err != nil {
- t.Error(err)
- }
+ ast, _ := ParseFile(*src_, func(err error) { t.Error(err) }, nil, AllowGenerics)
if ast != nil {
Fprint(testOut(), ast, LineForm)
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue47996.go2 b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue47996.go2
new file mode 100644
index 0000000000..56e90942ab
--- /dev/null
+++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue47996.go2
@@ -0,0 +1,8 @@
+// Copyright 2021 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
+
+// don't crash
+func T /* ERROR missing */ [P /* ERROR named */ ] m /* ERROR m */ () /* ERROR \) */ { /* ERROR { */ } /* ERROR } */