Skip to content

Commit

Permalink
cl: types record ast.ForPhrase
Browse files Browse the repository at this point in the history
  • Loading branch information
visualfc committed Feb 23, 2024
1 parent 010698b commit 8261fc2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ type Recorder interface {
// *ast.ForStmt
// *ast.RangeStmt
// *ast.ForPhraseStmt
// *ast.ForPhrase
// *ast.LambdaExpr
// *ast.LambdaExpr2
//
Scope(ast.Node, *types.Scope)
}
Expand Down
3 changes: 3 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,9 @@ func compileComprehensionExpr(ctx *blockCtx, v *ast.ComprehensionExpr, twoValue
compileExpr(ctx, forStmt.X)
cb.RangeAssignThen(forStmt.TokPos)
defNames(ctx, defineNames, cb.Scope())
if rec := ctx.recorder(); rec != nil {
rec.Scope(forStmt, cb.Scope())
}
if forStmt.Cond != nil {
cb.If()
if forStmt.Init != nil {
Expand Down
3 changes: 3 additions & 0 deletions x/typesutil/gopinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ type Info struct {
// *ast.ForStmt
// *ast.RangeStmt
// *ast.ForPhraseStmt
// *ast.ForPhrase
// *ast.LambdaExpr
// *ast.LambdaExpr2
//
Scopes map[ast.Node]*types.Scope

Expand Down
20 changes: 20 additions & 0 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,24 @@ func TestScopesInfo(t *testing.T) {
{`package p25; func test(fn func(int)int){};func _(){test( x => { y := x*x; return y } ) }`, []string{
"file:test", "func:fn", "func:", "lambda:x y",
}},
{`package p26; func _(){ b := {for x <- ["1", "3", "5", "7", "11"], x == "5"}; _ = b }`, []string{
"file:", "func:b", "for phrase:x",
}},
{`package p27; func _(){ b, ok := {i for i, x <- ["1", "3", "5", "7", "11"], x == "5"}; _ = b; _ = ok }`, []string{
"file:", "func:b ok", "for phrase:i x",
}},
{`package p28; func _(){ a := [x*x for x <- [1, 3.4, 5] if x > 2 ]; _ = a }`, []string{
"file:", "func:a", "for phrase:x",
}},
{`package p29; func _(){ arr := [1, 2, 3, 4.1, 5, 6];x := [[a, b] for a <- arr, a < b for b <- arr, b > 2]; _ = x }`, []string{
"file:", "func:arr x", "for phrase:b", "for phrase:a",
}},
{`package p30; func _(){ y := {x: i for i, x <- ["1", "3", "5", "7", "11"]}; _ = y }`, []string{
"file:", "func:y", "for phrase:i x",
}},
{`package p31; func _(){ z := {v: k for k, v <- {"Hello": 1, "Hi": 3, "xsw": 5, "Go+": 7}, v > 3}; _ = z }`, []string{
"file:", "func:z", "for phrase:k v",
}},
}

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

0 comments on commit 8261fc2

Please sign in to comment.