// 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)
}
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")()
--- /dev/null
+// 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 {}
+}