Skip to content

Commit 3e02cfd

Browse files
authored
cgen: fix in op usage on array of sumtypes without cast (#12141)
1 parent ceb24bc commit 3e02cfd

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

vlib/v/gen/c/infix_expr.v

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,22 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) {
394394
return
395395
}
396396
}
397+
if right.sym.info is ast.Array {
398+
elem_type := right.sym.info.elem_type
399+
elem_type_ := g.unwrap(elem_type)
400+
if elem_type_.sym.kind == .sum_type {
401+
if node.left_type in elem_type_.sym.sumtype_info().variants {
402+
new_node_left := ast.CastExpr{
403+
arg: ast.EmptyExpr{}
404+
typ: elem_type
405+
expr: node.left
406+
expr_type: node.left_type
407+
}
408+
g.gen_array_contains(node.right_type, node.right, new_node_left)
409+
return
410+
}
411+
}
412+
}
397413
g.gen_array_contains(node.right_type, node.right, node.left)
398414
} else if right.unaliased_sym.kind == .map {
399415
g.write('_IN_MAP(')

vlib/v/tests/in_expression_test.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,10 @@ fn test_in_sumtype_array() {
280280
println(foo)
281281
assert true
282282
}
283+
284+
// without sumtype cast
285+
mut foos := []Foo{}
286+
foos << Foo1{}
287+
assert Foo1{} in foos
288+
assert Foo2{} !in foos
283289
}

0 commit comments

Comments
 (0)