Skip to content

cmd/compile: specific type for generic struct type in recevier #62030

Closed
@septemhill

Description

@septemhill

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

$ go version
go version go1.21.0 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/Users/septemlee/Library/Caches/go-build'
GOENV='/Users/septemlee/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/septemlee/.gvm/pkgsets/go1.21.0/global/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/septemlee/.gvm/pkgsets/go1.21.0/global'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/septemlee/.gvm/gos/go1.21.0'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/septemlee/.gvm/gos/go1.21.0/pkg/tool/darwin_amd64'
GOVCS=''
GOVERSION='go1.21.0'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/1t/2fzzpms929v28gr7b0mdqcrr0000gn/T/go-build1561058215=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

First case: https://go.dev/play/p/B1k27y3z0OQ

type Base[T any] struct {
	Custom T
}

func (b *Base[T]) Show() {
	fmt.Println("Hi, Septem")
}

func (b *Base[int]) Call() {
	fmt.Println("Hi, Septem??")
}

func main() {
	f := &Base[float64]{}

	// I don't expect it could be compiled. The type of `f` is `*Base[float64]` rather than `*Base[int]`
	f.Call() 
}

Second case: https://go.dev/play/p/IHQyecfPsL3

type Base[T any] struct {
	Custom T
}

func (b *Base[T]) Show() {
	fmt.Println("Hi, Septem")
}

func (b *Base[int]) ReturnInt() int {
	return 123
}

func main() {
	f := &Base[int]{}

	// I expected it could be compiled.
	f.ReturnInt()
}

What did you expect to see?

First case shouldn't be compiled, and the second case could be compiled.

What did you see instead?

First case passed.
Second one failed, and show the following message: cannot use 123 (untyped int constant) as int /* with int declared at ./main.go:65:15 */ value in return statement

I'm not pretty sure do we allow this syntax (e.g. Base[int] as receiver in the case) or not.
But if we don't, both of these cases should failed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions