fix: assign composite literal by reference

This commit is contained in:
Marc Vertes
2020-04-16 12:24:04 +02:00
committed by GitHub
parent 50a34fd2a7
commit 29e1777d82
2 changed files with 18 additions and 1 deletions

16
_test/composite8.go Normal file
View File

@@ -0,0 +1,16 @@
package main
type T struct{ I int }
func main() {
t := []*T{}
s := []int{1, 2}
for _, e := range s {
x := &T{e}
t = append(t, x)
}
println(t[0].I, t[1].I)
}
// Output:
// 1 2

View File

@@ -1732,6 +1732,7 @@ func doCompositeLit(n *node, hasType bool) {
}
}
i := n.findex
n.exec = func(f *frame) bltn {
a := reflect.New(n.typ.TypeOf()).Elem()
for i, v := range values {
@@ -1743,7 +1744,7 @@ func doCompositeLit(n *node, hasType bool) {
case destInterface:
d.Set(reflect.ValueOf(valueInterface{n, a}))
default:
d.Set(a)
f.data[i] = a
}
return next
}