interp: improve method resolution for embedded interfaces

The function `getMethodByName()` is now able to look for
embedded `valueInterface` field for matching methods in interface
struct fields.
    
Fixes #1439 and #1427.
This commit is contained in:
Marc Vertes
2022-08-17 18:14:10 +02:00
committed by GitHub
parent b2aa636ea0
commit ab869c8d20
2 changed files with 49 additions and 0 deletions

25
_test/issue-1439.go Normal file
View File

@@ -0,0 +1,25 @@
package main
type Transformer interface {
Reset()
}
type Encoder struct {
Transformer
}
type nop struct{}
func (nop) Reset() { println("Reset") }
func f(e Transformer) {
e.Reset()
}
func main() {
e := Encoder{Transformer: nop{}}
f(e)
}
// Output:
// Reset

View File

@@ -1950,10 +1950,12 @@ func getMethodByName(n *node) {
}
val = v
}
if met := val.value.MethodByName(name); met.IsValid() {
getFrame(f, l).data[i] = met
return next
}
typ := val.node.typ
if typ.node == nil && typ.cat == valueT {
// happens with a var of empty interface type, that has value of concrete type
@@ -1963,10 +1965,32 @@ func getMethodByName(n *node) {
}
return next
}
m, li := typ.lookupMethod(name)
// Try harder to find a matching embedded valueInterface.
// TODO (marc): make sure it works for arbitrary depth and breadth.
if m == nil && isStruct(val.node.typ) {
v := val.value
for v.Type().Kind() == reflect.Ptr {
v = v.Elem()
}
nf := v.NumField()
for i := 0; i < nf; i++ {
var ok bool
if val, ok = v.Field(i).Interface().(valueInterface); !ok {
continue
}
if m, li = val.node.typ.lookupMethod(name); m != nil {
break
}
}
}
if m == nil {
panic(n.cfgErrorf("method not found: %s", name))
}
fr := f.clone(!fork)
nod := *m
nod.val = &nod