yaegi icon indicating copy to clipboard operation
yaegi copied to clipboard

type assertion on an interface value from reflect

Open mvertes opened this issue 5 years ago • 1 comments

The following program sample.go triggers a panic:

package main

import "reflect"

type I interface {
    Foo()
}
    
type T struct {
    Name string
}

func (t T) Foo() { println("In Foo", t.Name) }

func main() {
    t := T{"test"} 
    v := reflect.ValueOf(t)

    if i, ok := v.Interface().(I); ok {
        i.Foo()
    }
}

Expected result:

$ go run ./sample.go
In Foo test

Got:

$ yaegi ./sample.go
./sample.go:16:7: panic
reflect.Set: value of type struct { Name string } is not assignable to type interp.valueInterface
goroutine 1 [running]:
runtime/debug.Stack(0x1, 0xc00031e000, 0x40)
        /usr/lib/go/src/runtime/debug/stack.go:24 +0x9d
github.com/containous/yaegi/interp.(*Interpreter).Eval.func1(0xc00012bdf0)
        /home/marc/go/src/github.com/containous/yaegi/interp/interp.go:328 +0xbc
panic(0xc9a400, 0xc000206c40)
        /usr/lib/go/src/runtime/panic.go:969 +0x166
github.com/containous/yaegi/interp.runCfg.func1(0xc0002f20a0, 0xc000306e00)
        /home/marc/go/src/github.com/containous/yaegi/interp/run.go:114 +0x20c
panic(0xc9a400, 0xc000206c40)
        /usr/lib/go/src/runtime/panic.go:969 +0x166
reflect.Value.assignTo(0xc0002003c0, 0xc000206be0, 0x99, 0xdcca73, 0xb, 0xd1dca0, 0x0, 0xc00031a090, 0xc0002003c0, 0xc000206be0)
        /usr/lib/go/src/reflect/value.go:2403 +0x426
reflect.Value.Set(0xd1dca0, 0xc000204cc0, 0x199, 0xc0002003c0, 0xc000206be0, 0x99)
        /usr/lib/go/src/reflect/value.go:1532 +0xbd
github.com/containous/yaegi/interp.typeAssert2.func1(0xc0002f20a0, 0xc0002fe600)
        /home/marc/go/src/github.com/containous/yaegi/interp/run.go:198 +0x13e
github.com/containous/yaegi/interp.runCfg(0xc000306e00, 0xc0002f20a0)
        /home/marc/go/src/github.com/containous/yaegi/interp/run.go:120 +0x6d
github.com/containous/yaegi/interp.(*Interpreter).run(0xc0002ee000, 0xc000306600, 0xc0002f2000)
        /home/marc/go/src/github.com/containous/yaegi/interp/run.go:98 +0x239
github.com/containous/yaegi/interp.(*Interpreter).Eval(0xc0002ee000, 0xc0002fa000, 0xf9, 0x0, 0x0, 0x0, 0x0, 0x0)
        /home/marc/go/src/github.com/containous/yaegi/interp/interp.go:404 +0x52b
main.main()
        /home/marc/go/src/github.com/containous/yaegi/cmd/yaegi/yaegi.go:143 +0x553

mvertes avatar Jun 04 '20 13:06 mvertes

Well at least with the recent improvements to type assertions, this sample does not panic anymore. Instead the type assertion status is (incorrectly) set to false, and so i.Foo() is never run.

mpl avatar Dec 08 '20 10:12 mpl