]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: generalize error about var decls in init clauses
authorRobert Griesemer <gri@golang.org>
Mon, 13 Feb 2017 23:08:04 +0000 (15:08 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 13 Feb 2017 23:15:32 +0000 (23:15 +0000)
Change-Id: I62f9748b97bec245338ebf9686fbf6ad6dc6a9c2
Reviewed-on: https://go-review.googlesource.com/36931
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/syntax/parser.go
test/syntax/forvar.go [deleted file]
test/syntax/initvar.go [new file with mode: 0644]

index e4aaa12ae50e4ae0b81ff749f05090a032cd2683..d7f542e609da016d2e5a817e3b78d7e71d59034e 100644 (file)
@@ -1703,8 +1703,8 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
 
        if p.tok != _Semi {
                // accept potential varDecl but complain
-               if keyword == _For && p.got(_Var) {
-                       p.syntax_error("var declaration not allowed in for initializer")
+               if p.got(_Var) {
+                       p.syntax_error(fmt.Sprintf("var declaration not allowed in %s initializer", keyword.String()))
                }
                init = p.simpleStmt(nil, keyword == _For)
                // If we have a range clause, we are done (can only happen for keyword == _For).
diff --git a/test/syntax/forvar.go b/test/syntax/forvar.go
deleted file mode 100644 (file)
index 3a70d9c..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-// errorcheck
-
-// Copyright 2010 The Go Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-package main
-
-func main() {
-       var x int // avoid undefined: x error below with recursive-descent parser
-       for var x = 0; x < 10; x++ {    // ERROR "var declaration not allowed in for initializer"
diff --git a/test/syntax/initvar.go b/test/syntax/initvar.go
new file mode 100644 (file)
index 0000000..74623f5
--- /dev/null
@@ -0,0 +1,15 @@
+// errorcheck
+
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package main
+
+func main() {
+       if var x = 0; x < 10 {}    // ERROR "var declaration not allowed in if initializer"
+
+       switch var x = 0; x {}     // ERROR "var declaration not allowed in switch initializer"
+
+       for var x = 0; x < 10; {}  // ERROR "var declaration not allowed in for initializer"
+}