[generics] another parsing ambiguity

183 views
Skip to first unread message

Ben Kraft

unread,
Jun 16, 2020, 7:50:03 PM6/16/20
to golang-nuts
Consider the following code (playground):

type R(type T) struct{}
func F(type T)() {
     _ = func() R(T) { return R(T){} }
}

That is, we have some parameterized type R(T), and a function literal which returns that type R(T). (This is simplified from an example I found while writing generic composition with a result type.)

This currently fails with the following error message: prog.go:5:23: expected operand, found 'return' (the referenced location is indeed the return). (As an aside, this error message was pretty opaque -- not sure if improving that sort of thing is in-scope for the prototype.)

The issue appears to be a parsing ambiguity similar to the one described in the design draft, where func() R(T) has been interpreted as (func() R)(T). But the solution described there doesn't work: writing func() (R(T)) gets parsed as (and gofmted to) func() (R T). That is, it runs into another parsing ambiguity described in the draft. As a further workaround, I eventually realized to use func() (_ R(T)); from there things work fine.

Presumably the solution to the second parsing ambiguity could also work here, if applied to function returns; of course that expands the compatibility break described there.

Not sure if this qualifies as discussion or a bug report; I figured I'd start here but happy to make an issue if that's preferred.

Ian Lance Taylor

unread,
Jun 16, 2020, 9:06:26 PM6/16/20
to Ben Kraft, golang-nuts, Robert Griesemer
Interesting case. At the very least we need to describe this case in
the design draft. Maybe there is a way that we can fix it.

Please do open a bug report for this. Thanks.

Ian

Gert

unread,
Jun 17, 2020, 11:15:39 AM6/17/20
to golang-nuts
At Ben did you made a github issue yet for it? Can't seem to find it https://github.com/golang/go/issues?q=is%3Aissue+cmd%2Fgo2go Thanks

benjami...@gmail.com

unread,
Jun 17, 2020, 12:08:58 PM6/17/20
to golang-nuts
I've now created an issue here: https://github.com/golang/go/issues/39654.

Gert

unread,
Jun 17, 2020, 12:52:51 PM6/17/20
to golang-nuts
Thanks, can you maybe at the complete working code snippet in your issue too

```

type R(type T) struct{}
func F(type T)() {
_ = func() (_ R(T)) { return R(T){} }
}
```

I think it's not trivial code even after reading the issue what your solution was to the problem.
Reply all
Reply to author
Forward
0 new messages