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:
Marc Vertes
2022-10-04 12:00:08 +02:00
committed by GitHub
parent 143e4a4559
commit 6b8c94e6c4
2 changed files with 10 additions and 1 deletions

View File

@@ -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

View File

@@ -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"},
})
}