Skip to content

Commit 2e88083

Browse files
authored
interp: fix default comm clause in select
Do not attempt to init a non-existent channel setting when in default communication clause in select. Fixes #1442.
1 parent 79747f3 commit 2e88083

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

_test/issue-1442.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"context"
5+
)
6+
7+
func main() {
8+
ctx, _ := context.WithCancel(context.Background())
9+
ch := make(chan string, 20)
10+
defer close(ch)
11+
12+
go func(ctx context.Context, ch <-chan string) {
13+
for {
14+
select {
15+
case <-ctx.Done():
16+
return
17+
case tmp := <-ch:
18+
_ = tmp
19+
}
20+
}
21+
}(ctx, ch)
22+
23+
for _, i := range "abcdef" {
24+
for _, j := range "0123456789" {
25+
// i, j := "a", "0"
26+
for _, k := range "ABCDEF" {
27+
select {
28+
case <-ctx.Done():
29+
return
30+
default:
31+
tmp := string(i) + string(j) + string(k)
32+
ch <- tmp
33+
}
34+
}
35+
}
36+
}
37+
return
38+
}
39+
40+
// Output:
41+
//

_test/select1.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ func main() {
1010
c2 := make(chan string)
1111

1212
go func() {
13-
time.Sleep(1e7)
13+
time.Sleep(1e8)
1414
c1 <- "one"
1515
}()
1616
go func() {
17-
time.Sleep(2e7)
17+
time.Sleep(2e8)
1818
c2 <- "two"
1919
}()
2020

_test/select14.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
)
77

88
const (
9-
period = 100 * time.Millisecond
10-
precision = 7 * time.Millisecond
9+
period = 300 * time.Millisecond
10+
precision = 30 * time.Millisecond
1111
)
1212

1313
func main() {

interp/cfg.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,9 +1920,13 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
19201920
wireChild(n)
19211921
// Move action to block statement, so select node can be an exit point.
19221922
n.child[0].gen = _select
1923-
// Chain channel init actions in commClauses prior to invoke select.
1923+
// Chain channel init actions in commClauses prior to invoking select.
19241924
var cur *node
19251925
for _, c := range n.child[0].child {
1926+
if c.kind == commClauseDefault {
1927+
// No channel init in this case.
1928+
continue
1929+
}
19261930
var an, pn *node // channel init action nodes
19271931
if len(c.child) > 0 {
19281932
switch c0 := c.child[0]; {

0 commit comments

Comments
 (0)