Skip to content
This repository has been archived by the owner on Sep 20, 2022. It is now read-only.

Polymorphic recursion generates Go that won't compile #27

Open
zenhack opened this issue Nov 3, 2018 · 1 comment
Open

Polymorphic recursion generates Go that won't compile #27

zenhack opened this issue Nov 3, 2018 · 1 comment
Labels
bug Something isn't working

Comments

@zenhack
Copy link

zenhack commented Nov 3, 2018

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.

@albrow albrow added the bug Something isn't working label Nov 9, 2018
@albrow
Copy link
Owner

albrow commented Nov 9, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants