From 8ea3ef0b62e0644afbab88e8d8b0bddd2e99fec2 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 11 Feb 2020 13:15:08 +0300 Subject: [PATCH] compiler: process empty []byte{} literals as ByteArrays It is wrong to count an empty byte slice as an Array. --- pkg/compiler/analysis.go | 6 ++++++ pkg/compiler/for_test.go | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index ad46b8b93..bf3272baa 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -189,6 +189,12 @@ func isBuiltin(expr ast.Expr) bool { func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool { if len(lit.Elts) == 0 { + if typ, ok := lit.Type.(*ast.ArrayType); ok { + if name, ok := typ.Elt.(*ast.Ident); ok { + return name.Name == "byte" || name.Name == "uint8" + } + } + return false } diff --git a/pkg/compiler/for_test.go b/pkg/compiler/for_test.go index 83b460517..6c3f67b87 100644 --- a/pkg/compiler/for_test.go +++ b/pkg/compiler/for_test.go @@ -293,6 +293,18 @@ func TestAppendByte(t *testing.T) { eval(t, src, []uint8{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}) } +func TestAppendByteToEmpty(t *testing.T) { + src := ` + package foo + func Main() []byte { + out := []byte{} + out = append(out, 1) + out = append(out, 2) + return out + }` + eval(t, src, []byte{1, 2}) +} + func TestAppendString(t *testing.T) { src := ` package foo