]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile/internal/syntax: better error when an assignment is used in value context
authorRobert Griesemer <gri@golang.org>
Thu, 6 Feb 2020 23:32:40 +0000 (15:32 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 21 Feb 2020 22:57:52 +0000 (22:57 +0000)
The error message is now positioned at the statement position (which is
an identifing token, such as the '=' for assignments); and in case of
assignments it emphasizes the assignment by putting the Lhs and Rhs
in parentheses. Finally, the wording is changed from "use of * as value"
to the stronger "cannot use * as value" (for which there is precedent
elsewhere in the parser).

Fixes #36858.

Change-Id: Ic3f101bba50f58e3a1d9b29645066634631f2d61
Reviewed-on: https://go-review.googlesource.com/c/go/+/218337
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/syntax/testdata/issue23385.src
test/fixedbugs/issue18915.go
test/syntax/chan1.go
test/syntax/typesw.go

index f3c2c60ec81aa4d587d88146740f105b75714f46..469d9ad69baed86264488e9323cfb5d419a61654 100644 (file)
@@ -1886,11 +1886,16 @@ done:
                // which turns an expression into an assignment. Provide
                // a more explicit error message in that case to prevent
                // further confusion.
-               str := String(s)
+               var str string
                if as, ok := s.(*AssignStmt); ok && as.Op == 0 {
-                       str = "assignment " + str
+                       // 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) + ")"
+               } else {
+                       str = String(s)
                }
-               p.syntaxError(fmt.Sprintf("%s used as value", str))
+               p.syntaxErrorAt(s.Pos(), fmt.Sprintf("cannot use %s as value", str))
        }
 
        p.xnest = outer
index 44abcd7e23cf4fc8ed94698efb630e26da39d40e..2459a7369bc1e53c288899b1d35241916d7a1088 100644 (file)
@@ -6,7 +6,12 @@
 
 package p
 
-func f() {
-       if true || 0 = 1 /* ERROR assignment .* used as value */ {
+func _() {
+       if true || 0 /* ERROR cannot use assignment .* as value */ = 1 {
+       }
+}
+
+func _(a, b string) {
+       if a == "a" && b /* ERROR cannot use assignment .* as value */ = "b" {
        }
 }
index a432bbc17c9533f43220a427931513cb699890da..66e31e2556edfa299a8ed8816024af3ab953f811 100644 (file)
 package p
 
 func _() {
-       if a := 10 { // ERROR "a := 10 used as value"
+       if a := 10 { // ERROR "cannot use a := 10 as value"
        }
 
-       for b := 10 { // ERROR "b := 10 used as value"
+       for b := 10 { // ERROR "cannot use b := 10 as value"
        }
 
-       switch c := 10 { // ERROR "c := 10 used as value"
+       switch c := 10 { // ERROR "cannot use c := 10 as value"
        }
 }
index 4eb63796ac52d1d293f2fc747464be5d661ee766..56103d1d79ef6b7e7fdfc2126f005748c0a88731 100644 (file)
@@ -10,7 +10,7 @@ var c chan int
 var v int
 
 func main() {
-       if c <- v { // ERROR "used as value"
+       if c <- v { // ERROR "cannot use c <- v as value"
        }
 }
 
index f9120e885198e21d1b2c023f4d76ab79fb038da0..378193397897e5755d78d8f3cac2426fba8aa02d 100644 (file)
@@ -7,7 +7,7 @@
 package main
 
 func main() {
-       switch main() := interface{}(nil).(type) {      // ERROR "invalid variable name|used as value"
+       switch main() := interface{}(nil).(type) {      // ERROR "invalid variable name|cannot use .* as value"
        default:
        }
 }