compiler: process empty []byte{} literals as ByteArrays

It is wrong to count an empty byte slice as an Array.
This commit is contained in:
Evgenii Stratonikov 2020-02-11 13:15:08 +03:00
parent 32bce30777
commit 8ea3ef0b62
2 changed files with 18 additions and 0 deletions

View file

@ -189,6 +189,12 @@ func isBuiltin(expr ast.Expr) bool {
func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool { func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool {
if len(lit.Elts) == 0 { 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 return false
} }

View file

@ -293,6 +293,18 @@ func TestAppendByte(t *testing.T) {
eval(t, src, []uint8{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06}) 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) { func TestAppendString(t *testing.T) {
src := ` src := `
package foo package foo