1
0
mirror of https://github.com/golang/go synced 2024-11-26 14:56:47 -07:00

go/types: match types2 errors for incorrect method receiver count

Use "method has no receiver" and "method has multiple receivers"
in error messages for invalid receiver counts, matching the
corresponding types2 errors.

For #54511.

Change-Id: I96fc99440d6206c74e9416069db052234baa8248
Reviewed-on: https://go-review.googlesource.com/c/go/+/424934
Reviewed-by: Alan Donovan <adonovan@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
This commit is contained in:
Robert Griesemer 2022-08-18 20:16:15 -07:00
parent 84232b0b89
commit dbc3b44f85
3 changed files with 7 additions and 7 deletions

View File

@ -394,7 +394,7 @@ func (check *Checker) collectObjects() {
if d.decl.Recv.NumFields() == 0 {
// regular function
if d.decl.Recv != nil {
check.error(d.decl.Recv, _BadRecv, "method is missing receiver")
check.error(d.decl.Recv, _BadRecv, "method has no receiver")
// treat as function
}
if name == "init" || (name == "main" && check.pkg.name == "main") {

View File

@ -196,7 +196,7 @@ func (check *Checker) funcType(sig *Signature, recvPar *ast.FieldList, ftyp *ast
recv = NewParam(token.NoPos, nil, "", Typ[Invalid]) // ignore recv below
default:
// more than one receiver
check.error(recvList[len(recvList)-1], _InvalidRecv, "method must have exactly one receiver")
check.error(recvList[len(recvList)-1], _InvalidRecv, "method has multiple receivers")
fallthrough // continue with first receiver
case 1:
recv = recvList[0]

View File

@ -86,11 +86,11 @@ func (ptr /* ERROR "invalid receiver" */ ) _() {}
func (* /* ERROR "invalid receiver" */ ptr) _() {}
// Methods with zero or multiple receivers.
func ( /* ERROR "missing receiver" */ ) _() {}
func (T3, * /* ERROR "exactly one receiver" */ T3) _() {}
func (T3, T3, T3 /* ERROR "exactly one receiver" */ ) _() {}
func (a, b /* ERROR "exactly one receiver" */ T3) _() {}
func (a, b, c /* ERROR "exactly one receiver" */ T3) _() {}
func ( /* ERROR "method has no receiver" */ ) _() {}
func (T3, * /* ERROR "method has multiple receivers" */ T3) _() {}
func (T3, T3, T3 /* ERROR "method has multiple receivers" */ ) _() {}
func (a, b /* ERROR "method has multiple receivers" */ T3) _() {}
func (a, b, c /* ERROR "method has multiple receivers" */ T3) _() {}
// Methods associated with non-local or unnamed types.
func (int /* ERROR "cannot define new methods on non-local type int" */ ) m() {}