Skip to content

cmd/compile: generic type inference not working on return types only in a certain case #53089

Closed
@Qrnbth

Description

@Qrnbth

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

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