Skip to content

Commit 6b8c94e

Browse files
authored
interp: check that send operate on channel value
Not performing this check was leading to a panic at run-time. It now fails early with a compile error. Fixes #1453.
1 parent 143e4a4 commit 6b8c94e

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

interp/cfg.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
960960
}
961961
wireChild(n)
962962

963-
case declStmt, exprStmt, sendStmt:
963+
case sendStmt:
964+
if !isChan(n.child[0].typ) {
965+
err = n.cfgErrorf("invalid operation: cannot send to non-channel %s", n.child[0].typ.id())
966+
break
967+
}
968+
fallthrough
969+
970+
case declStmt, exprStmt:
964971
wireChild(n)
965972
l := n.lastChild()
966973
n.findex = l.findex

interp/interp_eval_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ func TestEvalChan(t *testing.T) {
642642
return ok && msg == "ping"
643643
})()`, res: "true",
644644
},
645+
{src: `a :=5; a <- 4`, err: "cannot send to non-channel int"},
646+
{src: `a :=5; b := <-a`, err: "cannot receive from non-channel int"},
645647
})
646648
}
647649

0 commit comments

Comments
 (0)