Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It panics when append functions into a slice of that type of function #1592

Open
PatriotBo opened this issue Sep 11, 2023 · 0 comments
Open

Comments

@PatriotBo
Copy link

The following program sample.go triggers an unexpected result

package main

import (
	"fmt"

	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

var funcs = `
package main

import "fmt"

func AppendSlice() {
	type funcType func()

	var fs []funcType
	fs = append(fs, a)

	for _, f := range fs {
		f()
	}
}

func a(){
	fmt.Println("execute func a")
}`

func main(){
	engine := interp.New(interp.Options{})
	// 引入标准库
	if err := engine.Use(stdlib.Symbols); err != nil {
		panic(err)
	}

	_, err := engine.Eval(funcs)
	if err != nil {
		fmt.Printf("evel funcs failed:%v \n", err)
		return
	}

	v, err := engine.Eval("main.AppendSlice")
	if err != nil {
		fmt.Printf("evel main.AppendSlice failed:%v \n", err)
		return
	}

	f, ok := v.Interface().(func())
	if !ok {
		fmt.Println("func not type of metadataudf.UserDefineFunc ")
		return
	}
	f()
}

Expected result

go run main.go

Got

7:2: panic
panic: reflect.Set: value of type *interp.node is not assignable to type func() [recovered]
        panic: reflect.Set: value of type *interp.node is not assignable to type func()

Yaegi Version

v0.15.1

Additional Notes

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant