From 43fbc68f1ed2eaa7d575c9968c1a957bced83942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Jen=C3=9F?= Date: Wed, 27 Oct 2021 16:53:50 +0200 Subject: [PATCH] checker: disallow `<-` (channel push) on right-hand side of assignment (fix #12309) (#12321) --- vlib/v/checker/checker.v | 6 ++++++ vlib/v/checker/tests/assign_expr_channel_push.out | 4 ++++ vlib/v/checker/tests/assign_expr_channel_push.vv | 2 ++ 3 files changed, 12 insertions(+) create mode 100644 vlib/v/checker/tests/assign_expr_channel_push.out create mode 100644 vlib/v/checker/tests/assign_expr_channel_push.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b320ea001c..12f199d35d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3711,6 +3711,12 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { right_len = 0 } } + if right is ast.InfixExpr { + if right.op == .arrow { + c.error('cannot use `<-` on the right-hand side of an assignment, as it does not return any values', + right.pos) + } + } } if node.left.len != right_len { if right_first is ast.CallExpr { diff --git a/vlib/v/checker/tests/assign_expr_channel_push.out b/vlib/v/checker/tests/assign_expr_channel_push.out new file mode 100644 index 0000000000..375afad7fa --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_channel_push.out @@ -0,0 +1,4 @@ +vlib/v/checker/tests/assign_expr_channel_push.vv:2:9: error: cannot use `<-` on the right-hand side of an assignment, as it does not return any values + 1 | ch := chan f64{} + 2 | _ := ch <- 4.56 + | diff --git a/vlib/v/checker/tests/assign_expr_channel_push.vv b/vlib/v/checker/tests/assign_expr_channel_push.vv new file mode 100644 index 0000000000..e00edcfbd7 --- /dev/null +++ b/vlib/v/checker/tests/assign_expr_channel_push.vv @@ -0,0 +1,2 @@ +ch := chan f64{} +_ := ch <- 4.56