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

Open
dfinkel opened this issue Jun 21, 2020 · 5 comments
Assignees
Milestone

Comments

@dfinkel
Copy link
Contributor

@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)
}

https://go2goplay.golang.org/p/1gqiYuDELuI (trimmed)
https://go2goplay.golang.org/p/qcdfl0tuHlb (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)

@tdakkota
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

https://go2goplay.golang.org/p/zpEMFBQSep-
works correctly.

@ianlancetaylor
Copy link
Contributor

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

@griesemer
Copy link
Contributor

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

@OneOfOne

This comment was marked as off-topic.

@ianlancetaylor
Copy link
Contributor

@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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.