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: record lambda scope #1772

Merged
merged 1 commit into from
Feb 23, 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
6 changes: 6 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,9 @@ func compileLambdaExpr(ctx *blockCtx, v *ast.LambdaExpr, sig *types.Signature) e
for _, v := range v.Rhs {
compileExpr(ctx, v)
}
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, ctx.cb.Scope())
}
ctx.cb.Return(len(v.Rhs)).End(v)
return nil
}
Expand All @@ -877,6 +880,9 @@ func compileLambdaExpr2(ctx *blockCtx, v *ast.LambdaExpr2, sig *types.Signature)
defNames(ctx, v.Lhs, cb.Scope())
}
compileStmts(ctx, v.Body.List)
if rec := ctx.recorder(); rec != nil {
rec.Scope(v, ctx.cb.Scope())
}
cb.End(v)
ctx.cb.SetComments(comments, once)
return nil
Expand Down
13 changes: 13 additions & 0 deletions x/typesutil/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,15 @@ func TestScopesInfo(t *testing.T) {
{`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",
}},
{`package p23; func test(fn func(int)int){};func _(){test(func(x int) int { y := x*x; return y } ) }`, []string{
"file:test", "func:fn", "func:", "func:x y",
}},
{`package p24; func test(fn func(int)int){};func _(){test( x => x*x );}`, []string{
"file:test", "func:fn", "func:", "lambda:x",
}},
{`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",
}},
}

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