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: infer generic type based on the function return type #54791

Closed
handsomefox opened this issue Aug 31, 2022 · 1 comment
Closed

cmd/compile: infer generic type based on the function return type #54791

handsomefox opened this issue Aug 31, 2022 · 1 comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime.

Comments

@handsomefox
Copy link

handsomefox commented Aug 31, 2022

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

$ go version
go version go1.19 windows/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\hands\AppData\Local\go-build
set GOENV=C:\Users\hands\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\hands\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\hands\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\hands\AppData\Local\Temp\go-build3627023353=/tmp/go-build -gno-record-gcc-switches

What did you do?

Wrote a generic struct and created a method that returns an empty instance of it.

type Example[T any] struct{}

func Function() Example[int] {
	return Example{}
}

What did you expect to see?

I expected the above code to infer generic type of Example based on the return type of the function and work.

What did you see instead?

The code does not compile, with an error message: cannot use generic type Example[T any] without instantiation.

You have to explicitly change the function to look like:

func Function() Example[int] {
	return Example[int]{}
}

to make it work.

@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Aug 31, 2022
@seankhliao
Copy link
Member

seankhliao commented Aug 31, 2022

Duplicate of #51928

@seankhliao seankhliao marked this as a duplicate of #51928 Aug 31, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2022
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