1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow function calls returning void, in () expressions, in assignments (#17205)

This commit is contained in:
Swastik Baranwal 2023-02-03 12:17:52 +05:30 committed by GitHub
parent 8cdc554c63
commit 6d63b27c26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 1 deletions

View File

@ -18,7 +18,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
mut right_type0 := ast.void_type
for i, mut right in node.right {
if right in [ast.CallExpr, ast.IfExpr, ast.LockExpr, ast.MatchExpr, ast.DumpExpr,
ast.SelectorExpr] {
ast.SelectorExpr, ast.ParExpr] {
if right in [ast.IfExpr, ast.MatchExpr] && node.left.len == node.right.len && !is_decl
&& node.left[i] in [ast.Ident, ast.SelectorExpr] && !node.left[i].is_blank_ident() {
c.expected_type = c.expr(node.left[i])
@ -79,6 +79,13 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
c.error('assignment mismatch: ${node.left.len} variable(s) but `${right_first.name}()` returns ${right_len} value(s)',
node.pos)
} else if right_first is ast.ParExpr {
if right_first.expr is ast.CallExpr {
if right_first.expr.return_type == ast.void_type {
c.error('assignment mismatch: expected ${node.left.len} value(s) but `${right_first.expr.name}()` returns ${right_len} value(s)',
node.pos)
}
}
} else {
c.error('assignment mismatch: ${node.left.len} variable(s) ${right_len} value(s)',
node.pos)

View File

@ -0,0 +1,3 @@
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)
1 | _ := (print(10))
| ~~

View File

@ -0,0 +1 @@
_ := (print(10))