fix: handle interfaces in composite sparce (#749)

Co-authored-by: Marc Vertes <mvertes@free.fr>
This commit is contained in:
Nicholas Wiersma
2020-07-06 15:41:27 +02:00
committed by GitHub
parent 2a70a71dc2
commit 9d4685deea
2 changed files with 26 additions and 2 deletions

19
_test/interface44.go Normal file
View File

@@ -0,0 +1,19 @@
package main
type S struct {
a int
}
func main() {
var i interface{} = S{a: 1}
s, ok := i.(S)
if !ok {
println("bad")
return
}
println(s.a)
}
// Output:
// 1

View File

@@ -2089,6 +2089,7 @@ func doCompositeSparse(n *node, hasType bool) {
if hasType {
child = n.child[1:]
}
destInterface := destType(n).cat == interfaceT
values := make(map[int]func(*frame) reflect.Value)
a, _ := n.typ.zero()
@@ -2110,9 +2111,13 @@ func doCompositeSparse(n *node, hasType bool) {
for i, v := range values {
a.Field(i).Set(v(f))
}
if d := value(f); d.Type().Kind() == reflect.Ptr {
d := value(f)
switch {
case d.Type().Kind() == reflect.Ptr:
d.Set(a.Addr())
} else {
case destInterface:
d.Set(reflect.ValueOf(valueInterface{n, a}))
default:
d.Set(a)
}
return next