Skip to content

Commit 422c7fe

Browse files
committed
cmd/compile: don't permit declarations in post statement of for loop
Report syntax error that was missed when moving to new parser. Fixes #19610. Change-Id: Ie5625f907a84089dc56fcccfd4f24df546042783 Reviewed-on: https://go-review.googlesource.com/38375 Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 17570a9 commit 422c7fe

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,9 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
17221722
p.want(_Semi)
17231723
if p.tok != _Lbrace {
17241724
post = p.simpleStmt(nil, false)
1725+
if a, _ := post.(*AssignStmt); a != nil && a.Op == Def {
1726+
p.syntax_error_at(a.Pos(), "cannot declare in post statement of for loop")
1727+
}
17251728
}
17261729
} else if p.tok != _Lbrace {
17271730
condStmt = p.simpleStmt(nil, false)

test/fixedbugs/issue19610.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// errorcheck
2+
3+
// Copyright 2017 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+
for ; ; x := 1 { // ERROR "cannot declare in post statement"
11+
_ = x
12+
break
13+
}
14+
}

0 commit comments

Comments
 (0)