aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/syntax
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-01-06 10:58:30 -0800
committerRobert Griesemer <gri@golang.org>2022-01-06 23:39:43 +0000
commit042548b1fdba21e351368e9f3ecd93059d09083f (patch)
tree479a06878234d9158fa1eafaabe4f115ab0b976d /src/cmd/compile/internal/syntax
parentb9cae6f78f129bb3f1b3293da5375040f5c4f356 (diff)
downloadgo-042548b1fdba21e351368e9f3ecd93059d09083f.tar.xz
cmd/compile: report type parameter error for methods only once
Move switch to enable method type parameters entirely to the parser, by adding the mode AllowMethodTypeParams. Ensure that the error messages are consistent. Remove unnecessary code in the type checker. Fixes #50317. Change-Id: I4f3958722400bdb919efa4c494b85cf62f4002bb Reviewed-on: https://go-review.googlesource.com/c/go/+/376054 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/syntax')
-rw-r--r--src/cmd/compile/internal/syntax/parser.go12
-rw-r--r--src/cmd/compile/internal/syntax/parser_test.go2
-rw-r--r--src/cmd/compile/internal/syntax/syntax.go1
-rw-r--r--src/cmd/compile/internal/syntax/testdata/issue48382.go210
-rw-r--r--src/cmd/compile/internal/syntax/testdata/tparams.go22
5 files changed, 17 insertions, 10 deletions
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index 40c5eca408..a75a3b1a2e 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -760,7 +760,13 @@ func (p *parser) funcDeclOrNil() *FuncDecl {
}
f.Name = p.name()
- f.TParamList, f.Type = p.funcType("")
+
+ context := ""
+ if f.Recv != nil && p.mode&AllowMethodTypeParams == 0 {
+ 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()
}
@@ -1415,7 +1421,7 @@ func (p *parser) funcType(context string) ([]*Field, *FuncType) {
if p.allowGenerics() && p.got(_Lbrack) {
if context != "" {
// accept but complain
- p.syntaxErrorAt(typ.pos, context+" cannot have type parameters")
+ p.syntaxErrorAt(typ.pos, context+" must have no type parameters")
}
if p.tok == _Rbrack {
p.syntaxError("empty type parameter list")
@@ -1823,7 +1829,7 @@ func (p *parser) methodDecl() *Field {
// TODO(gri) Record list as type parameter list with f.Type
// if we want to type-check the generic method.
// For now, report an error so this is not a silent event.
- p.errorAt(pos, "interface method cannot have type parameters")
+ p.errorAt(pos, "interface method must have no type parameters")
break
}
diff --git a/src/cmd/compile/internal/syntax/parser_test.go b/src/cmd/compile/internal/syntax/parser_test.go
index 68f3c376c9..e258a17c38 100644
--- a/src/cmd/compile/internal/syntax/parser_test.go
+++ b/src/cmd/compile/internal/syntax/parser_test.go
@@ -46,7 +46,7 @@ func TestParseGo2(t *testing.T) {
for _, fi := range list {
name := fi.Name()
if !fi.IsDir() && !strings.HasPrefix(name, ".") {
- ParseFile(filepath.Join(dir, name), func(err error) { t.Error(err) }, nil, AllowGenerics)
+ ParseFile(filepath.Join(dir, name), func(err error) { t.Error(err) }, nil, AllowGenerics|AllowMethodTypeParams)
}
}
}
diff --git a/src/cmd/compile/internal/syntax/syntax.go b/src/cmd/compile/internal/syntax/syntax.go
index f3d4c09ed5..25c8116206 100644
--- a/src/cmd/compile/internal/syntax/syntax.go
+++ b/src/cmd/compile/internal/syntax/syntax.go
@@ -17,6 +17,7 @@ type Mode uint
const (
CheckBranches Mode = 1 << iota // check correct use of labels, break, continue, and goto statements
AllowGenerics
+ AllowMethodTypeParams // does not support interface methods yet; ignored if AllowGenerics is not set
)
// Error describes a syntax error. Error implements the error interface.
diff --git a/src/cmd/compile/internal/syntax/testdata/issue48382.go2 b/src/cmd/compile/internal/syntax/testdata/issue48382.go2
index 1e8f4b0ec6..c00fee6f82 100644
--- a/src/cmd/compile/internal/syntax/testdata/issue48382.go2
+++ b/src/cmd/compile/internal/syntax/testdata/issue48382.go2
@@ -4,12 +4,12 @@
package p
-type _ func /* ERROR function type cannot have type parameters */ [ /* ERROR empty type parameter list */ ]()
-type _ func /* ERROR function type cannot have type parameters */ [ x /* ERROR missing type constraint */ ]()
-type _ func /* ERROR function type cannot have type parameters */ [P any]()
+type _ func /* ERROR function type must have no type parameters */ [ /* ERROR empty type parameter list */ ]()
+type _ func /* ERROR function type must have no type parameters */ [ x /* ERROR missing type constraint */ ]()
+type _ func /* ERROR function type must have no type parameters */ [P any]()
-var _ = func /* ERROR function literal cannot have type parameters */ [P any]() {}
+var _ = func /* ERROR function literal must have no type parameters */ [P any]() {}
type _ interface{
- m /* ERROR interface method cannot have type parameters */ [P any]()
+ m /* ERROR interface method must have no type parameters */ [P any]()
}
diff --git a/src/cmd/compile/internal/syntax/testdata/tparams.go2 b/src/cmd/compile/internal/syntax/testdata/tparams.go2
index 80e155bfe0..a9bd72cf2d 100644
--- a/src/cmd/compile/internal/syntax/testdata/tparams.go2
+++ b/src/cmd/compile/internal/syntax/testdata/tparams.go2
@@ -13,7 +13,7 @@ type t struct {
}
type t interface {
t[a]
- m /* ERROR method cannot have type parameters */ [_ _, /* ERROR mixed */ _]()
+ m /* ERROR method must have no type parameters */ [_ _, /* ERROR mixed */ _]()
t[a, b]
}