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/compile: cannot parse certain constraint literals in generic functions #49174

Closed
griesemer opened this issue Oct 27, 2021 · 4 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@griesemer
Copy link
Contributor

griesemer commented Oct 27, 2021

package p

func _[_ []int | int]() {}  // cannot parse: syntax error: unexpected |, expecting comma or ]
func _[_ int | []int]() {}  // ok

Both constraints are identical and both are valid. Should be able to parse both of them.

@griesemer griesemer added NeedsFix The path to resolution is known, but the work has not been done. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. release-blocker labels Oct 27, 2021
@griesemer griesemer added this to the Go1.18 milestone Oct 27, 2021
@griesemer griesemer self-assigned this Oct 27, 2021
@gopherbot gopherbot removed the NeedsFix The path to resolution is known, but the work has not been done. label Oct 27, 2021
@griesemer griesemer changed the title cmd/compile: cannot parse certain constraint literals cmd/compile: cannot parse certain constraint literals in generic functions Oct 27, 2021
@gopherbot
Copy link

Change https://golang.org/cl/359014 mentions this issue: cmd/compile/internal/syntax: fix constraint literal parsing for generic functions

@findleyr
Copy link
Contributor

This does not affect go/parser.

@griesemer
Copy link
Contributor Author

Fix is pending but this is also not a release blocker. There is a work-around:

func _[_ ([]int) | int]() {} // work-around: use parentheses

and this case is uncommon; most of the time we want

func _[_ ~[]int | ~int]() {}

@gopherbot
Copy link

Change https://golang.org/cl/359134 mentions this issue: go/parser: fix parsing of array or slice constraint types

gopherbot pushed a commit that referenced this issue Oct 28, 2021
Now that we allow eliding 'interface' from constraint types, we need to
be a bit more careful about not consuming a '[' when parsing the next
expression after "type T [". We want to check if the next expression is
an identifier not followed by ']', in which case we're in a generic
type, but need to avoid parsing index or slice expressions. Such
expressions aren't valid array lengths because these expressions are
never constant, so when encountering a following '[' we can instead
assume that this is a type parameter field with array or slice type
constraint.

Test cases are added for the related issues #49174 and #49175, along
with a flag to enable tracing error tests.

For #49174
For #49175

Change-Id: I0476ef20c4c134ac537118272f20caaf123ee70e
Reviewed-on: https://go-review.googlesource.com/c/go/+/359134
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
@golang golang locked and limited conversation to collaborators Jun 23, 2023
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

3 participants