mirror of https://github.com/golang/go.git
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>
This commit is contained in:
parent
4ffc2bc533
commit
df73945fd2
|
|
@ -2717,7 +2717,7 @@ func errorDetails(nl Nodes, tstruct *types.Type, isddd bool) string {
|
|||
// 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 @@ func sigrepr(t *types.Type) string {
|
|||
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 @@ func (nl Nodes) sigerr(isddd bool) string {
|
|||
}
|
||||
|
||||
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
|
||||
|
|
|
|||
|
|
@ -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\)"
|
||||
}
|
||||
|
|
@ -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\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\)"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue