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: specific type for generic struct type in recevier #62030

Closed
septemhill opened this issue Aug 15, 2023 · 1 comment
Closed

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

septemhill opened this issue Aug 15, 2023 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@septemhill
Copy link

septemhill commented Aug 15, 2023

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.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 15, 2023
@septemhill septemhill changed the title cmd/compile: specific type for generic struct type cmd/compile: specific type for generic struct type in recevier Aug 15, 2023
@uluyol
Copy link
Contributor

uluyol commented Aug 15, 2023

Duplicate of #47419

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.
Projects
None yet
Development

No branches or pull requests

3 participants