fo icon indicating copy to clipboard operation
fo copied to clipboard

Polymorphic recursion generates Go that won't compile

Open zenhack opened this issue 7 years ago • 1 comments

Given main.fo:

package main

type Foo[T] struct{
	Field []Foo[[]T]
}

func main(){
}

The generated go is:

package main

type Foo____T struct {
	Field []Foo______T
}

func main() {
}

Which results in a compile error:

$ fo run main.fo 
# command-line-arguments
./main.go:4:10: undefined: Foo______T
ERROR: exit status 2

What's happening is that fo is not properly dealing with polymorphic recursion, i.e. when a polymorphic/generic type is defined which is recursive, and uses something other than the original type parameter in the recursive uses of the type. The Go 2 generics draft design mentions this case:

https://go.googlesource.com/proposal/+/master/design/go2draft-contracts.md#parameterized-types

Their solution is to explicitly disallow it, because it makes an implementation which just specializes everything impossible. I think this is the correct solution. fo should check for this and emit a comprehensible error, rather than just spitting out broken code.

zenhack avatar Nov 03 '18 13:11 zenhack

@zenhack thank you for opening this issue. I agree with your proposed solution to simply disallow this case. It should be easy enough to catch in the type checker.

albrow avatar Nov 09 '18 21:11 albrow