fix: automatic type conversion for untyped arg of append (#177)
This commit is contained in:
committed by
Ludovic Fernandez
parent
5cdcf61e0e
commit
c6c7f8cea6
14
_test/a21.go
Normal file
14
_test/a21.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
a := []byte("hello")
|
||||
fmt.Println(a)
|
||||
a = append(a, '=')
|
||||
fmt.Println(a)
|
||||
}
|
||||
|
||||
// Output:
|
||||
// [104 101 108 108 111]
|
||||
// [104 101 108 108 111 61]
|
||||
@@ -1406,7 +1406,11 @@ func _append(n *Node) {
|
||||
l := len(args)
|
||||
values := make([]func(*Frame) reflect.Value, l)
|
||||
for i, arg := range args {
|
||||
values[i] = genValue(arg)
|
||||
if arg.typ.untyped {
|
||||
values[i] = genValueAs(arg, n.child[1].typ.TypeOf().Elem())
|
||||
} else {
|
||||
values[i] = genValue(arg)
|
||||
}
|
||||
}
|
||||
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
@@ -1419,6 +1423,9 @@ func _append(n *Node) {
|
||||
}
|
||||
} else {
|
||||
value0 := genValue(n.child[2])
|
||||
if n.child[2].typ.untyped {
|
||||
value0 = genValueAs(n.child[2], n.child[1].typ.TypeOf().Elem())
|
||||
}
|
||||
|
||||
n.exec = func(f *Frame) Builtin {
|
||||
dest(f).Set(reflect.Append(value(f), value0(f)))
|
||||
|
||||
@@ -39,6 +39,13 @@ func genValueRecv(n *Node) func(*Frame) reflect.Value {
|
||||
}
|
||||
}
|
||||
|
||||
func genValueAs(n *Node, t reflect.Type) func(*Frame) reflect.Value {
|
||||
v := genValue(n)
|
||||
return func(f *Frame) reflect.Value {
|
||||
return v(f).Convert(t)
|
||||
}
|
||||
}
|
||||
|
||||
func genValue(n *Node) func(*Frame) reflect.Value {
|
||||
switch n.kind {
|
||||
case BasicLit, FuncDecl:
|
||||
|
||||
Reference in New Issue
Block a user