traefik / yaegi

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

Errors handling slice/array type parameters #1488

Closed vsivsi closed 1 year ago

vsivsi commented 1 year ago

The following program sample.go triggers an unexpected result

package main

import "fmt"

type vector interface {
    []int | [3]int
}

func sum[V vector](v V) (out int) {
    for i := 0; i < len(v); i++ {
        out += v[i]
    }
    return
}

func main() {
    va := [3]int{1, 2, 3}
    vs := []int{1, 2, 3}
    fmt.Println(sum[[3]int](va), sum[[]int](vs))
}

Expected result

6 6

Got

panic: runtime error: index out of range [0] with length 0 [recovered]
    panic: sample.go:19:14: CFG post-order panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2.1()
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/cfg.go:536 +0x78
panic({0x1a54180, 0xc000040060})
    /usr/local/Cellar/go/1.19.3/libexec/src/runtime/panic.go:890 +0x262
github.com/traefik/yaegi/interp.nodeType2(0xc0002e6000, 0xc0002ee090, 0xc000421320, {0xc000099390, 0x1, 0x2})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/type.go:432 +0x7c4f
github.com/traefik/yaegi/interp.nodeType2(0xc0002e6000, 0xc0002ee090, 0xc000420c60, {0xc000014388, 0x0, 0x1})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/type.go:739 +0x63e6
github.com/traefik/yaegi/interp.nodeType(0x8?, 0xc000099370?, 0x1?)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/type.go:399 +0x25
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func1(0xc000420900)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/cfg.go:407 +0x1a79
github.com/traefik/yaegi/interp.(*node).Walk(0xc000420900, 0xc00041ea90, 0xc00041ead8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:284 +0x34
github.com/traefik/yaegi/interp.(*Interpreter).cfg(0xc0002e6000, 0xc000420900, 0xc0002ee090, {0xc00003fa48, 0x4}, {0xc00003fa48, 0x4})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/cfg.go:62 +0x2b4
github.com/traefik/yaegi/interp.(*Interpreter).cfg.func2(0xc0004165a0)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/cfg.go:878 +0x9c08
github.com/traefik/yaegi/interp.(*node).Walk(0xc0004165a0, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:291 +0xad
github.com/traefik/yaegi/interp.(*node).Walk(0xc000416480, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc000416000, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc000415e60, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc000414a20, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc000414360, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*node).Walk(0xc0002f07e0, 0xc00041f990, 0xc00041f9d8)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:288 +0x75
github.com/traefik/yaegi/interp.(*Interpreter).cfg(0xc0002e6000, 0xc0002f07e0, 0xc0002ee1b0, {0xc00003fa48, 0x4}, {0xc00003fa48, 0x4})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/cfg.go:62 +0x2b4
github.com/traefik/yaegi/interp.(*Interpreter).CompileAST(0xc0002e6000, {0x1c8a7a8?, 0xc0002a4100?})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/program.go:92 +0x1ad
github.com/traefik/yaegi/interp.(*Interpreter).compileSrc(0xc0002e6000, {0xc0002ae480?, 0x0?}, {0x7ffeefbffc06?, 0xc0002ae480?}, 0x13?)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/program.go:59 +0xb8
github.com/traefik/yaegi/interp.(*Interpreter).eval(0xc0002e6000, {0xc0002ae480?, 0x113?}, {0x7ffeefbffc06?, 0xc0002ae240?}, 0x13?)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:552 +0x28
github.com/traefik/yaegi/interp.(*Interpreter).EvalPath(0xc0002e6000, {0x7ffeefbffc06, 0x7})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/interp/interp.go:510 +0xab
main.runFile(0x7ffeefbffc06?, {0x7ffeefbffc06, 0x7}, 0x0)
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/cmd/yaegi/run.go:153 +0xee
main.run({0xc00019a010?, 0x1, 0x1})
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/cmd/yaegi/run.go:116 +0xbec
main.main()
    /Users/vsi/go/pkg/mod/github.com/traefik/yaegi@v0.14.3/cmd/yaegi/yaegi.go:144 +0x2cb

Yaegi Version

v0.14.3

Additional Notes

No response