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

cmd/gc: compiler accepts incorrect short variable declaration #8435

Closed
griesemer opened this issue Jul 28, 2014 · 5 comments
Closed

cmd/gc: compiler accepts incorrect short variable declaration #8435

griesemer opened this issue Jul 28, 2014 · 5 comments
Milestone

Comments

@griesemer
Copy link
Contributor

Originally reported by Evan Kroske via e-mail to golang-dev (July 27, 2014):

=================
In the process of working on [issue #6764][1], I realized that gc permits declaring a
variable twice in the same short variable declaration if the variable is shadowing
another variable.

For example, gc reports an error for the following code:

    a, a := 1, 2
    _ = a

However, gc compiles this code without complaint.

    var a int8
    {
        a, a := 1, 2
        println(a)
    }
    _ = a
=================

The compiler appears to accept this 2nd short variable declaration as a redeclaration of
the outer a, and new declaration of the inner a:

http://play.golang.org/p/Vy0kYBj82w

(running this program prints 1, 2, 3).

- gccgo also accepts this w/o errors.

- gotype complains with an error for this program:

$ gotype x.go
x.go:7:6: a redeclared in this block
x.go:7:3:   other declaration of a

The spec is not 100% clear, but the compiler behavior is odd at any rate: 

- it is not clear which one (the 1st or the 2nd) variable is the redeclaration, and thus
the value of the redeclared variable is unknown from the program
- code relying on this mechanism is likely to have bugs
- there's no such code in the std library since gotype passes it w/o errors

I think this is a compiler bug.

See also related issue #6764.
@griesemer
Copy link
Contributor Author

Comment 1:

Correction: The spec is very clear that only variables in the same block may be
redeclared. This is clearly a compiler bug.

Status changed to Accepted.

@evankroske
Copy link

Comment 2:

I'll fix this issue and issue #6764 together.

@gopherbot
Copy link

Comment 3:

CL https://golang.org/cl/116440046 mentions this issue.

@rsc
Copy link
Contributor

rsc commented Oct 6, 2014

Comment 4:

This issue was closed by revision 55df81d.

Status changed to Fixed.

@ianlancetaylor
Copy link
Contributor

Comment 5:

Issue #8993 has been merged into this issue.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 25, 2018
…bols

Fixes golang#6764.
Fixes golang#8435.

LGTM=rsc
R=golang-codereviews, r, gobot, rsc
CC=golang-codereviews
https://golang.org/cl/116440046
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jun 26, 2018
…bols

Fixes golang#6764.
Fixes golang#8435.

LGTM=rsc
R=golang-codereviews, r, gobot, rsc
CC=golang-codereviews
https://golang.org/cl/116440046
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 9, 2018
…bols

Fixes golang#6764.
Fixes golang#8435.

LGTM=rsc
R=golang-codereviews, r, gobot, rsc
CC=golang-codereviews
https://golang.org/cl/116440046
wheatman pushed a commit to wheatman/go-akaros that referenced this issue Jul 30, 2018
…bols

Fixes golang#6764.
Fixes golang#8435.

LGTM=rsc
R=golang-codereviews, r, gobot, rsc
CC=golang-codereviews
https://golang.org/cl/116440046
@rsc rsc removed their assignment Jun 23, 2022
This issue was closed.
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