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

go/printer: produces a invalid source, for a successfully parsed AST #69320

Open
mateusz834 opened this issue Sep 6, 2024 · 4 comments
Open
Assignees
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@mateusz834
Copy link
Member

Consider:

type _[a (b + 3),] struct{}

This is parsed successfully by go/parser and it gets formatted into:

type _[a b + 3] struct{}

This is not valid go syntax. Not sure how this should be handled, should type _[a (b + 3),] struct{} cause a parser error?

For example, the source below causes a parser error (expected ')', found '+')

type _[a ~int, b (a+3)]struct{}

CC @griesemer

@griesemer griesemer added this to the Go1.24 milestone Sep 6, 2024
@griesemer griesemer added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 6, 2024
@griesemer griesemer self-assigned this Sep 6, 2024
@griesemer
Copy link
Contributor

Milestone 1.24 only so it stays on the radar - this is not really urgent in any way as this code won't pass the typecheckers and thus won't appear in real code.

@griesemer griesemer changed the title go/printer: produces a invalid source, for a sucesfully parsed AST go/printer: produces a invalid source, for a successfully parsed AST Sep 6, 2024
@griesemer
Copy link
Contributor

Looking at this some more, type _[a (b + 3),] struct{} should probably be a parser error, but there's two ways to approach it: since (b + 3) cannot be a type, the entire parameter list is considered an array length, and the , is unexpected. Alternatively, the comma implies a type parameter list, but (b + 3) cannot be a type so there's an error there.

I believe the spec's current wording permits both approaches.

@adonovan
Copy link
Member

adonovan commented Sep 6, 2024

Looks like the comma causes it to be parsed as a type parameter list, even though the parenexpr holds a term not a type:

Screenshot 2024-09-06 at 5 54 52 PM

@griesemer
Copy link
Contributor

Yes, the comma forces parsing as a type parameter list which is expected in the current implementation. The question is what is the most consistent error to report: is it an invalid type constraint or an invalid array length.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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