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.
This commit is contained in:
@@ -960,7 +960,14 @@ func (interp *Interpreter) cfg(root *node, sc *scope, importPath, pkgName string
|
||||
}
|
||||
wireChild(n)
|
||||
|
||||
case declStmt, exprStmt, sendStmt:
|
||||
case sendStmt:
|
||||
if !isChan(n.child[0].typ) {
|
||||
err = n.cfgErrorf("invalid operation: cannot send to non-channel %s", n.child[0].typ.id())
|
||||
break
|
||||
}
|
||||
fallthrough
|
||||
|
||||
case declStmt, exprStmt:
|
||||
wireChild(n)
|
||||
l := n.lastChild()
|
||||
n.findex = l.findex
|
||||
|
||||
@@ -642,6 +642,8 @@ func TestEvalChan(t *testing.T) {
|
||||
return ok && msg == "ping"
|
||||
})()`, res: "true",
|
||||
},
|
||||
{src: `a :=5; a <- 4`, err: "cannot send to non-channel int"},
|
||||
{src: `a :=5; b := <-a`, err: "cannot receive from non-channel int"},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user