Skip to content

Commit 0a79069

Browse files
authored
fix: correct control flow graph for range init expression
The range init AST execution was skipped, and range could work only over variables or direct function calls. By setting the start node to the start of init and not init itself, we ensure that the init AST is always taken into account. Fixes #775.
1 parent 0c8f538 commit 0a79069

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

_test/issue-775.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http/httptest"
6+
)
7+
8+
func main() {
9+
recorder := httptest.NewRecorder()
10+
recorder.Header().Add("Foo", "Bar")
11+
12+
for key, value := range recorder.Header() {
13+
fmt.Println(key, value)
14+
}
15+
}
16+
17+
// Output:
18+
// Foo [Bar]

interp/cfg.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
13341334

13351335
case rangeStmt:
13361336
if sc.rangeChanType(n) != nil {
1337-
n.start = n.child[1] // Get chan
1337+
n.start = n.child[1].start // Get chan
13381338
n.child[1].tnext = n // then go to range function
13391339
n.tnext = n.child[2].start // then go to range body
13401340
n.child[2].tnext = n // then body go to range function (loop)
@@ -1346,7 +1346,7 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
13461346
} else {
13471347
k, o, body = n.child[0], n.child[1], n.child[2]
13481348
}
1349-
n.start = o // Get array or map object
1349+
n.start = o.start // Get array or map object
13501350
o.tnext = k.start // then go to iterator init
13511351
k.tnext = n // then go to range function
13521352
n.tnext = body.start // then go to range body

0 commit comments

Comments
 (0)