]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: improve not enough / too many arguments errors
authorMatthew Dempsky <mdempsky@google.com>
Fri, 28 Oct 2016 21:22:13 +0000 (14:22 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 28 Oct 2016 21:53:07 +0000 (21:53 +0000)
Use "have" and "want" and multiple lines like other similar error
messages. Also, fix handling of ... and multi-value function calls.

Fixes #17650.

Change-Id: I4850e79c080eac8df3b92a4accf9e470dff63c9a
Reviewed-on: https://go-review.googlesource.com/32261
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/typecheck.go
test/ddd1.go
test/fixedbugs/issue4215.go
test/fixedbugs/issue6750.go

index 039b447259c3cedcadd9384366a2d13c5ce9917b..2a087cd40b73dbf9cc1ec3b8728dcd4106b53565 100644 (file)
@@ -2692,12 +2692,12 @@ notenough:
                        // Method expressions have the form T.M, and the compiler has
                        // rewritten those to ONAME nodes but left T in Left.
                        if call.Op == ONAME && call.Left != nil && call.Left.Op == OTYPE {
-                               yyerror("not enough arguments in call to method expression %v, got %s want %v", call, nl.retsigerr(), tstruct)
+                               yyerror("not enough arguments in call to method expression %v\n\thave %s\n\twant %v", call, nl.retsigerr(isddd), tstruct)
                        } else {
-                               yyerror("not enough arguments in call to %v, got %s want %v", call, nl.retsigerr(), tstruct)
+                               yyerror("not enough arguments in call to %v\n\thave %s\n\twant %v", call, nl.retsigerr(isddd), tstruct)
                        }
                } else {
-                       yyerror("not enough arguments to %v, got %s want %v", op, nl.retsigerr(), tstruct)
+                       yyerror("not enough arguments to %v\n\thave %s\n\twant %v", op, nl.retsigerr(isddd), tstruct)
                }
                if n != nil {
                        n.Diag = 1
@@ -2708,9 +2708,9 @@ notenough:
 
 toomany:
        if call != nil {
-               yyerror("too many arguments in call to %v, got %s want %v", call, nl.retsigerr(), tstruct)
+               yyerror("too many arguments in call to %v\n\thave %s\n\twant %v", call, nl.retsigerr(isddd), tstruct)
        } else {
-               yyerror("too many arguments to %v, got %s want %v", op, nl.retsigerr(), tstruct)
+               yyerror("too many arguments to %v\n\thave %s\n\twant %v", op, nl.retsigerr(isddd), tstruct)
        }
        goto out
 }
