Skip to content

Commit c543cc3

Browse files
griesemerrsc
authored andcommitted
[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 <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-on: https://go-review.googlesource.com/36422 Run-TryBot: Russ Cox <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent f0749fe commit c543cc3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,12 @@ func (p *parser) header(forStmt bool) (init SimpleStmt, cond Expr, post SimpleSt
16801680
case *ExprStmt:
16811681
cond = s.X
16821682
default:
1683-
p.error("invalid condition, tag, or type switch guard")
1683+
// Not obviously a syntax error but by making it one, we get
1684+
// automatic filtering of multiple syntax error messages per
1685+
// line in the compiler. This avoids the follow-up error
1686+
// "missing condition in if statement" for an if statement
1687+
// (minimal fix for #18915).
1688+
p.syntax_error(fmt.Sprintf("%s used as value", String(s)))
16841689
}
16851690

16861691
p.xnest = outer

test/fixedbugs/issue18915.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
// Make sure error message for invalid conditions
8+
// or tags are consistent with earlier Go versions.
9+
10+
package p
11+
12+
func _() {
13+
if a := 10 { // ERROR "a := 10 used as value"
14+
}
15+
16+
for b := 10 { // ERROR "b := 10 used as value"
17+
}
18+
19+
switch c := 10 { // ERROR "c := 10 used as value"
20+
}
21+
}

0 commit comments

Comments
 (0)