Skip to content

Commit

Permalink
cl: forPhraseStmt scope
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Feb 22, 2024
1 parent 1eaa06d commit 0dbbd27
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type Recorder interface {
// *ast.CommClause
// *ast.ForStmt
// *ast.RangeStmt
// *ast.ForPhraseStmt
//
Scope(ast.Node, *types.Scope)
}
Expand Down
45 changes: 45 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4338,3 +4338,48 @@ func demo() {
}
`)
}

func TestForPhraseScope(t *testing.T) {
gopClTest(t, `sum := 0
for x <- [1, 3, 5, 7, 11, 13, 17] {
sum = sum + x
println x
x := 200
println x
}`, `package main
import "fmt"
func main() {
sum := 0
for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
sum = sum + x
fmt.Println(x)
x := 200
fmt.Println(x)
}
}
`)
gopClTest(t, `sum := 0
for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 {
sum = sum + x
println x
x := 200
println x
}`, `package main
import "fmt"
func main() {
sum := 0
for _, x := range []int{1, 3, 5, 7, 11, 13, 17} {
if x > 3 {
sum = sum + x
fmt.Println(x)
x := 200
fmt.Println(x)
}
}
}
`)
}
11 changes: 11 additions & 0 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,26 @@ func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
if len(defineNames) > 0 {
defNames(ctx, defineNames, cb.Scope())
}
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, cb.Scope())
}
if v.Cond != nil {
cb.If()
compileExpr(ctx, v.Cond)
cb.Then()
compileStmts(ctx, v.Body.List)
cb.SetComments(comments, once)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v.Body, cb.Scope())
}
cb.End()
} else {
cb.VBlock()
compileStmts(ctx, v.Body.List)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v.Body, cb.Scope())
}
cb.End(v.Body)
cb.SetComments(comments, once)
}
setBodyHandler(ctx)
Expand Down
1 change: 1 addition & 0 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ type Info struct {
// *ast.CommClause
// *ast.ForStmt
// *ast.RangeStmt
// *ast.ForPhraseStmt
//
Scopes map[ast.Node]*types.Scope

Expand Down
8 changes: 8 additions & 0 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,12 @@ func TestScopesInfo(t *testing.T) {
{`package p21; var s int; func _(a []int) { for i, x := range a { c := i; println(c) } }`, []string{
"file:", "func:a", "range:i x", "block:c",
}},
{`package p22; func _(){ sum := 0; for x <- [1, 3, 5, 7, 11, 13, 17], x > 3 { sum = sum + x; c := sum; _ = c } }`, []string{
"file:", "func:sum", "for phrase:x", "block:c",
}},
{`package p23; func _(){ sum := 0; for x <- [1, 3, 5, 7, 11, 13, 17] { sum = sum + x; c := sum; _ = c } }`, []string{
"file:", "func:sum", "for phrase:x", "block:c",
}},
}

for _, test := range tests {
Expand Down Expand Up @@ -1781,6 +1787,8 @@ func TestScopesInfo(t *testing.T) {
kind = "for"
case *ast.RangeStmt:
kind = "range"
case *ast.ForPhraseStmt:
kind = "for phrase"
default:
kind = fmt.Sprintf("<unknown node kind> %T", node)
}
Expand Down

0 comments on commit 0dbbd27

Please sign in to comment.