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/go2go: constraints on type arguments appear to be ignored when validating other constraint arguments on functions #39733

dfinkel opened this issue Jun 21, 2020 · 5 comments


Copy link

@dfinkel dfinkel commented Jun 21, 2020

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

Verified on the dev.go2go branch

$ go version
% bin/go version
go version devel +0a030888da Sat Jun 20 04:46:40 2020 +0000 linux/amd64

Does this issue reproduce with the latest release?

Not in a release as it's on the `dev.go2go branch.

Also present in the go2go playground (currently version devel +5f01333bf1 Wed Jun 17 04:20:54 2020 +0000.)

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

LInux AMD64, but also the go2go playground

What did you do?

package main

type MapSliceConstraint(type K comparable, V interface{}) interface {
	type map[K]V, []V

func genLen(type T MapSliceConstraint(K, V), K comparable, V interface{})(collection T) int {
	return len(collection)
} (trimmed) (with a main)

What did you expect to see?

Compiling and printing:

len map: 1
len slice: 2

What did you see instead?

% go tool go2go run fim.go2
type checking failed for main
/tmp/go2go-run155966463/fim.go2:11:39: K does not satisfy comparable

(same error on either of the go2go playground links above)

Copy link

@tdakkota tdakkota commented Jun 21, 2020

func genLen(type K comparable, V interface{}, T MapSliceConstraint(K, V))(collection T) int
instead of
func genLen(type T MapSliceConstraint(K, V), K comparable, V interface{})(collection T) int
works correctly.

Copy link

@ianlancetaylor ianlancetaylor commented Jun 22, 2020

This seems to be an error in the type checker.

At the very least the error message is wrong, as K is constrained to satisfy comparable.

Copy link

@griesemer griesemer commented Jun 22, 2020

It's a phase-ordering issue. We know that we have several of those and fixing them will require a fairly substantial rewrite of the core of the type checker which we didn't attempt yet since this is a prototype.

(Both go/types, but also the compiler, have some of these phase-ordering issues with type-checking, due to the "eager" approach of type-checking. For regular Go, these issues mostly appear for corner-case scenarios. Unfortunately, with generics, correct phase-ordering has become paramount.)


This comment was marked as off-topic.

Copy link

@ianlancetaylor ianlancetaylor commented Jun 25, 2020

@OneOfOne The test case in #39733 (comment) isn't valid: comparable permits == and !=, it doesn't permit >.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants