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: type parameter order is significant when it should not be #40789

Open
rogpeppe opened this issue Aug 14, 2020 · 2 comments
Open

cmd/go2go: type parameter order is significant when it should not be #40789

rogpeppe opened this issue Aug 14, 2020 · 2 comments
Assignees
Milestone

Comments

@rogpeppe
Copy link
Contributor

@rogpeppe rogpeppe commented Aug 14, 2020

commit 722af87

The following program fails with the error "K does not satisfy comparable", in the copyMap function definition when AFAICS K is declared correctly as having a comparable constraint.

package main

import "fmt"

func main() {
	m := map[string]int{
		"a": 6,
		"b": 7,
	}
	fmt.Println(copyMap[map[string]int, string, int](m))
}

type Map[K comparable, V any] interface {
	type map[K] V
}

func copyMap[M Map[K, V], K comparable, V any](m M) M {
	m1 := make(M)
	for k, v := range m {
		m1[k] = v
	}
	return m1
}```

If the type parameters are reordered to put `K` first, the example [works OK](https://go2goplay.golang.org/p/JwcN6NspdYu).
@rogpeppe rogpeppe changed the title cmd/go2go: cmd/go2go: type parameter order is significant when it should not be Aug 14, 2020
@ianlancetaylor ianlancetaylor added this to the Unreleased milestone Aug 14, 2020
@AndrewWPhillips
Copy link

@AndrewWPhillips AndrewWPhillips commented Sep 17, 2020

Here's something simpler to demonstrate the problem.

package main

type A[X comparable] interface {
	type []X
}

func f[B A[X], X comparable]() B {
	return nil
}

func main() {
}

@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 23, 2020

Thanks for this. Another case where the prototype type-checks too eagerly. This is a problem we need to solve in general in a real implementation (probably requires a two-phase approach). Addressing this should resolve a large number of bugs (not just with generics but elsewhere as well).

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
4 participants
You can’t perform that action at this time.