Skip to content

Commit

Permalink
interp: check that send operate on channel value
Browse files Browse the repository at this point in the history
Not performing this check was leading to a panic at run-time. It now fails early with a compile error.

Fixes #1453.
  • Loading branch information
mvertes authored Oct 4, 2022
1 parent 143e4a4 commit 6b8c94e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
})
}

Expand Down

0 comments on commit 6b8c94e

Please sign in to comment.