From 2cad84c5b187a7f8294adc97fe9e7cf9c0f036e5 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 09:39:54 +0100 Subject: [PATCH 1/4] compiler: fix variadic args not being nil when zero length. Fixes #806. --- compiler/utils.go | 4 ++++ tests/compiler_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/compiler_test.go 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..88932c254 --- /dev/null +++ b/tests/compiler_test.go @@ -0,0 +1,17 @@ +// +build js + +package tests + +import ( + "testing" +) + +func TestVariadicNil(t *testing.T) { + printVari := func(strs ...string) []string { + return strs + } + + if v := printVari(); v != nil { + t.Errorf("expected printVari() to be %v; got: %v", nil, v) + } +} From 20847916aadff567edc8f5b59d123b95f012cef5 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 18:08:22 +0100 Subject: [PATCH 2/4] Address feedback --- tests/compiler_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/compiler_test.go b/tests/compiler_test.go index 88932c254..8f848299f 100644 --- a/tests/compiler_test.go +++ b/tests/compiler_test.go @@ -1,5 +1,3 @@ -// +build js - package tests import ( @@ -11,7 +9,7 @@ func TestVariadicNil(t *testing.T) { return strs } - if v := printVari(); v != nil { - t.Errorf("expected printVari() to be %v; got: %v", nil, v) + if got := printVari(); got != nil { + t.Errorf("expected printVari() to be %v; got: %v", nil, got) } } From 00b561fa347b7a2b5deffa29a9391ab7648b9d25 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 18:34:25 +0100 Subject: [PATCH 3/4] Address more feedback --- tests/compiler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/compiler_test.go b/tests/compiler_test.go index 8f848299f..58b4f692a 100644 --- a/tests/compiler_test.go +++ b/tests/compiler_test.go @@ -10,6 +10,6 @@ func TestVariadicNil(t *testing.T) { } if got := printVari(); got != nil { - t.Errorf("expected printVari() to be %v; got: %v", nil, got) + t.Errorf("printVari(): got: %#v; want %#v.", got, nil) } } From cafa873c3d5760cc25485f771bdb40c943f8c188 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Mon, 30 Apr 2018 07:31:37 +0100 Subject: [PATCH 4/4] Add a couple more tests for varidic nil/non-nil checks --- tests/compiler_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/compiler_test.go b/tests/compiler_test.go index 58b4f692a..1ed209596 100644 --- a/tests/compiler_test.go +++ b/tests/compiler_test.go @@ -12,4 +12,18 @@ func TestVariadicNil(t *testing.T) { 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) + } + } }