Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 844e1fc

Browse files
committedMay 3, 2021
cmd/compile: make typecheckaste correctly report invalid use of "..."
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 <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 9ed736a commit 844e1fc

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
 

‎src/cmd/compile/internal/typecheck/typecheck.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,9 @@ func typecheckaste(op ir.Op, call ir.Node, isddd bool, tstruct *types.Type, nl i
13301330
n1 := tstruct.NumFields()
13311331
n2 := len(nl)
13321332
if !hasddd(tstruct) {
1333+
if isddd {
1334+
goto invalidddd
1335+
}
13331336
if n2 > n1 {
13341337
goto toomany
13351338
}
@@ -1395,6 +1398,8 @@ func typecheckaste(op ir.Op, call ir.Node, isddd bool, tstruct *types.Type, nl i
13951398
if i < len(nl) {
13961399
goto toomany
13971400
}
1401+
1402+
invalidddd:
13981403
if isddd {
13991404
if call != nil {
14001405
base.Errorf("invalid use of ... in call to %v", call)

‎test/ddd1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
_ = sum(tuple())
3030
_ = sum(tuple()...) // ERROR "multiple-value"
3131
_ = sum3(tuple())
32-
_ = sum3(tuple()...) // ERROR "multiple-value"
32+
_ = sum3(tuple()...) // ERROR "multiple-value" ERROR "invalid use of .*[.][.][.]"
3333
)
3434

3535
type T []T

‎test/fixedbugs/issue45913.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// errorcheck
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
)
12+
13+
func f(s1, s2 string) { fmt.Printf("%s %s", s1, s2) }
14+
15+
func main() {
16+
f([2]string{"a", "b"}...) // ERROR "invalid use of .*[.][.][.]|cannot use [.][.][.] in call to non-variadic"
17+
}

0 commit comments

Comments
 (0)
Please sign in to comment.