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

interp: fix default comm clause in select #1450

Merged
merged 3 commits into from
Sep 12, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions _test/issue-1442.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
)

func main() {
ctx, _ := context.WithCancel(context.Background())
ch := make(chan string, 20)
defer close(ch)

go func(ctx context.Context, ch <-chan string) {
for {
select {
case <-ctx.Done():
return
case tmp := <-ch:
_ = tmp
}
}
}(ctx, ch)

for _, i := range "abcdef" {
for _, j := range "0123456789" {
// i, j := "a", "0"
for _, k := range "ABCDEF" {
select {
case <-ctx.Done():
return
default:
tmp := string(i) + string(j) + string(k)
ch <- tmp
}
}
}
}
return
}

// Output:
//
4 changes: 2 additions & 2 deletions _test/select1.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ func main() {
c2 := make(chan string)

go func() {
time.Sleep(1e7)
time.Sleep(1e8)
c1 <- "one"
}()
go func() {
time.Sleep(2e7)
time.Sleep(2e8)
c2 <- "two"
}()

Expand Down
4 changes: 2 additions & 2 deletions _test/select14.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

const (
period = 100 * time.Millisecond
precision = 7 * time.Millisecond
period = 300 * time.Millisecond
precision = 30 * time.Millisecond
)

func main() {
Expand Down
6 changes: 5 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1920,9 +1920,13 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
wireChild(n)
// Move action to block statement, so select node can be an exit point.
n.child[0].gen = _select
// Chain channel init actions in commClauses prior to invoke select.
// Chain channel init actions in commClauses prior to invoking select.
var cur *node
for _, c := range n.child[0].child {
if c.kind == commClauseDefault {
// No channel init in this case.
continue
}
var an, pn *node // channel init action nodes
if len(c.child) > 0 {
switch c0 := c.child[0]; {
Expand Down