Skip to content

go/types, cmd/compile/internal/types2: type parameter inference fails for constraints that succeed individually #66751

Closed
@firelizzard18

Description

@firelizzard18

Go version

go version go1.22.1 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/firelizzard/.cache/go-build'
GOENV='/home/firelizzard/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/firelizzard/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/firelizzard/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/usr/lib/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.1'
GCCGO='/usr/bin/gccgo'
GOAMD64='v1'
AR='ar'
CC='x86_64-pc-linux-gnu-gcc'
CXX='x86_64-pc-linux-gnu-g++'
CGO_ENABLED='1'
GOMOD='/home/firelizzard/src/yaegi/go-script/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 -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3476193043=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/p/fVSkvTEgYx_1

package main

type ptrTo[A any] interface{ ~*A }
type hasFoo[A any] interface{ foo(A) }
type both[A, B any] interface {
	ptrTo[A]
	hasFoo[B]
}

type fooer[A any] struct{}

func (f *fooer[A]) foo(A) {}

func withPtr[A ptrTo[B], B any]()       {}
func withFoo[A hasFoo[B], B any]()      {}
func withBoth[A both[B, C], B, C any]() {}

func main() {
	withPtr[*fooer[int]]()  // Ok
	withFoo[*fooer[int]]()  // Ok
	withBoth[*fooer[int]]() // Error: cannot infer C
}

What did you see happen?

Inference succeeds on all three calls.

What did you expect to see?

Inference succeeds on the first two but fails on the third. If inference of B for withPtr or withFoo failed, it would make sense. But inference succeeds on both of those so it's surprising and confusing that it fails for withBoth.

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.TypeInferenceIssue is related to generic type inferencecompiler/runtimeIssues related to the Go compiler and/or runtime.

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions