fix: assign a literal composite to an interface object

This commit is contained in:
Marc Vertes
2019-11-19 14:34:05 +01:00
committed by Traefiker Bot
parent 773147ef71
commit c5ec5e492f
2 changed files with 33 additions and 2 deletions

18
_test/interface12.go Normal file
View File

@@ -0,0 +1,18 @@
package main
type I1 interface {
Truc()
}
type T1 struct{}
func (T1) Truc() { println("in T1 truc") }
var x I1 = T1{}
func main() {
x.Truc()
}
// Output:
// in T1 truc

View File

@@ -1495,6 +1495,15 @@ func compositeBinStruct(n *node) {
}
}
func destType(n *node) *itype {
switch n.anc.kind {
case assignStmt, defineStmt:
return n.anc.child[0].typ
default:
return n.typ
}
}
// compositeLit creates and populates a struct object
func compositeLit(n *node) {
value := valueGenerator(n, n.findex)
@@ -1503,6 +1512,7 @@ func compositeLit(n *node) {
if !n.typ.untyped {
child = n.child[1:]
}
destInterface := destType(n).cat == interfaceT
values := make([]func(*frame) reflect.Value, len(child))
for i, c := range child {
@@ -1519,9 +1529,12 @@ func compositeLit(n *node) {
for i, v := range values {
a.Field(i).Set(v(f))
}
if d := value(f); d.Type().Kind() == reflect.Ptr {
switch d := value(f); {
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