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: generic type inference not working on return types only in a certain case #53089

Closed
Qrnbth opened this issue May 26, 2022 · 3 comments

Comments

@Qrnbth
Copy link

Qrnbth commented May 26, 2022

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

$ go version
go version go1.18.1 windows/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
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Lich's Laptop\AppData\Local\go-build
set GOENV=C:\Users\Lich's Laptop\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Lich's Laptop\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Lich's Laptop\go
set GOPRIVATE=
set GOPROXY=https://goproxy.io
set GOROOT=D:\Installed Software\- Dev -\Go 1.18.1
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=D:\Installed Software\- Dev -\Go 1.18.1\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18.1
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\LICH'S~1\AppData\Local\Temp\go-build2554615698=/tmp/go-build -gno-record-gcc-switches

What did you do?

I'm trying to define a function that returns a generic type value,but without any parameter defined as the generic.
codes are something like below:

package main
import "encoding/json"
type ObjectConstraint interface {
	Object | Object2
	Method()
}

func DoSomething[ObjectWithConstraint ObjectConstraint]() ObjectWithConstraint {
	objectJson := "{\"content\":\"This Would Cause Compiling Error\" +}"
	var object ObjectWithConstraint
	json.Unmarshal([]byte(objectJson), object)
	return object
}

type Object struct {
	content string
}

// Just for compiling,irrelevant
func (Object) Method() {}

//  Caused Compiling Error: cannot infer ObjectWithConstraint 
func DoSomething_Wrapped() Object {
	return DoSomething()
}

type Object2 struct {
	content string
}

// Just for compiling,irrelevant
func (Object2) Method() {}

// Same stuff with type Object2
func DoSomething_Wrapped() Object2 {
	return DoSomething()
}

func main() {
	object := DoSomething()
	object.Method()
}

It is able to compile with single type,since the compiler seems not gonna confuse with the type value that returned,
but with mutiple types defined in the generic constraint,things just gonna change.

As some sort of a fixture, add a dummy object in the parameter,it could just work:

func DoSomething[ObjectWithConstraint ObjectConstraint](dummyObject ObjectWithConstraint) ObjectWithConstraint{
// dummy Object unused....
}

func DoSomething_Wrapped() Object {
	dummy := Object{content: ""}
	return DoSomething( dummy )
}

// main
func main() {
	dummyObject := Object{content: ""}
	object := DoSomething(dummyObject)
	object.Method()
}

What did you expect to see?

it could just compile with the original code without that dummy,with some more accurate type inference

What did you see instead?

Compilation Error

@seankhliao
Copy link
Member

seankhliao commented May 26, 2022

Duplicate of #51928

@seankhliao seankhliao marked this as a duplicate of #51928 May 26, 2022
@go101
Copy link

go101 commented May 29, 2022

This is not a dup of other issues. The OP's code is not complete.

package main
import "encoding/json"
type ObjectConstraint interface {
	Object | Object2
	Method()
}

func DoSomething[ObjectWithConstraint ObjectConstraint]() ObjectWithConstraint {
	objectJson := "{\"content\":\"This Would Cause Compiling Error\" +}"
	var object ObjectWithConstraint
	json.Unmarshal([]byte(objectJson), object)
	return object
}

type Object struct {
	content string
}

// Just for compiling,irrelevant
func (Object) Method() {}

//  Caused Compiling Error: cannot infer ObjectWithConstraint 
func DoSomething_Wrapped() Object {
	return DoSomething[Object]()
}

type Object2 struct {
	content string
}

// Just for compiling,irrelevant
func (Object2) Method() {}

// Same stuff with type Object2
func DoSomething_Wrapped2() Object2 {
	return DoSomething[Object2]()
}

func main() {
	object := DoSomething[Object]()
	object.Method()
}

@seankhliao
Copy link
Member

seankhliao commented May 29, 2022

looks the same to me, it's asking for type inference based on the context in which a function occurs.

func DoSomething_Wrapped() Object {
	return DoSomething()
}

is the same as asking for

var _ Object = DoSomething() // infer type for DoSomething

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants