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 parameter unexpectedly does not implement constraint #56716

Closed
silviucm opened this issue Nov 12, 2022 · 2 comments
Closed
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@silviucm
Copy link

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

$ go version
go version go1.19.3 linux/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOVERSION="go1.19.3"
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"

What did you do?

Run the issue reported at
https://groups.google.com/g/golang-nuts/c/_-vNSfDmPG8?pli=1
both on the playground
https://go.dev/play/p/GS_x9Y8HOMK
as well as locally with go run [temp go file]

@ianlancetaylor suggested in that Google group thread that there is merit to record this as a bug, to be further looked at.
It felt interesting so I recorded it, despite not being the original submitter from the group (apologies if duplicate).

What did you expect to see?

Potentially the program successfully build

What did you see instead?

command-line-arguments
./test.go:22:41: []T does not implement Scalar|Vector ([]T missing in int | float32 | float64 | []int | []float32 | []float64)
./test.go:24:19: []T does not implement Scalar|Vector ([]T missing in int | float32 | float64 | []int | []float32 | []float64)

@silviucm silviucm changed the title affected/package: affected/package: generics Nov 12, 2022
@ianlancetaylor ianlancetaylor changed the title affected/package: generics cmd/compile: type parameter unexpectedly does not implement constraint Nov 12, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Nov 12, 2022
@ianlancetaylor
Copy link
Contributor

CC @griesemer @findleyr

@ianlancetaylor ianlancetaylor added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. generics Issue is related to generics labels Nov 12, 2022
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Nov 12, 2022
@griesemer
Copy link
Contributor

This is working as expected.

Here's a simpler case reporting the same error:

type Container[T []int] struct {
	Val T
}

func Create1[T []int](val T) Container[T] {
	return Container[T]{}
}

func Create2[T int](val T) Container[[]T] { // ERROR
	return Container[[]T]{} // ERROR
}

Create2 uses the type []T to instantiate a type parameter constrained by []int. Note that []T is not []int (even though T is constrained to be just int). When testing constraint satisfaction (which is interface implementation), we simply need to show that the type argument []T (a single type) is an element of the type set of interface{[]int}, which it is not. Hence the instantiation fails. (Note that this works.)

@golang golang locked and limited conversation to collaborators Nov 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge generics Issue is related to generics NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants