]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use more specific error message for assignment mismatch
authorDavid Heuschmann <heuschmann.d@gmail.com>
Sat, 15 Sep 2018 11:04:59 +0000 (13:04 +0200)
committerRobert Griesemer <gri@golang.org>
Thu, 27 Sep 2018 00:35:06 +0000 (00:35 +0000)
Show a more specifc error message in the form of "%d variables but %v
returns %d values" if an assignment mismatch occurs with a function
or method call on the right.

Fixes #27595

Change-Id: Ibc97d070662b08f150ac22d686059cf224e012ab
Reviewed-on: https://go-review.googlesource.com/135575
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/gc/typecheck.go
test/fixedbugs/issue26616.go
test/fixedbugs/issue27595.go [new file with mode: 0644]

index 69dced00ac729fd461f72dca5e7e615c02b77602..24aba6bac4d1e3a8be67144fa0ff24a350f56cb5 100644 (file)
@@ -3341,7 +3341,7 @@ func typecheckas(n *Node) {
        checkassign(n, n.Left)
        if n.Right != nil && n.Right.Type != nil {
                if n.Right.Type.IsFuncArgStruct() {
-                       yyerror("assignment mismatch: 1 variable but %d values", n.Right.Type.NumFields())
+                       yyerror("assignment mismatch: 1 variable but %v returns %d values", n.Right.Left, n.Right.Type.NumFields())
                        // Multi-value RHS isn't actually valid for OAS; nil out
                        // to indicate failed typechecking.
                        n.Right.Type = nil
@@ -3486,7 +3486,12 @@ func typecheckas2(n *Node) {
        }
 
 mismatch:
-       yyerror("assignment mismatch: %d variables but %d values", cl, cr)
+       switch r.Op {
+       default:
+               yyerror("assignment mismatch: %d variable but %d values", cl, cr)
+       case OCALLFUNC, OCALLMETH, OCALLINTER:
+               yyerror("assignment mismatch: %d variables but %v returns %d values", cl, r.Left, cr)
+       }
 
        // second half of dance
 out:
index 46136dc68f53271496f23db07d718b18ebbeba08..e5565b68ca55aa9160d9625188f62dc038f6b21e 100644 (file)
@@ -6,13 +6,13 @@
 
 package p
 
-var x int = three() // ERROR "1 variable but 3 values"
+var x int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
 
 func f() {
-       var _ int = three() // ERROR "1 variable but 3 values"
-       var a int = three() // ERROR "1 variable but 3 values"
-       a = three()         // ERROR "1 variable but 3 values"
-       b := three()        // ERROR "1 variable but 3 values"
+       var _ int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
+       var a int = three() // ERROR "assignment mismatch: 1 variable but three returns 3 values"
+       a = three()         // ERROR "assignment mismatch: 1 variable but three returns 3 values"
+       b := three()        // ERROR "assignment mismatch: 1 variable but three returns 3 values"
 
        _, _ = a, b
 }
diff --git a/test/fixedbugs/issue27595.go b/test/fixedbugs/issue27595.go
new file mode 100644 (file)
index 0000000..af5c7a1
--- /dev/null
@@ -0,0 +1,19 @@
+// errorcheck
+
+// Copyright 2018 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 main
+
+var a = twoResults()       // ERROR "assignment mismatch: 1 variable but twoResults returns 2 values"
+var b, c, d = twoResults() // ERROR "assignment mismatch: 3 variables but twoResults returns 2 values"
+var e, f = oneResult()     // ERROR "assignment mismatch: 2 variables but oneResult returns 1 values"
+
+func twoResults() (int, int) {
+       return 1, 2
+}
+
+func oneResult() int {
+       return 1
+}