Skip to content

Commit efb3cab

Browse files
committed
cmd/compile/internal/syntax: generalize error about var decls in init clauses
Change-Id: I62f9748b97bec245338ebf9686fbf6ad6dc6a9c2 Reviewed-on: https://go-review.googlesource.com/36931 Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent f823d30 commit efb3cab

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/cmd/compile/internal/syntax/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,8 +1703,8 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
17031703

17041704
if p.tok != _Semi {
17051705
// accept potential varDecl but complain
1706-
if keyword == _For && p.got(_Var) {
1707-
p.syntax_error("var declaration not allowed in for initializer")
1706+
if p.got(_Var) {
1707+
p.syntax_error(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
17081708
}
17091709
init = p.simpleStmt(nil, keyword == _For)
17101710
// If we have a range clause, we are done (can only happen for keyword == _For).

test/syntax/forvar.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

test/syntax/initvar.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// errorcheck
2+
3+
// Copyright 2010 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
if var x = 0; x < 10 {} // ERROR "var declaration not allowed in if initializer"
11+
12+
switch var x = 0; x {} // ERROR "var declaration not allowed in switch initializer"
13+
14+
for var x = 0; x < 10; {} // ERROR "var declaration not allowed in for initializer"
15+
}

0 commit comments

Comments
 (0)