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: document that x[i,] (trailing comma) is permitted #55007

Closed
adonovan opened this issue Sep 11, 2022 · 3 comments
Closed

spec: document that x[i,] (trailing comma) is permitted #55007

adonovan opened this issue Sep 11, 2022 · 3 comments
Assignees
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
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

@griesemer
Copy link
Contributor

@ianlancetaylor agrees that we should not report a syntax error during type checking. I will update the spec accordingly. Notably, gofmt will remove any such trailing commas.

@griesemer griesemer added Documentation and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Nov 15, 2022
@griesemer griesemer changed the title spec: is x[i,] legal? spec: document that x[i,] (trailing comma) is permitted Nov 15, 2022
@gopherbot
Copy link

Change https://go.dev/cl/452618 mentions this issue: spec: document that trailing comma is valid after index in index expressions

@dmitshur dmitshur added the NeedsFix The path to resolution is known, but the work has not been done. label Nov 23, 2022
@golang golang locked and limited conversation to collaborators Nov 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

4 participants