mirror of
https://github.com/golang/go
synced 2024-11-17 18:04:48 -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:
parent
0fd0639e4c
commit
4cbc0a5554
@ -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
|
||||
}
|
||||
|
14
src/cmd/go/testdata/script/list_test_err.txt
vendored
14
src/cmd/go/testdata/script/list_test_err.txt
vendored
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user