checker: error if right side of infix is void (fix #23976) (#24001)

This commit is contained in:
Swastik Baranwal 2025-03-30 18:46:51 +05:30 committed by GitHub
parent 98142aeb6d
commit 21e6c46ae8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 8 deletions

View File

@ -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
}

View File

@ -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('')
| ~~~~~~~~~~~~~~~

View File

@ -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('')

View File

@ -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 |