]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make typecheckaste correctly report invalid use of "..."
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 2 May 2021 19:19:10 +0000 (02:19 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Mon, 3 May 2021 15:03:57 +0000 (15:03 +0000)
Currently, when "..." argument is passed to non-variadic function, the
compiler may skip that check, but continue checking whether the number
of arguments matches the function signature.

That causes the sanity check which was added in CL 255241 trigger.

Instead, we should report an invalid use of "...", which matches the
behavior of new type checker and go/types.

Fixes #45913

Change-Id: Icbb254052cbcd756bbd41f966c2c8e316c44420f
Reviewed-on: https://go-review.googlesource.com/c/go/+/315796
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>
src/cmd/compile/internal/typecheck/typecheck.go
test/ddd1.go
test/fixedbugs/issue45913.go [new file with mode: 0644]

index 16501443755e5c9114011039ce900a82272cbbff..4c5472137a58cf3022de467ba50b4a0412500769 100644 (file)
@@ -1330,6 +1330,9 @@ func typecheckaste(op ir.Op, call ir.Node, isddd bool, tstruct *types.Type, nl i
        n1 := tstruct.NumFields()
        n2 := len(nl)
        if !hasddd(tstruct) {
+               if isddd {
+                       goto invalidddd
+               }
                if n2 > n1 {
                        goto toomany
                }
@@ -1395,6 +1398,8 @@ func typecheckaste(op ir.Op, call ir.Node, isddd bool, tstruct *types.Type, nl i
        if i < len(nl) {
                goto toomany
        }
+
+invalidddd:
        if isddd {
                if call != nil {
                        base.Errorf("invalid use of ... in call to %v", call)
index 01b9c0eadb2e821c108f6bf82babf35f3d4c2b4d..ad49b347f49dd66439b17fb86e9d2ad3569fc2e3 100644 (file)
@@ -29,7 +29,7 @@ var (
        _ = sum(tuple())
        _ = sum(tuple()...) // ERROR "multiple-value"
        _ = sum3(tuple())
-       _ = sum3(tuple()...) // ERROR "multiple-value"
+       _ = sum3(tuple()...) // ERROR "multiple-value" ERROR "invalid use of .*[.][.][.]"
 )
 
 type T []T
diff --git a/test/fixedbugs/issue45913.go b/test/fixedbugs/issue45913.go
new file mode 100644 (file)
index 0000000..aa86028
--- /dev/null
@@ -0,0 +1,17 @@
+// errorcheck
+
+// Copyright 2021 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
+
+import (
+       "fmt"
+)
+
+func f(s1, s2 string) { fmt.Printf("%s %s", s1, s2) }
+
+func main() {
+       f([2]string{"a", "b"}...) // ERROR "invalid use of .*[.][.][.]|cannot use [.][.][.] in call to non-variadic"
+}