Skip to content

Commit 0dbbd27

Browse files
committed
cl: forPhraseStmt scope
1 parent 1eaa06d commit 0dbbd27

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

cl/compile.go

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ type Recorder interface {
157157
// *ast.CommClause
158158
// *ast.ForStmt
159159
// *ast.RangeStmt
160+
// *ast.ForPhraseStmt
160161
//
161162
Scope(ast.Node, *types.Scope)
162163
}

cl/compile_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -4338,3 +4338,48 @@ func demo() {
43384338
}
43394339
`)
43404340
}
4341+
4342+
func TestForPhraseScope(t *testing.T) {
4343+
gopClTest(t, `sum := 0
4344+
for x <- [1, 3, 5, 7, 11, 13, 17] {
4345+
sum = sum + x
4346+
println x
4347+
x := 200
4348+
println x
4349+
}`, `package main
4350+
4351+
import "fmt"
4352+
4353+
func main() {
4354+
sum := 0
4355+
for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
4356+
sum = sum + x
4357+
fmt.Println(x)
4358+
x := 200
4359+
fmt.Println(x)
4360+
}
4361+
}
4362+
`)
4363+
gopClTest(t, `sum := 0
4364+
for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
4365+
sum = sum + x
4366+
println x
4367+
x := 200
4368+
println x
4369+
}`, `package main
4370+
4371+
import "fmt"
4372+
4373+
func main() {
4374+
sum := 0
4375+
for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
4376+
if x > 3 {
4377+
sum = sum + x
4378+
fmt.Println(x)
4379+
x := 200
4380+
fmt.Println(x)
4381+
}
4382+
}
4383+
}
4384+
`)
4385+
}

cl/stmt.go

+11
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,26 @@ func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
417417
if len(defineNames) > 0 {
418418
defNames(ctx, defineNames, cb.Scope())
419419
}
420+
if rec := ctx.recorder(); rec != nil {
421+
rec.Scope(v, cb.Scope())
422+
}
420423
if v.Cond != nil {
421424
cb.If()
422425
compileExpr(ctx, v.Cond)
423426
cb.Then()
424427
compileStmts(ctx, v.Body.List)
425428
cb.SetComments(comments, once)
429+
if rec := ctx.recorder(); rec != nil {
430+
rec.Scope(v.Body, cb.Scope())
431+
}
426432
cb.End()
427433
} else {
434+
cb.VBlock()
428435
compileStmts(ctx, v.Body.List)
436+
if rec := ctx.recorder(); rec != nil {
437+
rec.Scope(v.Body, cb.Scope())
438+
}
439+
cb.End(v.Body)
429440
cb.SetComments(comments, once)
430441
}
431442
setBodyHandler(ctx)

x/typesutil/gopinfo.go

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ type Info struct {
121121
// *ast.CommClause
122122
// *ast.ForStmt
123123
// *ast.RangeStmt
124+
// *ast.ForPhraseStmt
124125
//
125126
Scopes map[ast.Node]*types.Scope
126127

x/typesutil/info_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,12 @@ func TestScopesInfo(t *testing.T) {
17431743
{`package p21; var s int; func _(a []int) { for i, x := range a { c := i; println(c) } }`, []string{
17441744
"file:", "func:a", "range:i x", "block:c",
17451745
}},
1746+
{`package p22; func _(){ sum := 0; for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 { sum = sum + x; c := sum; _ = c } }`, []string{
1747+
"file:", "func:sum", "for phrase:x", "block:c",
1748+
}},
1749+
{`package p23; func _(){ sum := 0; for x <- [1, 3, 5, 7, 11, 13, 17] { sum = sum + x; c := sum; _ = c } }`, []string{
1750+
"file:", "func:sum", "for phrase:x", "block:c",
1751+
}},
17461752
}
17471753

17481754
for _, test := range tests {
@@ -1781,6 +1787,8 @@ func TestScopesInfo(t *testing.T) {
17811787
kind = "for"
17821788
case *ast.RangeStmt:
17831789
kind = "range"
1790+
case *ast.ForPhraseStmt:
1791+
kind = "for phrase"
17841792
default:
17851793
kind = fmt.Sprintf("<unknown node kind> %T", node)
17861794
}

0 commit comments

Comments
 (0)