From 21e6c46ae83f1cbe05a74f49afa969c37f8c89b2 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Sun, 30 Mar 2025 18:46:51 +0530 Subject: [PATCH] checker: error if right side of infix is void (fix #23976) (#24001) --- vlib/v/checker/check_types.v | 4 ++++ vlib/v/checker/tests/infix_err.out | 21 ++++++++++++------- vlib/v/checker/tests/infix_err.vv | 3 ++- ...elled_mod_const_should_have_suggestion.out | 5 +++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index c4fcddc529..cf028127a2 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -710,6 +710,10 @@ fn (mut c Checker) promote(left_type ast.Type, right_type ast.Type) ast.Type { } else if left_type.has_flag(.option) != right_type.has_flag(.option) { // incompatible return ast.void_type + } else if (left_type == ast.void_type && right_type != ast.void_type) + || (right_type == ast.void_type && left_type != ast.void_type) { + // incompatible as well + return ast.void_type } else { return left_type // default to left if not automatic promotion possible } diff --git a/vlib/v/checker/tests/infix_err.out b/vlib/v/checker/tests/infix_err.out index 1451e4e3e7..82b4819c19 100644 --- a/vlib/v/checker/tests/infix_err.out +++ b/vlib/v/checker/tests/infix_err.out @@ -45,45 +45,45 @@ vlib/v/checker/tests/infix_err.vv:13:7: error: `+` cannot be used with `?int` 12 | 13 | _ = 4 + g() | ^ - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) vlib/v/checker/tests/infix_err.vv:13:9: error: unwrapped Option cannot be used in an infix expression 11 | _ = f() + f() 12 | 13 | _ = 4 + g() | ~~~ - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) vlib/v/checker/tests/infix_err.vv:14:14: error: unwrapped Option cannot be used in an infix expression 12 | 13 | _ = 4 + g() - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() | ~~~ 15 | _ = g() + int(3) 16 | _ = g() + 3 vlib/v/checker/tests/infix_err.vv:15:9: error: `+` cannot be used with `?int` 13 | _ = 4 + g() - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) | ^ 16 | _ = g() + 3 17 | vlib/v/checker/tests/infix_err.vv:15:5: error: unwrapped Option cannot be used in an infix expression 13 | _ = 4 + g() - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) | ~~~ 16 | _ = g() + 3 17 | vlib/v/checker/tests/infix_err.vv:16:9: error: `+` cannot be used with `?int` - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) 16 | _ = g() + 3 | ^ 17 | 18 | // binary operands vlib/v/checker/tests/infix_err.vv:16:5: error: unwrapped Option cannot be used in an infix expression - 14 | _ = int(0) + g() // FIXME: not detected + 14 | _ = int(0) + g() 15 | _ = g() + int(3) 16 | _ = g() + 3 | ~~~ @@ -123,8 +123,15 @@ vlib/v/checker/tests/infix_err.vv:23:22: error: ambiguous boolean expression. us 23 | _ = 1 == 1 && 2 == 2 || 3 == 3 | ~~ 24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4 + 25 | _ = 1 + println('') vlib/v/checker/tests/infix_err.vv:24:22: error: ambiguous boolean expression. use `()` to ensure correct order of operations 22 | // boolean expressions 23 | _ = 1 == 1 && 2 == 2 || 3 == 3 24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4 | ~~ + 25 | _ = 1 + println('') +vlib/v/checker/tests/infix_err.vv:25:5: error: mismatched types `int literal` and `void` + 23 | _ = 1 == 1 && 2 == 2 || 3 == 3 + 24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4 + 25 | _ = 1 + println('') + | ~~~~~~~~~~~~~~~ diff --git a/vlib/v/checker/tests/infix_err.vv b/vlib/v/checker/tests/infix_err.vv index 5e9d9c6c36..08a5a38db8 100644 --- a/vlib/v/checker/tests/infix_err.vv +++ b/vlib/v/checker/tests/infix_err.vv @@ -11,7 +11,7 @@ _ = f() + '' _ = f() + f() _ = 4 + g() -_ = int(0) + g() // FIXME: not detected +_ = int(0) + g() _ = g() + int(3) _ = g() + 3 @@ -22,3 +22,4 @@ _ = true || 2 // boolean expressions _ = 1 == 1 && 2 == 2 || 3 == 3 _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4 +_ = 1 + println('') diff --git a/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out index 875ffb57cf..68b9fe7d0f 100644 --- a/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out +++ b/vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out @@ -4,6 +4,11 @@ Did you mean `time.second`? 2 | 3 | time.sleep(1 * time.secondz) | ~~~~~~~ +vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv:3:12: error: mismatched types `int literal` and `void` + 1 | import time + 2 | + 3 | time.sleep(1 * time.secondz) + | ~~~~~~~~~~~~~~~~ vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv:3:12: error: `1 * time.secondz` (no value) used as value in argument 1 to `time.sleep` 1 | import time 2 |