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

spec: is x[i,] legal? #55007

Open
adonovan opened this issue Sep 11, 2022 · 1 comment
Open

spec: is x[i,] legal? #55007

adonovan opened this issue Sep 11, 2022 · 1 comment
Assignees
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Sep 11, 2022

The spec does not seem to permit a comma after the index in an expression such as a[i,], yet both the compiler and the go/parser package permit it, presumably because without type information it's impossible to distinguish an array index expression a[i,] from a parameterized type such as M[K,V,] where a comma is permitted after the list of instantiating types. The spec should probably be updated to say that a comma is allowed but ignored. The alternative, of having the type checker allow the comma in a type but reject it in a term, would require recording the presence of the comma in the syntax tree, which seems undesirable.

https://go.dev/play/p/JcES0elTHiG

@griesemer griesemer added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Sep 11, 2022
@griesemer griesemer added this to the Go1.20 milestone Sep 11, 2022
@griesemer
Copy link
Contributor

griesemer commented Sep 11, 2022

Your analysis is correct. Array/slice index expressions do not permit a trailing comma, but instantiations do because they follow the syntax of (type) parameter lists, which generally allow a trailing comma.

The other approach is to consider an expression of the form x[t,] (with a trailing comma) always as instantiation, i.e., "foo"[0,] would be a type error ("foo" is not a generic type, 0 is not a type, etc.).

Related: https://twitter.com/robertgriesemer/status/1283142956153573376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

2 participants