Skip to content

spec: function type inference ignores type parameter constraints #50272

@xaoctech

Description

@xaoctech

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

$ go version
go version devel go1.18-87b2a548 Sun Dec 19 20:16:45 2021 +0000 linux/amd64

Does this issue reproduce with the latest release?

No, generics are required

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vlad/.cache/go-build"
GOENV="/home/vlad/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/vlad/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/vlad/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/vlad/sdk/gotip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/vlad/sdk/gotip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.18-87b2a548 Sun Dec 19 20:16:45 2021 +0000"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3350654881=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Compile this code:

func a[T any, S []T](s S) int {
	return b(s)
}

func b[T any](s []T) int {
	return len(s)
}

func main() {
	a(make([]int, 0))
}

What did you expect to see?

The code should compile successfully

What did you see instead?

Compilation failed with type S of s does not match []T (cannot infer T) error message

This code compiles fine:

func a[T any, S []T](s S) int {
	return b([]T(s))
}

func b[T any](s []T) int {
	return len(s)
}

func main() {
	a(make([]int, 0))
}

Metadata

Metadata

Labels

FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.TypeInferenceIssue is related to generic type inferencegenericsIssue is related to generics

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions