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: clarify that short variable redeclaration cannot redeclare the same variable multiple times #45652

Closed
griesemer opened this issue Apr 20, 2021 · 7 comments
Milestone

Comments

@griesemer
Copy link
Contributor

It is not obvious from the spec on short variable declarations that

package p

func _() {
	var b int
	a, b, b := 1, 2, 3
	_ = a
	_ = b
}

should be invalid: the variable b is redeclared, but it is redeclared twice. The compiler reports an error but go/types and types2 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 and types2 issue as part of #43087.

@griesemer griesemer added this to the Backlog milestone Apr 20, 2021
@griesemer griesemer self-assigned this Apr 20, 2021
@griesemer
Copy link
Contributor Author

cc: @findleyr
cc: @mdempsky

@ianlancetaylor
Copy link
Contributor

gccgo reports:

foo.go:5:15: error: ‘b’ repeated on left side of :=
    5 |         a, b, b := 1, 2, 3
      |               ^

@mdempsky
Copy link
Member

mdempsky commented Apr 20, 2021

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 a, _, b := 1, 2, 3 if they needed.

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.

@findleyr
Copy link
Contributor

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.

@mdempsky
Copy link
Member

mdempsky commented Apr 20, 2021

Since names cannot be repeated in a normal variable declaration

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:

Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

because earlier https://golang.org/ref/spec#Declarations_and_scope said

An identifier declared in a block may be redeclared in an inner block.

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".

@gopherbot
Copy link

Change https://golang.org/cl/312170 mentions this issue: cmd/compile/internal/types2: better errors for invalid short var decls

gopherbot pushed a commit that referenced this issue Apr 22, 2021
- 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>
@griesemer griesemer modified the milestones: Backlog, Go1.19 Mar 18, 2022
@gopherbot
Copy link

Change https://go.dev/cl/405757 mentions this issue: spec: state that variable names must be unique in short var decls

@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.
Projects
None yet
Development

No branches or pull requests

5 participants