cmd/compile: make error message involving variadic calls clearer

Fixes #41440

Change-Id: I2fbac72ae3b76bca32cdeaee678a19af3595d116
Reviewed-on: https://go-review.googlesource.com/c/go/+/255241
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
diff --git a/src/cmd/compile/internal/gc/typecheck.go b/src/cmd/compile/internal/gc/typecheck.go
index cbfaa30..2654177 100644
--- a/src/cmd/compile/internal/gc/typecheck.go
+++ b/src/cmd/compile/internal/gc/typecheck.go
@@ -2717,7 +2717,7 @@
 // sigrepr is a type's representation to the outside world,
 // in string representations of return signatures
 // e.g in error messages about wrong arguments to return.
-func sigrepr(t *types.Type) string {
+func sigrepr(t *types.Type, isddd bool) string {
 	switch t {
 	case types.Idealstring:
 		return "string"
@@ -2732,6 +2732,13 @@
 		return "number"
 	}
 
+	// Turn []T... argument to ...T for clearer error message.
+	if isddd {
+		if !t.IsSlice() {
+			Fatalf("bad type for ... argument: %v", t)
+		}
+		return "..." + t.Elem().String()
+	}
 	return t.String()
 }
 
@@ -2742,15 +2749,12 @@
 	}
 
 	var typeStrings []string
-	for _, n := range nl.Slice() {
-		typeStrings = append(typeStrings, sigrepr(n.Type))
+	for i, n := range nl.Slice() {
+		isdddArg := isddd && i == nl.Len()-1
+		typeStrings = append(typeStrings, sigrepr(n.Type, isdddArg))
 	}
 
-	ddd := ""
-	if isddd {
-		ddd = "..."
-	}
-	return fmt.Sprintf("(%s%s)", strings.Join(typeStrings, ", "), ddd)
+	return fmt.Sprintf("(%s)", strings.Join(typeStrings, ", "))
 }
 
 // type check composite
diff --git a/test/fixedbugs/issue41440.go b/test/fixedbugs/issue41440.go
new file mode 100644
index 0000000..2b441db
--- /dev/null
+++ b/test/fixedbugs/issue41440.go
@@ -0,0 +1,14 @@
+// errorcheck
+
+// Copyright 2020 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 f(...int) {}
+
+func g() {
+	var x []int
+	f(x, x...) // ERROR "have \(\[\]int, \.\.\.int\)"
+}
diff --git a/test/fixedbugs/issue6750.go b/test/fixedbugs/issue6750.go
index dbbb454..f62a850 100644
--- a/test/fixedbugs/issue6750.go
+++ b/test/fixedbugs/issue6750.go
@@ -18,5 +18,5 @@
 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\n\thave \(number, string, \[\]int\.\.\.\)\n\twant \(...int\)"
+	printmany(1, "abc", []int{2, 3}...) // ERROR "too many arguments in call to printmany\n\thave \(number, string, \.\.\.int\)\n\twant \(...int\)"
 }