compiler: allow to use type conversion in range

This commit is contained in:
Evgenii Stratonikov 2020-09-02 15:35:20 +03:00
parent 3af7ce8c6b
commit 74dda0ac66
2 changed files with 19 additions and 0 deletions

View file

@ -737,6 +737,20 @@ func TestForLoopRangeMap(t *testing.T) {
eval(t, src, big.NewInt(42))
}
func TestForLoopRangeTypeConversion(t *testing.T) {
src := `package foo
type intArr []int
func Main() int {
a := []int{1, 2, 3}
s := 0
for _, v := range intArr(a) {
s += v
}
return s
}`
eval(t, src, big.NewInt(6))
}
func TestForLoopComplexConditions(t *testing.T) {
src := `
package foo

View file

@ -102,6 +102,11 @@ func (c *funcScope) analyzeVoidCalls(node ast.Node) bool {
}
case *ast.BinaryExpr:
return false
case *ast.RangeStmt:
ce, ok := n.X.(*ast.CallExpr)
if ok {
c.voidCalls[ce] = false
}
case *ast.IfStmt:
// we can't just return `false`, because we still need to process body
ce, ok := n.Cond.(*ast.CallExpr)