From 88b99aa1d0a434fee9d488a082327fd2b88fd4ce Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 09:39:54 +0100 Subject: [PATCH 1/5] 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 81f451651..eeeb8971a 100644 --- a/compiler/utils.go +++ b/compiler/utils.go @@ -115,6 +115,10 @@ func (fc *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr, sigTypes := signatureTypes{Sig: sig} + 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 || fc.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 0cc819b5bf93981b42268dc58a2f4f50ff8489c3 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 18:08:22 +0100 Subject: [PATCH 2/5] 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 6f0cc784d29966aab0d79cebe4e8ae086a6bd6e2 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Thu, 26 Apr 2018 18:34:25 +0100 Subject: [PATCH 3/5] 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 beaadd3a6a90df518215d088d8bfe55ec14fbed5 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Mon, 30 Apr 2018 07:31:37 +0100 Subject: [PATCH 4/5] 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) + } + } } From 31134a5954210df28ce0f73b75d07e3f03633f52 Mon Sep 17 00:00:00 2001 From: Jonathan Hall Date: Mon, 27 Dec 2021 17:02:32 +0100 Subject: [PATCH 5/5] Get the code up to date with the latest master --- compiler/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/utils.go b/compiler/utils.go index eeeb8971a..e53a4036f 100644 --- a/compiler/utils.go +++ b/compiler/utils.go @@ -116,7 +116,7 @@ func (fc *funcContext) translateArgs(sig *types.Signature, argExprs []ast.Expr, sigTypes := signatureTypes{Sig: sig} if sig.Variadic() && len(argExprs) == 0 { - return []string{fmt.Sprintf("%s.nil", c.typeName(varargType))} + return []string{fmt.Sprintf("%s.nil", fc.typeName(sigTypes.VariadicType()))} } preserveOrder := false