From dc3ad28984b8355c91a50d33c09d73fa4c8abf49 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 22 Feb 2025 08:54:03 +0800 Subject: [PATCH 1/2] #2107 cl: extend compileSendStmt --- cl/_testgop/{append => append1}/in.gop | 0 cl/_testgop/{append => append1}/out.go | 0 cl/_testgop/append2/in.gop | 7 +++++++ cl/_testgop/append2/out.go | 12 ++++++++++++ cl/stmt.go | 11 +++++++++-- 5 files changed, 28 insertions(+), 2 deletions(-) rename cl/_testgop/{append => append1}/in.gop (100%) rename cl/_testgop/{append => append1}/out.go (100%) create mode 100644 cl/_testgop/append2/in.gop create mode 100644 cl/_testgop/append2/out.go diff --git a/cl/_testgop/append/in.gop b/cl/_testgop/append1/in.gop similarity index 100% rename from cl/_testgop/append/in.gop rename to cl/_testgop/append1/in.gop diff --git a/cl/_testgop/append/out.go b/cl/_testgop/append1/out.go similarity index 100% rename from cl/_testgop/append/out.go rename to cl/_testgop/append1/out.go diff --git a/cl/_testgop/append2/in.gop b/cl/_testgop/append2/in.gop new file mode 100644 index 000000000..39d9a2b9f --- /dev/null +++ b/cl/_testgop/append2/in.gop @@ -0,0 +1,7 @@ +a := [1, 2, 3] +a <- 4, 5 +echo a + +b := [7, 8] +a <- b... +echo a diff --git a/cl/_testgop/append2/out.go b/cl/_testgop/append2/out.go new file mode 100644 index 000000000..238884c87 --- /dev/null +++ b/cl/_testgop/append2/out.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main() { + a := []int{1, 2, 3} + a = append(a, 4, 5) + fmt.Println(a) + b := []int{7, 8} + a = append(a, b...) + fmt.Println(a) +} diff --git a/cl/stmt.go b/cl/stmt.go index b2cc98176..48d92bb09 100644 --- a/cl/stmt.go +++ b/cl/stmt.go @@ -274,7 +274,11 @@ func compileSendStmt(ctx *blockCtx, expr *ast.SendStmt) { for _, v := range vals { compileExpr(ctx, v) } - cb.CallWith(len(vals)+1, 0, expr).AssignWith(1, 1, expr) + flags := gogen.InstrFlags(0) + if expr.Ellipsis != 0 { // a = append(a, b...) + flags |= gogen.InstrFlagEllipsis + } + cb.CallWith(len(vals)+1, flags, expr).AssignWith(1, 1, expr) return } goto normal @@ -282,7 +286,10 @@ func compileSendStmt(ctx *blockCtx, expr *ast.SendStmt) { compileExpr(ctx, ch) normal: - compileExpr(ctx, vals[0]) // TODO(xsw): issue #2107 + if len(vals) != 1 || expr.Ellipsis != 0 { + panic(ctx.newCodeError(vals[0].End(), "can't send multiple values to a channel")) + } + compileExpr(ctx, vals[0]) ctx.cb.Send() } From 1fe01a347d9e003f2d5df299b3799f675eb1bd24 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sat, 22 Feb 2025 08:58:44 +0800 Subject: [PATCH 2/2] TestErrSendStmt --- cl/error_msg_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cl/error_msg_test.go b/cl/error_msg_test.go index 6341a7004..a9553ee39 100644 --- a/cl/error_msg_test.go +++ b/cl/error_msg_test.go @@ -37,6 +37,13 @@ func codeErrorTestAst(t *testing.T, pkgname, filename, msg, src string) { cltest.ErrorAst(t, pkgname, filename, msg, src) } +func TestErrSendStmt(t *testing.T) { + codeErrorTest(t, `bar.gop:3:8: can't send multiple values to a channel`, ` + var a chan int + a <- 1, 2 +`) +} + func TestErrVargCommand(t *testing.T) { codeErrorTest(t, `bar.gop:5:1: not enough arguments in call to Ls have ()