aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2017-02-06 15:08:28 -0800
committerRuss Cox <rsc@golang.org>2017-02-07 16:51:22 +0000
commitc543cc353deb4cffe213be5b094c8357faecab07 (patch)
tree96f7c1b517e5cf2214a67a271c9df333a1151085
parentf0749fe163ca4917f903ed424008af37e834ace4 (diff)
downloadgo-c543cc353deb4cffe213be5b094c8357faecab07.tar.gz
go-c543cc353deb4cffe213be5b094c8357faecab07.zip
[release-branch.go1.8] cmd/compile/internal/syntax: make a parser error "1.7 compliant"
For code such as if a := 10 { ... the 1.7 compiler reported a := 10 used as value while the 1.8 compiler reported invalid condition, tag, or type switch guard Changed the error message to match the 1.7 compiler. Fixes #18915. Change-Id: I01308862e461922e717f9f8295a9db53d5a914eb Reviewed-on: https://go-review.googlesource.com/36470 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-on: https://go-review.googlesource.com/36422 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
-rw-r--r--src/cmd/compile/internal/syntax/parser.go7
-rw-r--r--test/fixedbugs/issue18915.go21
2 files changed, 27 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index 121dfb75e5..92eae0a458 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -1680,7 +1680,12 @@ func (p *parser) header(forStmt bool) (init SimpleStmt, cond Expr, post SimpleSt
case *ExprStmt:
cond = s.X
default:
- p.error("invalid condition, tag, or type switch guard")
+ // Not obviously a syntax error but by making it one, we get
+ // automatic filtering of multiple syntax error messages per
+ // line in the compiler. This avoids the follow-up error
+ // "missing condition in if statement" for an if statement
+ // (minimal fix for #18915).
+ p.syntax_error(fmt.Sprintf("%s used as value", String(s)))
}
p.xnest = outer
diff --git a/test/fixedbugs/issue18915.go b/test/fixedbugs/issue18915.go
new file mode 100644
index 0000000000..a432bbc17c
--- /dev/null
+++ b/test/fixedbugs/issue18915.go
@@ -0,0 +1,21 @@
+// errorcheck
+
+// Copyright 2017 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.
+
+// Make sure error message for invalid conditions
+// or tags are consistent with earlier Go versions.
+
+package p
+
+func _() {
+ if a := 10 { // ERROR "a := 10 used as value"
+ }
+
+ for b := 10 { // ERROR "b := 10 used as value"
+ }
+
+ switch c := 10 { // ERROR "c := 10 used as value"
+ }
+}