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

fix #1228, lamda support in return statement #1230

Merged
merged 3 commits into from
May 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 22 additions & 2 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,7 @@ func main() {
`)
}

func _TestLambdaExpr3(t *testing.T) {
func TestLambdaExpr3(t *testing.T) {
gopClTest(t, `
func intSeq() func() int {
i := 0
Expand All @@ -3234,7 +3234,27 @@ func intSeq() func() int {
return i
}
}
`, `
`, `package main

func intSeq() func() int {
i := 0
return func() int {
i++
return i
}
}
`)
gopClTest(t, `
func intDouble() func(int) int {
return i => i*2
}
`, `package main

func intDouble() func(int) int {
return func(i int) int {
return i * 2
}
}
`)
}

Expand Down
20 changes: 20 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ var foo, foo1 func() = nil, => {}
"./bar.gop:3:15: lambda unsupport multiple assignment", `
var foo func()
_, foo = nil, => {}
`)
codeErrorTest(t,
"./bar.gop:4:9: cannot use lambda expression as type int in return statement", `
func intSeq() int {
i := 0
return => {
i++
return i
}
}
`)
codeErrorTest(t,
"./bar.gop:6:10: cannot use i (type int) as type string in return argument", `
func intSeq() func() string {
i := 0
return => {
i++
return i
}
}
`)
}

Expand Down
13 changes: 12 additions & 1 deletion cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,18 @@ func compileReturnStmt(ctx *blockCtx, expr *ast.ReturnStmt) {
twoValue = (results.Len() == 2)
}
}
compileExpr(ctx, ret, twoValue)
switch v := ret.(type) {
case *ast.LambdaExpr, *ast.LambdaExpr2:
rtyp := ctx.cb.Func().Type().(*types.Signature).Results().At(i).Type()
sig, ok := rtyp.(*types.Signature)
if !ok {
panic(ctx.newCodeErrorf(
ret.Pos(), "cannot use lambda expression as type %v in return statement", rtyp))
}
compileLambda(ctx, v, sig)
default:
compileExpr(ctx, ret, twoValue)
}
}
}
ctx.cb.Return(len(expr.Results), expr)
Expand Down