Skip to content

Commit 21e6c46

Browse files
authored
checker: error if right side of infix is void (fix #23976) (#24001)
1 parent 98142ae commit 21e6c46

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

vlib/v/checker/check_types.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ fn (mut c Checker) promote(left_type ast.Type, right_type ast.Type) ast.Type {
710710
} else if left_type.has_flag(.option) != right_type.has_flag(.option) {
711711
// incompatible
712712
return ast.void_type
713+
} else if (left_type == ast.void_type && right_type != ast.void_type)
714+
|| (right_type == ast.void_type && left_type != ast.void_type) {
715+
// incompatible as well
716+
return ast.void_type
713717
} else {
714718
return left_type // default to left if not automatic promotion possible
715719
}

vlib/v/checker/tests/infix_err.out

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,45 +45,45 @@ vlib/v/checker/tests/infix_err.vv:13:7: error: `+` cannot be used with `?int`
4545
12 |
4646
13 | _ = 4 + g()
4747
| ^
48-
14 | _ = int(0) + g() // FIXME: not detected
48+
14 | _ = int(0) + g()
4949
15 | _ = g() + int(3)
5050
vlib/v/checker/tests/infix_err.vv:13:9: error: unwrapped Option cannot be used in an infix expression
5151
11 | _ = f() + f()
5252
12 |
5353
13 | _ = 4 + g()
5454
| ~~~
55-
14 | _ = int(0) + g() // FIXME: not detected
55+
14 | _ = int(0) + g()
5656
15 | _ = g() + int(3)
5757
vlib/v/checker/tests/infix_err.vv:14:14: error: unwrapped Option cannot be used in an infix expression
5858
12 |
5959
13 | _ = 4 + g()
60-
14 | _ = int(0) + g() // FIXME: not detected
60+
14 | _ = int(0) + g()
6161
| ~~~
6262
15 | _ = g() + int(3)
6363
16 | _ = g() + 3
6464
vlib/v/checker/tests/infix_err.vv:15:9: error: `+` cannot be used with `?int`
6565
13 | _ = 4 + g()
66-
14 | _ = int(0) + g() // FIXME: not detected
66+
14 | _ = int(0) + g()
6767
15 | _ = g() + int(3)
6868
| ^
6969
16 | _ = g() + 3
7070
17 |
7171
vlib/v/checker/tests/infix_err.vv:15:5: error: unwrapped Option cannot be used in an infix expression
7272
13 | _ = 4 + g()
73-
14 | _ = int(0) + g() // FIXME: not detected
73+
14 | _ = int(0) + g()
7474
15 | _ = g() + int(3)
7575
| ~~~
7676
16 | _ = g() + 3
7777
17 |
7878
vlib/v/checker/tests/infix_err.vv:16:9: error: `+` cannot be used with `?int`
79-
14 | _ = int(0) + g() // FIXME: not detected
79+
14 | _ = int(0) + g()
8080
15 | _ = g() + int(3)
8181
16 | _ = g() + 3
8282
| ^
8383
17 |
8484
18 | // binary operands
8585
vlib/v/checker/tests/infix_err.vv:16:5: error: unwrapped Option cannot be used in an infix expression
86-
14 | _ = int(0) + g() // FIXME: not detected
86+
14 | _ = int(0) + g()
8787
15 | _ = g() + int(3)
8888
16 | _ = g() + 3
8989
| ~~~
@@ -123,8 +123,15 @@ vlib/v/checker/tests/infix_err.vv:23:22: error: ambiguous boolean expression. us
123123
23 | _ = 1 == 1 && 2 == 2 || 3 == 3
124124
| ~~
125125
24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4
126+
25 | _ = 1 + println('')
126127
vlib/v/checker/tests/infix_err.vv:24:22: error: ambiguous boolean expression. use `()` to ensure correct order of operations
127128
22 | // boolean expressions
128129
23 | _ = 1 == 1 && 2 == 2 || 3 == 3
129130
24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4
130131
| ~~
132+
25 | _ = 1 + println('')
133+
vlib/v/checker/tests/infix_err.vv:25:5: error: mismatched types `int literal` and `void`
134+
23 | _ = 1 == 1 && 2 == 2 || 3 == 3
135+
24 | _ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4
136+
25 | _ = 1 + println('')
137+
| ~~~~~~~~~~~~~~~

vlib/v/checker/tests/infix_err.vv

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ _ = f() + ''
1111
_ = f() + f()
1212

1313
_ = 4 + g()
14-
_ = int(0) + g() // FIXME: not detected
14+
_ = int(0) + g()
1515
_ = g() + int(3)
1616
_ = g() + 3
1717

@@ -22,3 +22,4 @@ _ = true || 2
2222
// boolean expressions
2323
_ = 1 == 1 && 2 == 2 || 3 == 3
2424
_ = 1 == 1 && 2 == 2 || 3 == 3 && 4 == 4
25+
_ = 1 + println('')

vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Did you mean `time.second`?
44
2 |
55
3 | time.sleep(1 * time.secondz)
66
| ~~~~~~~
7+
vlib/v/checker/tests/misspelled_mod_const_should_have_suggestion.vv:3:12: error: mismatched types `int literal` and `void`
8+
1 | import time
9+
2 |
10+
3 | time.sleep(1 * time.secondz)
11+
| ~~~~~~~~~~~~~~~~
712
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`
813
1 | import time
914
2 |

0 commit comments

Comments
 (0)