Skip to content

Commit 6d63b27

Browse files
authored
checker: disallow function calls returning void, in () expressions, in assignments (#17205)
1 parent 8cdc554 commit 6d63b27

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

vlib/v/checker/assign.v

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
1818
mut right_type0 := ast.void_type
1919
for i, mut right in node.right {
2020
if right in [ast.CallExpr, ast.IfExpr, ast.LockExpr, ast.MatchExpr, ast.DumpExpr,
21-
ast.SelectorExpr] {
21+
ast.SelectorExpr, ast.ParExpr] {
2222
if right in [ast.IfExpr, ast.MatchExpr] && node.left.len == node.right.len && !is_decl
2323
&& node.left[i] in [ast.Ident, ast.SelectorExpr] && !node.left[i].is_blank_ident() {
2424
c.expected_type = c.expr(node.left[i])
@@ -79,6 +79,13 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
7979
}
8080
c.error('assignment mismatch: ${node.left.len} variable(s) but `${right_first.name}()` returns ${right_len} value(s)',
8181
node.pos)
82+
} else if right_first is ast.ParExpr {
83+
if right_first.expr is ast.CallExpr {
84+
if right_first.expr.return_type == ast.void_type {
85+
c.error('assignment mismatch: expected ${node.left.len} value(s) but `${right_first.expr.name}()` returns ${right_len} value(s)',
86+
node.pos)
87+
}
88+
}
8289
} else {
8390
c.error('assignment mismatch: ${node.left.len} variable(s) ${right_len} value(s)',
8491
node.pos)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vlib/v/checker/tests/par_expr_assign_void_right_type_err.vv:1:3: error: assignment mismatch: expected 1 value(s) but `print()` returns 0 value(s)
2+
1 | _ := (print(10))
3+
| ~~
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_ := (print(10))

0 commit comments

Comments
 (0)