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: type inference fails for unused type in generic type alias #70948

Closed
DmitriyMV opened this issue Dec 21, 2024 · 4 comments
Closed
Assignees
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. 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

Comments

@DmitriyMV
Copy link
Contributor

DmitriyMV commented Dec 21, 2024

Go version

go version devel go1.24-669d87a Thu Dec 19 19:19:59 2024 -0800 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/dmitry/Library/Caches/go-build'
GODEBUG=''
GOENV='/Users/dmitry/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/59/2slgfdl91td3z8w75b7zh6kw0000gn/T/go-build2195658086=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/dmitry/alias_example/go.mod'
GOMODCACHE='/Users/dmitry/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/dmitry/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/dmitry/sdk/gotip'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/dmitry/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/Users/dmitry/sdk/gotip/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-669d87a Thu Dec 19 19:19:59 2024 -0800'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

$ > cat main.go
package main

type A[V any] = struct{}

func Foo[V any](_ A[V]) {}

func main() {
	Foo(A[int]{})
}

$ > gotip version
go version devel go1.24-669d87a Thu Dec 19 19:19:59 2024 -0800 darwin/arm64

$ > gotip run main.go
# command-line-arguments
./main.go:8:5: in call to Foo, cannot infer V (declared at ./main.go:5:10)

What did you see happen?

Compilation failure.

What did you expect to see?

Successful compilation, even tho type A[V any] = struct{} doesn't make much sense.

Notes:

@seankhliao seankhliao changed the title cmd/go: cannot infer V for generic alias to an empty struct cmd/go: type inference fails for unused type in generic type alias Dec 21, 2024
@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 21, 2024
@dr2chase
Copy link
Contributor

@griesemer @timothy-king

@ianlancetaylor ianlancetaylor changed the title cmd/go: type inference fails for unused type in generic type alias cmd/compile: type inference fails for unused type in generic type alias Dec 21, 2024
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Dec 21, 2024
@ianlancetaylor
Copy link
Member

CC @griesemer

@griesemer griesemer self-assigned this Jan 6, 2025
@griesemer griesemer added the TypeInference Issue is related to generic type inference label Jan 6, 2025
@griesemer
Copy link
Contributor

This may not be working as expected, but it is working as intended:

The type parameter V of A is not used, meaning no matter what type is used for V, the actual type of A is always just struct{} and there's no way for F to infer the type argument's type. More precisely, any type F would infer would be correct. For instance (playground):

type A[V any] = struct{}

func f[V any](A[V]) {}

func main() {
	f[string](A[int]{})
}

This code works even though the explicit type argument to F is string, which is different from int provided to A. It works because A[int] and A[string] are identical types.

Generally, type inference must fail in cases where there is more than type possible because it cannot prove that whatever type it selected is the right one. Only the programmer can answer that question.

As soon as V is used, inference does what one would expect (playground):

type A[V any] = struct{ _ V }

func f[V any](A[V]) {}

func main() {
	f(A[int]{})
}

compiles and runs as expected.

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. 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

6 participants