diff --git a/compiler/utils.go b/compiler/utils.go index d5452e0a6..98e3ed3c0 100644 --- a/compiler/utils.go +++ b/compiler/utils.go @@ -95,6 +95,10 @@ func (c *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr, e varargType = sig.Params().At(paramsLen - 1).Type().(*types.Slice) } + if sig.Variadic() && len(argExprs) == 0 { + return []string{fmt.Sprintf("%s.nil", c.typeName(varargType))} + } + preserveOrder := false for i := 1; i < len(argExprs); i++ { preserveOrder = preserveOrder || c.Blocking[argExprs[i]] diff --git a/tests/compiler_test.go b/tests/compiler_test.go new file mode 100644 index 000000000..1ed209596 --- /dev/null +++ b/tests/compiler_test.go @@ -0,0 +1,29 @@ +package tests + +import ( + "testing" +) + +func TestVariadicNil(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) + } + } + + { + want := []string{} + if got := printVari(want...); got == nil || len(got) != len(want) { + t.Errorf("printVari(want...): got: %#v; want %#v.", got, want) + } + } +}