From 4cbc0a555474f59b8600e7e8ecba5c70d619ba0b Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Thu, 14 Oct 2021 11:34:00 -0400 Subject: [PATCH] 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 Run-TryBot: Bryan C. Mills Reviewed-by: Bryan C. Mills TryBot-Result: Go Bot --- src/cmd/go/internal/load/test.go | 10 ++++++++-- src/cmd/go/testdata/script/list_test_err.txt | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/cmd/go/internal/load/test.go b/src/cmd/go/internal/load/test.go index da6d1cb21d..4cefb62d51 100644 --- a/src/cmd/go/internal/load/test.go +++ b/src/cmd/go/internal/load/test.go @@ -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 } diff --git a/src/cmd/go/testdata/script/list_test_err.txt b/src/cmd/go/testdata/script/list_test_err.txt index c6f1ecf400..25dbb969b0 100644 --- a/src/cmd/go/testdata/script/list_test_err.txt +++ b/src/cmd/go/testdata/script/list_test_err.txt @@ -44,6 +44,10 @@ stdout 'testdep_b ' 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 -- 2.50.0