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>
This commit is contained in:
Robert Griesemer 2022-01-06 10:58:30 -08:00
parent b9cae6f78f
commit 042548b1fd
14 changed files with 43 additions and 45 deletions

View file

@ -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
}