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

#2107 cl: extend compileSendStmt #2113

Merged
merged 2 commits into from
Feb 22, 2025
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
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions cl/_testgop/append2/in.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a := [1, 2, 3]
a <- 4, 5
echo a

b := [7, 8]
a <- b...
echo a
12 changes: 12 additions & 0 deletions cl/_testgop/append2/out.go
Original file line number Diff line number Diff line change
@@ -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)
}
7 changes: 7 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
11 changes: 9 additions & 2 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,22 @@ 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
}
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()
}

Expand Down