Repository: goplus/gop
Pull request #1767 (2024-02-22 01:22:13)

cl: forPhraseStmt scope

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/goplus/gop/pull/1767?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`4d49c6f`)](https://app.codecov.io/gh/goplus/gop/commit/4d49c6f78c2a8a2cafee1b0fb0785d153bf5e3ac?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) 92.19% compared to head [(`3cf7138`)](https://app.codecov.io/gh/goplus/gop/pull/1767?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus) 92.85%.
> Report is 3 commits behind head on main.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #1767      +/-   ##
==========================================
+ Coverage   92.19%   92.85%   +0.65%     
==========================================
  Files          40       39       -1     
  Lines       10985     9039    -1946     
==========================================
- Hits        10128     8393    -1735     
+ Misses        694      483     -211     
  Partials      163      163              
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/goplus/gop/pull/1767?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goplus).

xushiwei: please fix conflict

Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 84a51673..c22a7735 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -157,6 +157,7 @@ type Recorder interface {
 	//     *ast.CommClause
 	//     *ast.ForStmt
 	//     *ast.RangeStmt
+	//     *ast.ForPhraseStmt
 	//
 	Scope(ast.Node, *types.Scope)
 }
diff --git a/cl/stmt.go b/cl/stmt.go
index 4714925c..359f5481 100644
--- a/cl/stmt.go
+++ b/cl/stmt.go
@@ -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)
diff --git a/x/typesutil/gopinfo.go b/x/typesutil/gopinfo.go
index 39f67b38..c3b2e896 100644
--- a/x/typesutil/gopinfo.go
+++ b/x/typesutil/gopinfo.go
@@ -121,6 +121,7 @@ type Info struct {
 	//     *ast.CommClause
 	//     *ast.ForStmt
 	//     *ast.RangeStmt
+	//     *ast.ForPhraseStmt
 	//
 	Scopes map[ast.Node]*types.Scope
 


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index decca2b5..d4b8a206 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -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)
+		}
+	}
+}
+`)
+}
diff --git a/x/typesutil/info_test.go b/x/typesutil/info_test.go
index bd1d4584..17e59a56 100644
--- a/x/typesutil/info_test.go
+++ b/x/typesutil/info_test.go
@@ -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 {
@@ -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)
 			}