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: compiler error after translation #39692

Closed
zeebo opened this issue Jun 18, 2020 · 4 comments
Closed

cmd/go2go: compiler error after translation #39692

zeebo opened this issue Jun 18, 2020 · 4 comments

Comments

@zeebo
Copy link
Contributor

zeebo commented Jun 18, 2020

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

go2go playground

What did you do?

https://go2goplay.golang.org/p/LDEr36y6IUr

Source
package main

type Func(type A, B) interface {
	Run(A) B
}

//////

type Pair(type A, B) struct {
	L A
	R B
}

//////

type Compose(type A, B, C) struct {
	L Func(A, B)
	R Func(B, C)
}

func (p Compose(A, B, C)) Run(in A) C {
	return p.R.Run(p.L.Run(in))
}

//////

type Dup(type A) struct{}

func (d Dup(A)) Run(in A) Pair(A, A) {
	return Pair(A, A){
		L: in,
		R: in,
	}
}

//////

func main() {
	_ = Compose(
		int,
		Pair(int, int),
		Pair(Pair(int, int), Pair(int, int)),
	){
		L: Dup(int){},
		R: Dup(Pair(int, int)){},
	}
}

What did you expect to see?

No compiler error and no output

What did you see instead?

A large error message

# play
./prog.go2:45: cannot use instantiate୦୦Dup୦main୮aPair୮8int୮3୮0int୮9 literal (type instantiate୦୦Dup୦main୮aPair୮8int୮3୮0int୮9) as type instantiate୦୦Func୦main୮aPair୮8int୮3୮0int୮9୦main୮aPair୮8main୮aPair୮8int୮3୮0int୮9୮3୮0main୮aPair୮8int୮3୮0int୮9୮9 in field value:
	instantiate୦୦Dup୦main୮aPair୮8int୮3୮0int୮9 does not implement instantiate୦୦Func୦main୮aPair୮8int୮3୮0int୮9୦main୮aPair୮8main୮aPair୮8int୮3୮0int୮9୮3୮0main୮aPair୮8int୮3୮0int୮9୮9 (wrong type for Run method)
		have Run(instantiate୦୦Pair୦int୦int) instantiate୦୦Pair୦main୮aPair୮8int୮3୮0int୮9୦main୮aPair୮8int୮3୮0int୮9
		want Run(instantiate୦୦Pair୦int୦int) instantiate୦୦Pair୦struct୮4L୮0int୮2୮0R୮0int୮5୦struct୮4L୮0int୮2୮0R୮0int୮5

This seemed more important because it happens after translation, which possibly indicates a soundness hole in the type checker? Though, I do believe the program is well typed, so maybe just a bug in the translation.

@bcmills
Copy link
Contributor

bcmills commented Jun 18, 2020

Looks like the translator somehow substituted the underlying type for the defined type.

@ianlancetaylor ianlancetaylor self-assigned this Jun 18, 2020
@ianlancetaylor
Copy link
Contributor

This kind of thing is a bug in the translation tool. Basically, the code passed the type checker, but the generated Go-without-generics code is somehow using the wrong type.

Although I've been working on similar bug reports, and this code now fails in a completely different way. But it's still a bug in the translation tool.

@ianlancetaylor
Copy link
Contributor

This should be fixed now in the dev.go2go branch.

Thanks for the test case.

@gopherbot
Copy link

Change https://golang.org/cl/238797 mentions this issue: [dev.go2go] go/go2go: add type arguments to type instantiation

gopherbot pushed a commit that referenced this issue Jun 18, 2020
Previously we instantiated the underlying type, so we didn't need this.
We stopped doing that to get the same type in the generated code,
but now we need to get the right type arguments, rather than just
leaving the type parameters there.

Fixes #39692

Change-Id: I56ba8ff017c37219e7cb9bb6e07a4beb8c50e9dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/238797
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Jun 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants