Skip to content

cmd/compile: type parameter unexpectedly does not implement constraint #56716

Closed

Description

@silviucm

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)

Activity

  1. changed the title [-]affected/package: [/-] [+]affected/package: generics[/+] on Nov 12, 2022
  2. changed the title [-]affected/package: generics[/-] [+]cmd/compile: type parameter unexpectedly does not implement constraint[/+] on Nov 12, 2022
  3. ianlancetaylor commented on Nov 12, 2022

    @ianlancetaylor
    Contributor
  4. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    genericsIssue is related to generics
    on Nov 12, 2022
  5. added this to the Backlog milestone on Nov 12, 2022
  6. griesemer commented on Nov 14, 2022

    @griesemer
    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.)

  7. locked and limited conversation to collaborators on Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.compiler/runtimeIssues related to the Go compiler and/or runtime.genericsIssue is related to generics

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions