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

Fails to import and instantiate a certain generic struct #1577

Open motoki317 opened 1 year ago

motoki317 commented 1 year ago

The following program sample.go triggers an unexpected result

package main

import (
    "github.com/motoki317/sc"
)

func main() {
    _ = &sc.Cache[string, string]{}
}

Expected result

$ yaegi < sample.go
// no error, no output

Got

$ yaegi < sample.go
2:7: CFG post-order panic: /home/moto/go/src/github.com/motoki317/sc/cache.go:84:33: incomplete type Cache[string,string]
goroutine 31 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:24 +0x65
github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext.func1.1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:585 +0x99
panic({0xeae9c0, 0xc0006b5098})
        /usr/local/go/src/runtime/panic.go:884 +0x213
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2.1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:601 +0x78
panic({0xeae9c0, 0xc0006b5080})
        /usr/local/go/src/runtime/panic.go:890 +0x263
github.com/traefik/yaegi/interp.(*itype).frameType(0x20?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/type.go:2202 +0x1c5
github.com/traefik/yaegi/interp.(*scope).add(0xc000422bd0, 0xc00041dcc8?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/scope.go:210 +0x7b
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2(0xc00074f7c0)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:1408 +0x1645
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f7c0, 0xc000757d70, 0xc000757db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:294 +0xad
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f680, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f400, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f2c0, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074eb40, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074e8c0, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*Interpreter).cfg(0xc00018f680, 0xc00074e8c0, 0xc000422bd0, {0xc00012c9d8, 0x4}, {0xc00012c9d8, 0x4})
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:62 +0x2b4
github.com/traefik/yaegi/interp.(*Interpreter).CompileAST(0xc00018f680, {0x10a2d58?, 0xc000422ab0?})
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/program.go:97 +0x1ad
github.com/traefik/yaegi/interp.(*Interpreter).compileSrc(0xc00018f680, {0xc00003ffc0?, 0x0?}, {0x0?, 0x0?}, 0x0?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/program.go:64 +0xb8
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc00018f680, {0xc00003ffc0?, 0x0?}, {0x0?, 0x0?}, 0x0?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:556 +0x28
github.com/traefik/yaegi/interp.(*Interpreter).Eval(...)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:498
github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext.func1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:589 +0x8e
created by github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:580 +0x1b8

run: 2:7: CFG post-order panic: /home/moto/go/src/github.com/motoki317/sc/cache.go:84:33: incomplete type Cache[string,string]
goroutine 31 [running]:
runtime/debug.Stack()
        /usr/local/go/src/runtime/debug/stack.go:24 +0x65
github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext.func1.1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:585 +0x99
panic({0xeae9c0, 0xc0006b5098})
        /usr/local/go/src/runtime/panic.go:884 +0x213
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2.1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:601 +0x78
panic({0xeae9c0, 0xc0006b5080})
        /usr/local/go/src/runtime/panic.go:890 +0x263
github.com/traefik/yaegi/interp.(*itype).frameType(0x20?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/type.go:2202 +0x1c5
github.com/traefik/yaegi/interp.(*scope).add(0xc000422bd0, 0xc00041dcc8?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/scope.go:210 +0x7b
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2(0xc00074f7c0)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:1408 +0x1645
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f7c0, 0xc000757d70, 0xc000757db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:294 +0xad
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f680, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f400, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074f2c0, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074eb40, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc00074e8c0, 0xc000763d70, 0xc000763db8)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:291 +0x75
github.com/traefik/yaegi/interp.(*Interpreter).cfg(0xc00018f680, 0xc00074e8c0, 0xc000422bd0, {0xc00012c9d8, 0x4}, {0xc00012c9d8, 0x4})
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/cfg.go:62 +0x2b4
github.com/traefik/yaegi/interp.(*Interpreter).CompileAST(0xc00018f680, {0x10a2d58?, 0xc000422ab0?})
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/program.go:97 +0x1ad
github.com/traefik/yaegi/interp.(*Interpreter).compileSrc(0xc00018f680, {0xc00003ffc0?, 0x0?}, {0x0?, 0x0?}, 0x0?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/program.go:64 +0xb8
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc00018f680, {0xc00003ffc0?, 0x0?}, {0x0?, 0x0?}, 0x0?)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:556 +0x28
github.com/traefik/yaegi/interp.(*Interpreter).Eval(...)
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:498
github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext.func1()
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:589 +0x8e
created by github.com/traefik/yaegi/interp.(*Interpreter).EvalWithContext
        /home/moto/go/pkg/mod/github.com/traefik/yaegi@v0.15.1/interp/interp.go:580 +0x1b8

Yaegi Version

Additional Notes

I encountered this issue while trying to write a new traefik plugin. https://github.com/motoki317/traefik-swr/issues/1

I tried to prepare a minimum reproduction example, but this was the best I could do with my knowledge. I also searched for some possibly related issues with generics and "CFG post-order panic" error messages, but I couldn't determine if my issue was a new one or not. Hopefuly someone with more knowledge can look into this.

go.mod used in the sample program:

module playground

go 1.20

require github.com/motoki317/sc v1.6.0