Skip to content

go/types, types2: generic type inference - generic interface input parameter of generic function #62612

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

Closed
xymon2 opened this issue Sep 13, 2023 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Milestone

Comments

@xymon2
Copy link

xymon2 commented Sep 13, 2023

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

$ go version
go version go1.21.1 darwin/arm64

Does this issue reproduce with the latest release?

NO

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

go env Output
$ go env
GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/godot/Library/Caches/go-build'
GOENV='/Users/godot/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/godot/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/godot/go'
GOPRIVATE='github.com/channel-io/*'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.1'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/godot/Desktop/WORK/DEV/chx-backend/go.mod'
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 arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/jx/_3jk95_s41g4q3q50mhll7th0000gn/T/go-build1234621002=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

I tried to compile my project (that used to compile successfully with Go 1.20) but I got an error.

I made a function getting a parameter with generic interface type.

// BoilerModel is generated by sqlboiler - v4.15
func TestFunction[D sqlrepo.DomainModel, B sqlrepo.BoilerModel](
	repo sqlrepo.SQLRepo[D, B],
) {}

Then I called that function like this.

// models.Test is generated by sqlboiler - v4.15
type testRepo struct {
	sqlrepo.SQLRepo[*model.Test, *models.Test]
}

func (repo *testRepo) ErrorFunction() {
	TestFunction(repo.SQLRepo)
}

Result of build is

 cannot infer B 

But go 1.20 builds that code well.
And why only B cannot be inferred?

https://go.dev/play/p/VxOg1KSvJdz
This is playgroud to reproduce that error but it raises error "cannot infer D" not B.

What did you expect to see?

no error as it is with go 1.20

What did you see instead?

 cannot infer B 
@xymon2 xymon2 changed the title generic type inference - generic interface as input parameter of generic function generic type inference - generic interface input parameter of generic function Sep 13, 2023
@xymon2 xymon2 changed the title generic type inference - generic interface input parameter of generic function o/types, types2: generic type inference - generic interface input parameter of generic function Sep 13, 2023
@xymon2 xymon2 changed the title o/types, types2: generic type inference - generic interface input parameter of generic function go/types, types2: generic type inference - generic interface input parameter of generic function Sep 13, 2023
@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference labels Sep 13, 2023
@ianlancetaylor ianlancetaylor added this to the Go1.22 milestone Sep 13, 2023
@ianlancetaylor
Copy link
Contributor

CC @griesemer

@ianlancetaylor
Copy link
Contributor

@xymon2 can you provide a complete, stand-alone example? Thanks.

@ianlancetaylor
Copy link
Contributor

Oh sorry, just saw the Go playground link. My apologies.

@ianlancetaylor
Copy link
Contributor

This is a case where the interface does not use its type parameters.

type SQLRepo[D DomainModel, B BoilerModel] interface {
	Repo()
}

It's hard to infer a type that isn't used, as really we can infer any type at all. The code does build with go build -gcflags=-lang=go1.20. It should also build if you put go 1.20 in your go.mod file. For 1.21 and later you need to change your code. Sorry.

Closing this issue as a dup of #60377 and #61903. Thanks for reporting it.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 13, 2023
@golang golang locked and limited conversation to collaborators Sep 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. TypeInference Issue is related to generic type inference
Projects
None yet
Development

No branches or pull requests

3 participants