Skip to content

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

Closed
@vyskocilm

Description

@vyskocilm

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) {
    panic("*(vector[int]).PushInt")
}

func main() {
    vs := vector[string]{}
    vs.PushInt("42")
}

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 https://go.googlesource.com/proposal/+/refs/heads/master/design/43651-type-parameters.md

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]:
main.(*vector[...]).PushInt(...)
        /tmp/main.go:7
main.main()
        /tmp/main.go:12 +0x27
exit status 2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions