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/compile: invalid for range loop executes w/o error message #6772

Closed
griesemer opened this issue Nov 15, 2013 · 5 comments
Closed

cmd/compile: invalid for range loop executes w/o error message #6772

griesemer opened this issue Nov 15, 2013 · 5 comments

Comments

@griesemer
Copy link
Contributor

This program

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

reports an error as expected, albeit a confusing one. This is related to, or the same as
issue #6764.

A short variable declaration in a for range loop can never redeclare an existing
variable because a for statement opens a new scope and thus there are no variables
declared earlier in that scope. So there should be a double declaration error.

Interestingly, adding an external declaration appears to "fix" the problem:

http://play.golang.org/p/4spddNy6Ki

The code compiles and runs without errors. Output:
0
1
2
3

Here it looks like the short variable declaration actually was handled like a
redeclaration of the outer a. Interestingly, value assigned to a is the index and not
the indexed element. Contrast this (and its output) to the following correct program:

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

Output:
1
2
3
3
@rsc
Copy link
Contributor

rsc commented Nov 27, 2013

Comment 1:

Labels changed: added go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 2:

Labels changed: added release-none, removed go1.3maybe.

@rsc
Copy link
Contributor

rsc commented Dec 4, 2013

Comment 3:

Labels changed: added repo-main.

@rsc rsc removed the compiler-bug label Apr 10, 2015
@rsc rsc added this to the Unplanned milestone Apr 10, 2015
@rsc rsc changed the title cmd/gc: invalid for range loop executes w/o error message cmd/compile: invalid for range loop executes w/o error message Jun 8, 2015
@odeke-em
Copy link
Member

@griesemer, it seems like CL https://go-review.googlesource.com/c/4851 solved the
issue(the CL is from February 2015, while this issue is from November 2013) repros
in #6772 (comment)

Let me write a CL to lock in this behavior with tests.

@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Dec 24, 2017
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

4 participants