Skip to content

cmd/compile: generics cannot infer slice/array of slice/array types #68262

@karalabe

Description

@karalabe

Go version

go version go1.22.4 darwin/arm64

Output of go env in your module/workspace:

Doesn't matter.

What did you do?

Go's generics supports defining constraints on array sizes:

type commonBinaryLengths interface {
	// footgun | address | hash | pubkey
	~[]byte | ~[20]byte | ~[32]byte | [48]byte
}

However, creating constraints out of those in another dimension fails to infer:

type commonArrayLengths[U commonBinaryLengths] interface {
	// footgun | roots
	~[]U | ~[8192]U
}

Explicitly spoon feeding the compiler with the types will succeed in building the code, so it seems it's not a misuse of generics, simply a bug in the compiler's inference system:

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

What did you see happen?

The inferred generic calls fail to compile whereas the spoon-fed ones build fine.

What did you expect to see?

I expect all lines to compile and run successfully.

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions