]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: better error message for incorrect if/switch header
authorRobert Griesemer <gri@golang.org>
Tue, 3 Apr 2018 21:39:10 +0000 (14:39 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 3 Apr 2018 21:57:37 +0000 (21:57 +0000)
Fixes #23664.

Change-Id: Ic0637e9f896b2fc6502dfbab2d1c4de3c62c0bd2
Reviewed-on: https://go-review.googlesource.com/104616
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>

src/cmd/compile/internal/syntax/parser.go
test/fixedbugs/issue23664.go [new file with mode: 0644]

index 68d09ef69725797c09f87845bda5353f481c4a77..e1cd8f9f5a1bb1de66c0d0f4db4d82efbe6b5967 100644 (file)
@@ -1824,7 +1824,8 @@ func (p *parser) header(keyword token) (init SimpleStmt, cond Expr, post SimpleS
                        semi.lit = p.lit
                        p.next()
                } else {
-                       p.want(_Semi)
+                       // asking for a '{' rather than a ';' here leads to a better error message
+                       p.want(_Lbrace)
                }
                if keyword == _For {
                        if p.tok != _Semi {
diff --git a/test/fixedbugs/issue23664.go b/test/fixedbugs/issue23664.go
new file mode 100644 (file)
index 0000000..1925ebf
--- /dev/null
@@ -0,0 +1,17 @@
+// errorcheck
+
+// Copyright 2018 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.
+
+// Verify error messages for incorrect if/switch headers.
+
+package p
+
+func f() {
+       if f() true { // ERROR "unexpected true, expecting {"
+       }
+       
+       switch f() true { // ERROR "unexpected true, expecting {"
+       }
+}