@@ -2738,17 +2738,27 @@ func (t *Type) sigrepr() string {
 
 // retsigerr returns the signature of the types
 // at the respective return call site of a function.
-func (nl Nodes) retsigerr() string {
+func (nl Nodes) retsigerr(isddd bool) string {
        if nl.Len() < 1 {
                return "()"
        }
 
        var typeStrings []string
-       for _, n := range nl.Slice() {
-               typeStrings = append(typeStrings, n.Type.sigrepr())
+       if nl.Len() == 1 && nl.First().Type != nil && nl.First().Type.IsFuncArgStruct() {
+               for _, f := range nl.First().Type.Fields().Slice() {
+                       typeStrings = append(typeStrings, f.Type.sigrepr())
+               }
+       } else {
+               for _, n := range nl.Slice() {
+                       typeStrings = append(typeStrings, n.Type.sigrepr())
+               }
        }
 
-       return fmt.Sprintf("(%s)", strings.Join(typeStrings, ", "))
+       ddd := ""
+       if isddd {
+               ddd = "..."
+       }
+       return fmt.Sprintf("(%s%s)", strings.Join(typeStrings, ", "), ddd)
 }
 
 // type check composite
index 7ea04f31c380073da44b31475cebda78266cff36..cf6a3a58734144005146eb0b58d48e03177e963c 100644 (file)
@@ -27,9 +27,9 @@ func tuple() (int, int, int) { return 1, 2, 3 }
 
 var (
        _ = sum(tuple())
-       _ = sum(tuple()...) // ERROR "multiple-value|[.][.][.]"
+       _ = sum(tuple()...) // ERROR "multiple-value"
        _ = sum3(tuple())
-       _ = sum3(tuple()...) // ERROR "multiple-value|[.][.][.]" "not enough"
+       _ = sum3(tuple()...) // ERROR "multiple-value" "not enough"
 )
 
 type T []T
@@ -59,4 +59,3 @@ func bad(args ...int) {
        _ = [...]byte("foo") // ERROR "[.][.][.]"
        _ = [...][...]int{{1,2,3},{4,5,6}}      // ERROR "[.][.][.]"
 }
-
index 7b8903fedede9ae04e6372c27e3ec102185083e9..795d48d7f557c552025bac9c0b92676ff6f4f8a1 100644 (file)
@@ -7,28 +7,28 @@
 package main
 
 func foo() (int, int) {
-       return 2.3 // ERROR "not enough arguments to return, got \(number\) want \(int, int\)"
+       return 2.3 // ERROR "not enough arguments to return\n\thave \(number\)\n\twant \(int, int\)"
 }
 
 func foo2() {
-       return int(2), 2 // ERROR "too many arguments to return, got \(int, number\) want \(\)"
+       return int(2), 2 // ERROR "too many arguments to return\n\thave \(int, number\)\n\twant \(\)"
 }
 
 func foo3(v int) (a, b, c, d int) {
        if v >= 0 {
-               return 1 // ERROR "not enough arguments to return, got \(number\) want \(int, int, int, int\)"
+               return 1 // ERROR "not enough arguments to return\n\thave \(number\)\n\twant \(int, int, int, int\)"
        }
-       return 2, 3 // ERROR "not enough arguments to return, got \(number, number\) want \(int, int, int, int\)"
+       return 2, 3 // ERROR "not enough arguments to return\n\thave \(number, number\)\n\twant \(int, int, int, int\)"
 }
 
 func foo4(name string) (string, int) {
        switch name {
        case "cow":
-               return "moo" // ERROR "not enough arguments to return, got \(string\) want \(string, int\)"
+               return "moo" // ERROR "not enough arguments to return\n\thave \(string\)\n\twant \(string, int\)"
        case "dog":
-               return "dog", 10, true // ERROR "too many arguments to return, got \(string, number, bool\) want \(string, int\)"
+               return "dog", 10, true // ERROR "too many arguments to return\n\thave \(string, number, bool\)\n\twant \(string, int\)"
        case "fish":
-               return "" // ERROR "not enough arguments to return, got \(string\) want \(string, int\)"
+               return "" // ERROR "not enough arguments to return\n\thave \(string\)\n\twant \(string, int\)"
        default:
                return "lizard", 10
        }
@@ -40,14 +40,14 @@ type U float64
 
 func foo5() (S, T, U) {
        if false {
-               return "" // ERROR "not enough arguments to return, got \(string\) want \(S, T, U\)"
+               return "" // ERROR "not enough arguments to return\n\thave \(string\)\n\twant \(S, T, U\)"
        } else {
                ptr := new(T)
-               return ptr // ERROR "not enough arguments to return, got \(\*T\) want \(S, T, U\)"
+               return ptr // ERROR "not enough arguments to return\n\thave \(\*T\)\n\twant \(S, T, U\)"
        }
-       return new(S), 12.34, 1 + 0i, 'r', true // ERROR "too many arguments to return, got \(\*S, number, number, number, bool\) want \(S, T, U\)"
+       return new(S), 12.34, 1 + 0i, 'r', true // ERROR "too many arguments to return\n\thave \(\*S, number, number, number, bool\)\n\twant \(S, T, U\)"
 }
 
 func foo6() (T, string) {
-       return "T", true, true // ERROR "too many arguments to return, got \(string, bool, bool\) want \(T, string\)"
+       return "T", true, true // ERROR "too many arguments to return\n\thave \(string, bool, bool\)\n\twant \(T, string\)"
 }
index 8854bf9e09ff6f6cbe4f6c4ce060317d72e555d3..dbbb454435df396650e1937343b5b235da1a2556 100644 (file)
@@ -18,5 +18,5 @@ func printmany(nums ...int) {
 func main() {
        printmany(1, 2, 3)
        printmany([]int{1, 2, 3}...)
-       printmany(1, "abc", []int{2, 3}...) // ERROR "too many arguments in call to printmany, got \(number, string, \[\]int\) want \(...int\)"
+       printmany(1, "abc", []int{2, 3}...) // ERROR "too many arguments in call to printmany\n\thave \(number, string, \[\]int\.\.\.\)\n\twant \(...int\)"
 }