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: another parsing ambiguity #39654

Closed
benjaminjkraft opened this issue Jun 17, 2020 · 3 comments
Closed

cmd/go2go: another parsing ambiguity #39654

benjaminjkraft opened this issue Jun 17, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@benjaminjkraft
Copy link

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. Or, this could be documented similar to [](R(T)).

See also golang-nuts thread.

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 17, 2020
@andybons andybons added this to the Unplanned milestone Jun 17, 2020
@andybons
Copy link
Member

@griesemer @ianlancetaylor

@benjaminjkraft
Copy link
Author

To be clear, the workaround in full looks like:

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

@griesemer
Copy link
Contributor

There's only one issue here, which is the ambiguity of instantiated (result) parameter types. As you say, this works.

In short, this is working as expected at the moment. I am going to close this as not a bug; but we are looking into changing the parsing here. This is obviously a very common issue for lots of people.

@golang golang locked and limited conversation to collaborators Jun 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants