golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.3k stars 17.7k forks source link

cmd/go2go: compiler error after translation #39692

Closed zeebo closed 4 years ago

zeebo commented 4 years ago

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 commented 4 years ago

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

ianlancetaylor commented 4 years ago

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 commented 4 years ago

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

Thanks for the test case.

gopherbot commented 4 years ago

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