golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.7k stars 17.5k forks source link

cmd/go: `wrong signature` error when using a type alias for `*testing.T` #62454

Open ghost opened 1 year ago

ghost commented 1 year ago

Seems you can't alias testing.T, it confuses go test. Confuses Go Playground too.

go version go1.18.1 linux/amd64

What did you do?

https://go.dev/play/p/21W7rITsLUb

What did you expect to see?

Test results

What did you see instead?

wrong signature for TestLastIndex, must be: func TestLastIndex(t *testing.T)

cherrymui commented 1 year ago

Do you have a specific reason for aliasing testing.T? Thanks.

cc @bcmills

ghost commented 1 year ago

Do you have a specific reason for aliasing testing.T? Thanks.

Not really, no. I alias struct pointer types so it becomes impossible to forget the and makes refactoring easier later. But apparently testing.T is special.

wangxuw commented 12 months ago

hi, I reproduced this error using the provided test code snippet. This error is returned from https://github.com/golang/go/blob/master/src/cmd/go/internal/load/test.go#L770. And it comes from a type assertion, https://github.com/golang/go/blob/master/src/cmd/go/internal/load/test.go#L575 where go command checks whether the first param holds ast.StarExpr. When aliasing testing.T, the underlying type for fn.Type.Params.List[0].Type would be `ast.Ident`.