yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

cannot use type func(*time.Location) string as type func() string in struct literal

Open bailsman opened this issue 3 years ago • 1 comments

The following program sample.go triggers an unexpected result

package main

import (
        "time"
        "fmt"
)

type Foo struct {
        Loc func() string
}

func main() {
    f := Foo{(&time.Location{}).String}
    fmt.Println(f.Loc())
}

Expected result

$ go run ./sample.go
// no output

Got

$ yaegi ./sample.go
run: ./sample.go:13:14: cannot use type func(*time.Location) string as type func() string in struct literal

Yaegi Version

fbee2baf9ddfad88f1d9b8c32fc3ca492e5c1ea5

Additional Notes

Bit of a contrived example but I wanted something that works with the standard library. (I originally hit this with something I had compiled in using yaegi extract.)

With the following change, the problem no longer occurs:

diff --git a/interp/cfg.go b/interp/cfg.go
index b2e36ad..7989481 100644
--- a/interp/cfg.go
+++ b/interp/cfg.go
@@ -1647,7 +1647,7 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
                                        } else if method, ok := reflect.PtrTo(n.typ.val.rtype).MethodByName(n.child[1].ident); ok {
                                                n.val = method.Index
                                                n.gen = getIndexBinMethod
-                                               n.typ = valueTOf(method.Type, withRecv(valueTOf(reflect.PtrTo(n.typ.val.rtype), isBinMethod())))
+                                               n.typ = valueTOf(method.Type, isBinMethod(), withRecv(valueTOf(reflect.PtrTo(n.typ.val.rtype))))
                                                n.recv = &receiver{node: n.child[0]}
                                                n.action = aGetMethod
                                        } else if field, ok := n.typ.val.rtype.FieldByName(n.child[1].ident); ok {

But note that this is more trial and error than me really understanding what's going on.

bailsman avatar Dec 24 '21 22:12 bailsman