Skip to content

Commit

Permalink
fix: assign a literal composite to an interface object
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and traefiker committed Nov 19, 2019
1 parent 773147e commit c5ec5e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
18 changes: 18 additions & 0 deletions _test/interface12.go
Original file line number Diff line number Diff line change
@@ -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
17 changes: 15 additions & 2 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit c5ec5e4

Please sign in to comment.