interp: fix append a slice on binary slice

Fixes #1128.
This commit is contained in:
Marc Vertes
2021-06-15 14:34:08 +02:00
committed by GitHub
parent ab44c38298
commit db955e671f
4 changed files with 37 additions and 6 deletions

14
_test/append3.go Normal file
View File

@@ -0,0 +1,14 @@
package main
import "fmt"
func main() {
a := []int{1, 2}
b := [2]int{3, 4}
fmt.Println(append(a, b[:]...))
fmt.Println(append(a, []int{5, 6}...))
}
// Output:
// [1 2 3 4]
// [1 2 5 6]

10
_test/issue-1128.go Normal file
View File

@@ -0,0 +1,10 @@
package main
import "net"
func main() {
c := append(net.Buffers{}, []byte{})
println(len(c))
}
// Output: 1

View File

@@ -2917,7 +2917,7 @@ func _append(n *node) {
if len(n.child) == 3 {
c1, c2 := n.child[1], n.child[2]
if (c1.typ.cat == valueT || c2.typ.cat == valueT) && c1.typ.rtype == c2.typ.rtype ||
(c2.typ.cat == arrayT || c2.typ.cat == sliceT || c2.typ.cat == variadicT) && c2.typ.val.id() == n.typ.val.id() ||
isArray(c2.typ) && c2.typ.elem().id() == n.typ.elem().id() ||
isByteArray(c1.typ.TypeOf()) && isString(c2.typ.TypeOf()) {
appendSlice(n)
return
@@ -2957,13 +2957,13 @@ func _append(n *node) {
}
} else {
var value0 func(*frame) reflect.Value
switch {
case isEmptyInterface(n.typ.val):
switch elem := n.typ.elem(); {
case isEmptyInterface(elem):
value0 = genValue(n.child[2])
case isInterfaceSrc(n.typ.val):
case isInterfaceSrc(elem):
value0 = genValueInterface(n.child[2])
case isRecursiveType(n.typ.val, n.typ.val.rtype):
value0 = genValueRecursiveInterface(n.child[2], n.typ.val.rtype)
case isRecursiveType(elem, elem.rtype):
value0 = genValueRecursiveInterface(n.child[2], elem.rtype)
case n.child[2].typ.untyped:
value0 = genValueAs(n.child[2], n.child[1].typ.TypeOf().Elem())
default:

View File

@@ -1601,6 +1601,13 @@ func (t *itype) hasNil() bool {
return false
}
func (t *itype) elem() *itype {
if t.cat == valueT {
return &itype{cat: valueT, rtype: t.rtype.Elem()}
}
return t.val
}
func copyDefined(m map[string]*itype) map[string]*itype {
n := make(map[string]*itype, len(m))
for k, v := range m {