Skip to content

Pass nil slice when variadic arguments are omitted after regular args. #1150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions compiler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,15 @@ func (fc *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr,
// If variadic arguments were passed in as individual elements, regroup them
// into a slice and pass it as a single argument.
if sig.Variadic() && !ellipsis {
return append(args[:sigTypes.RequiredParams()],
fmt.Sprintf("new %s([%s])", fc.typeName(sigTypes.VariadicType()), strings.Join(args[sigTypes.RequiredParams():], ", ")))
required := args[:sigTypes.RequiredParams()]
var variadic string
if len(args) == sigTypes.RequiredParams() {
// If no variadic parameters were passed, the slice value defaults to nil.
variadic = fmt.Sprintf("%s.nil", fc.typeName(sigTypes.VariadicType()))
} else {
variadic = fmt.Sprintf("new %s([%s])", fc.typeName(sigTypes.VariadicType()), strings.Join(args[sigTypes.RequiredParams():], ", "))
}
return append(required, variadic)
}
return args
}
Expand Down
57 changes: 41 additions & 16 deletions tests/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,50 @@ import (
)

func TestVariadicNil(t *testing.T) {
printVari := func(strs ...string) []string {
return strs
}
t.Run("only variadic", func(t *testing.T) {
printVari := func(strs ...string) []string {
return strs
}

if got := printVari(); got != nil {
t.Errorf("printVari(): got: %#v; want %#v.", got, nil)
}

{
var want []string
if got := printVari(want...); got != nil {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, nil)
}
}

if got := printVari(); got != nil {
t.Errorf("printVari(): got: %#v; want %#v.", got, nil)
}
{
want := []string{}
if got := printVari(want...); got == nil || len(got) != len(want) {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, want)
}
}
})
t.Run("mixed", func(t *testing.T) {
printVari := func(_ int, strs ...string) []string {
return strs
}

if got := printVari(0); got != nil {
t.Errorf("printVari(): got: %#v; want %#v.", got, nil)
}

{
var want []string
if got := printVari(want...); got != nil {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, nil)
{
var want []string
if got := printVari(0, want...); got != nil {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, nil)
}
}
}

{
want := []string{}
if got := printVari(want...); got == nil || len(got) != len(want) {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, want)
{
want := []string{}
if got := printVari(0, want...); got == nil || len(got) != len(want) {
t.Errorf("printVari(want...): got: %#v; want %#v.", got, want)
}
}
}
})
}