1
0
mirror of https://github.com/golang/go synced 2024-11-17 20:24:46 -07:00

cmd/go: produce a better error for generic test functions

Test functions with type parameters cannot be invoked without type
arguments, so cmd/go would previously fail with a type-checking error in
the test harness.

Avoid this by producing an error explaining that test functions cannot
have type parameters.

Fixes #48953

Change-Id: I4e39c9b7a06c964fad5f12d74d262aa090c25d79
Reviewed-on: https://go-review.googlesource.com/c/go/+/355889
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Robert Findley 2021-10-14 11:34:00 -04:00
parent 0fd0639e4c
commit 4cbc0a5554
2 changed files with 22 additions and 2 deletions

View File

@ -678,10 +678,16 @@ func (t *testFuncs) load(filename, pkg string, doImport, seen *bool) error {
}
func checkTestFunc(fn *ast.FuncDecl, arg string) error {
var why string
if !isTestFunc(fn, arg) {
name := fn.Name.String()
why = fmt.Sprintf("must be: func %s(%s *testing.%s)", fn.Name.String(), strings.ToLower(arg), arg)
}
if fn.Type.TypeParams.NumFields() > 0 {
why = "test functions cannot have type parameters"
}
if why != "" {
pos := testFileSet.Position(fn.Pos())
return fmt.Errorf("%s: wrong signature for %s, must be: func %s(%s *testing.%s)", pos, name, name, strings.ToLower(arg), arg)
return fmt.Errorf("%s: wrong signature for %s, %s", pos, fn.Name.String(), why)
}
return nil
}

View File

@ -44,6 +44,10 @@ stdout 'testdep_b <nil>'
stdout 'nameerr\.test "[^"]*wrong signature for TestBad'
! stderr 'wrong signature for TestBad'
# go list prints a useful error for generic test functions
! go list -test -deps genericerr
stderr 'wrong signature for TestGeneric, test functions cannot have type parameters'
# go list prints partial information with error if test has cyclic import
! go list -test -deps cycleerr
stdout cycleerr
@ -106,6 +110,16 @@ import (
func TestBad(t *testing.B) {}
-- genericerr/genericerr.go --
package genericerr
-- genericerr/genericerr_test.go --
package genericerr
import "testing"
func TestGeneric[T any](t *testing.T) {}
-- cycleerr/cycleerr_test.go --
package cycleerr