traefik / yaegi

Yaegi is Another Elegant Go Interpreter
https://pkg.go.dev/github.com/traefik/yaegi
Apache License 2.0
6.82k stars 342 forks source link

Doesn't work well with generic methods #1573

Open zbysir opened 1 year ago

zbysir commented 1 year ago

The following program sample.go triggers an unexpected result

package main

type B[T any] struct {
    data T
}

func (b *B[T]) SetData(data T) {
    b.clearData()
}

func (b *B[T]) clearData() {
}

func main() {
    _ = B[string]{}
    println("PASS")
}

Expected result

PASS

Got

run: ./sample.go:8:2: undefined selector: clearData

Yaegi Version

0.15.1

Additional Notes

The key to reproduce this problem is to call another generic method(clearData) in a generic method(SetData).

The problem is probably around this code https://github.com/traefik/yaegi/blob/75e5f99bc5761d8e920e726f75932a31b6f06669/interp/type.go#L1172, but it's a very complicated problem, I don't have the ability to fix it, so I can only ask for help.

By the way, works fine if clearData is declared before SetData

type B[T any] struct {
    data T
}

func (b *B[T]) clearData() {
}

func (b *B[T]) SetData(data T) {
    b.clearData()
}
zbysir commented 1 year ago

Is it more appropriate to generate a generic type in the gta step? Is it possible to solve this problem?

zbysir commented 1 year ago

Yaegi is an amazing project, but there are still some problems that need to be fixed. If someone also encounters this problem, maybe we can challenge it together. I am too limited by myself.