Skip to content

Commit 52e481a

Browse files
authored
checker: check error for sumtype in array (#19183)
1 parent 6b3ffe8 commit 52e481a

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

vlib/v/checker/infix.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
190190
for i, typ in node.right.expr_types {
191191
c.ensure_type_exists(typ, node.right.exprs[i].pos())
192192
}
193+
} else {
194+
elem_type := right_final_sym.array_info().elem_type
195+
c.check_expected(left_type, elem_type) or {
196+
c.error('left operand to `${node.op}` does not match the array element type: ${err.msg()}',
197+
left_right_pos)
198+
}
193199
}
194200
}
195201
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/infix_sumtype_in_array_err.vv:15:7: error: left operand to `in` does not match the array element type: expected `rune`, not `RuneAliasOrOtherType`
2+
13 | match x {
3+
14 | RuneAlias {
4+
15 | if x in whitespace {
5+
| ~~~~~~~~~~~~~~~
6+
16 | // doing `if x as RuneAlias in whitepsace` here
7+
17 | // works but it should be doing that automatically
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
struct OtherType {
2+
foo string
3+
bar string
4+
}
5+
6+
type RuneAlias = rune
7+
type RuneAliasOrOtherType = OtherType | RuneAlias
8+
9+
const whitespace = [rune(0x0009), 0x000a, 0x000c, 0x000d, 0x0020]
10+
11+
fn main() {
12+
mut x := RuneAliasOrOtherType(RuneAlias(rune(`!`)))
13+
match x {
14+
RuneAlias {
15+
if x in whitespace {
16+
// doing `if x as RuneAlias in whitepsace` here
17+
// works but it should be doing that automatically
18+
// since I'm inside the RuneAlias match condition.
19+
}
20+
}
21+
else {
22+
// do something else
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)