aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/compile/internal/syntax/parser.go17
-rw-r--r--src/cmd/compile/internal/syntax/testdata/issue60599.go11
2 files changed, 24 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/syntax/parser.go b/src/cmd/compile/internal/syntax/parser.go
index c8b8ab0601..b5602fcff7 100644
--- a/src/cmd/compile/internal/syntax/parser.go
+++ b/src/cmd/compile/internal/syntax/parser.go
@@ -2369,10 +2369,8 @@ done:
// further confusion.
var str string
if as, ok := s.(*AssignStmt); ok && as.Op == 0 {
- // Emphasize Lhs and Rhs of assignment with parentheses to highlight '='.
- // Do it always - it's not worth going through the trouble of doing it
- // only for "complex" left and right sides.
- str = "assignment (" + String(as.Lhs) + ") = (" + String(as.Rhs) + ")"
+ // Emphasize complex Lhs and Rhs of assignment with parentheses to highlight '='.
+ str = "assignment " + emphasize(as.Lhs) + " = " + emphasize(as.Rhs)
} else {
str = String(s)
}
@@ -2383,6 +2381,17 @@ done:
return
}
+// emphasize returns a string representation of x, with (top-level)
+// binary expressions emphasized by enclosing them in parentheses.
+func emphasize(x Expr) string {
+ s := String(x)
+ if op, _ := x.(*Operation); op != nil && op.Y != nil {
+ // binary expression
+ return "(" + s + ")"
+ }
+ return s
+}
+
func (p *parser) ifStmt() *IfStmt {
if trace {
defer p.trace("ifStmt")()
diff --git a/src/cmd/compile/internal/syntax/testdata/issue60599.go b/src/cmd/compile/internal/syntax/testdata/issue60599.go
new file mode 100644
index 0000000000..711d97bde0
--- /dev/null
+++ b/src/cmd/compile/internal/syntax/testdata/issue60599.go
@@ -0,0 +1,11 @@
+// Copyright 2023 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 p
+
+func _(x, y, z int) {
+ if x /* ERROR cannot use assignment x = y as value */ = y {}
+ if x || y /* ERROR cannot use assignment \(x || y\) = z as value */ = z {}
+ if x /* ERROR cannot use assignment x = \(y || z\) as value */ = y || z {}
+}