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: clarify that short variable redeclaration cannot redeclare the same variable multiple times #45652
Comments
gccgo reports:
|
Agreed, the current spec wording seems to support go/types behavior. But if cmd/compile and gccgo already make it an error, I support codifying that to make the risk of accidental reuse less likely. Users can just as easily write Edit: I misunderstood the issue with go/types as that it accepted the code, but it still rejects it, just with a different error message. So all the more reason that we should document existing practice. |
Agree that the language should be clarified. I think it's at least implicit in the current wording that names should not be repeated, in that it is "shorthand for a regular variable declaration with initializer expressions but no types". Since names cannot be repeated in a normal variable declaration, they shouldn't be repeated in a short variable declaration, notwithstanding the allowance for redeclaration of names earlier in the block. |
I don't think that's directly stated though, no? I believe it's just a consequence of "No identifier may be declared twice in the same block," whereas a short variable declaration does not actually declare identifiers that have already been declared. -- Incidentally, it seems a bit confusing that https://golang.org/ref/spec#Short_variable_declarations defines:
because earlier https://golang.org/ref/spec#Declarations_and_scope said
That is, we have both a defined term "redeclare" that actually means not declaring something; but then we also use the term "redeclare" to mean "declare again in a different scope". |
Change https://golang.org/cl/312170 mentions this issue: |
- rewrite Checker.shortVarDecl core loop for clarity - match compiler error messages (#43087) - don't allow multiple identical redeclarations (#45652) For #43087. For #45652. Change-Id: I8c3329a553aa104d7853fbaea8b88049bc9b3b88 Reviewed-on: https://go-review.googlesource.com/c/go/+/312170 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
Change https://go.dev/cl/405757 mentions this issue: |
It is not obvious from the spec on short variable declarations that
should be invalid: the variable
b
is redeclared, but it is redeclared twice. The compiler reports an error butgo/types
andtypes2
do not. It makes sense to disallow it as it's likely an error (and the same effect can be achieved in different ways).Will fix the
go/types
andtypes2
issue as part of #43087.The text was updated successfully, but these errors were encountered: