Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: generics definition with primitive type leads to weird behavior #48813

vyskocilm opened this issue Oct 6, 2021 · 2 comments


Copy link

@vyskocilm vyskocilm commented Oct 6, 2021

What version of Go are you using (go version)?

$ go version
go version devel go1.18-7983830 Tue Oct 5 14:12:44 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

Yes, on latest master.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

package main

type vector[T any] []T

func (iv *vector[int]) PushInt(value int) {

func main() {
    vs := vector[string]{}

What did you expect to see?

I have expected that a primitive type like int won't be allowed as a type parameter. If it's not a compiler error, then PushInt method shall not available for any other instance than vector[int]. I did not find it described in the design document

What did you see instead?

It sounds that [int] is treated as [T any], so PushInt is called for vector[string] too.

panic: *(vector[int]).PushInt

goroutine 1 [running]:
        /tmp/main.go:12 +0x27
exit status 2
Copy link

@tdakkota tdakkota commented Oct 6, 2021


Copy link

@vyskocilm vyskocilm commented Oct 6, 2021

@tdakkota thanks! This is not obvious - hope that there will be some linter for it.

To summarize it

func foo[T]() { }  // illegal without constraint
func (s someStruct[T]) foo() {} // legal, because constraint is defined on a struct level


@vyskocilm vyskocilm closed this Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants