Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cl: forPhraseStmt scope #1767

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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