Skip to content

Commit a810fbb

Browse files
authored
cgen: fix infix_expr_in_optimization compile error when treating some kind cannot directly use '==' (#14015)
1 parent 8788512 commit a810fbb

File tree

3 files changed

+61
-28
lines changed

3 files changed

+61
-28
lines changed

vlib/v/gen/c/array.v

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,11 +675,7 @@ fn (mut g Gen) gen_array_contains_methods() {
675675
fn_name := '${left_type_str}_contains'
676676
left_info := left_final_sym.info as ast.Array
677677
mut elem_type_str := g.typ(left_info.elem_type)
678-
mut elem_sym := g.table.sym(left_info.elem_type)
679-
if elem_sym.kind == .alias {
680-
info := elem_sym.info as ast.Alias
681-
elem_sym = g.table.sym(info.parent_type)
682-
}
678+
elem_sym := g.table.sym(left_info.elem_type)
683679
if elem_sym.kind == .function {
684680
left_type_str = 'Array_voidptr'
685681
elem_type_str = 'voidptr'
@@ -707,6 +703,9 @@ fn (mut g Gen) gen_array_contains_methods() {
707703
} else if elem_sym.kind == .sum_type && left_info.elem_type.nr_muls() == 0 {
708704
ptr_typ := g.equality_fn(left_info.elem_type)
709705
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq((($elem_type_str*)a.data)[i], v)) {')
706+
} else if elem_sym.kind == .alias && left_info.elem_type.nr_muls() == 0 {
707+
ptr_typ := g.equality_fn(left_info.elem_type)
708+
fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq((($elem_type_str*)a.data)[i], v)) {')
710709
} else {
711710
fn_builder.writeln('\t\tif ((($elem_type_str*)a.data)[i] == v) {')
712711
}
@@ -757,11 +756,7 @@ fn (mut g Gen) gen_array_index_methods() {
757756
fn_name := '${left_type_str}_index'
758757
info := final_left_sym.info as ast.Array
759758
mut elem_type_str := g.typ(info.elem_type)
760-
mut elem_sym := g.table.sym(info.elem_type)
761-
if elem_sym.kind == .alias {
762-
info_t := elem_sym.info as ast.Alias
763-
elem_sym = g.table.sym(info_t.parent_type)
764-
}
759+
elem_sym := g.table.sym(info.elem_type)
765760
if elem_sym.kind == .function {
766761
left_type_str = 'Array_voidptr'
767762
elem_type_str = 'voidptr'
@@ -790,6 +785,9 @@ fn (mut g Gen) gen_array_index_methods() {
790785
} else if elem_sym.kind == .sum_type {
791786
ptr_typ := g.equality_fn(info.elem_type)
792787
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(*pelem, v)) {')
788+
} else if elem_sym.kind == .alias {
789+
ptr_typ := g.equality_fn(info.elem_type)
790+
fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq(*pelem, v)) {')
793791
} else {
794792
fn_builder.writeln('\t\tif (*pelem == v) {')
795793
}

vlib/v/gen/c/infix_expr.v

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -454,25 +454,38 @@ fn (mut g Gen) infix_expr_in_op(node ast.InfixExpr) {
454454
// and transform them in a serie of equality comparison
455455
// i.e. `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
456456
fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, right ast.ArrayInit) {
457-
is_str := right.elem_type.idx() == ast.string_type_idx
458-
elem_sym := g.table.sym(right.elem_type)
459-
is_array := elem_sym.kind == .array
457+
mut elem_sym := g.table.sym(right.elem_type)
460458
for i, array_expr in right.exprs {
461-
if is_str {
462-
g.write('string__eq(')
463-
} else if is_array {
464-
ptr_typ := g.equality_fn(right.elem_type)
465-
g.write('${ptr_typ}_arr_eq(')
466-
}
467-
g.expr(left)
468-
if is_str || is_array {
469-
g.write(', ')
470-
} else {
471-
g.write(' == ')
472-
}
473-
g.expr(array_expr)
474-
if is_str || is_array {
475-
g.write(')')
459+
match elem_sym.kind {
460+
.string, .alias, .sum_type, .map, .interface_, .array, .struct_ {
461+
if elem_sym.kind == .string {
462+
g.write('string__eq(')
463+
} else {
464+
ptr_typ := g.equality_fn(right.elem_type)
465+
if elem_sym.kind == .alias {
466+
g.write('${ptr_typ}_alias_eq(')
467+
} else if elem_sym.kind == .sum_type {
468+
g.write('${ptr_typ}_sumtype_eq(')
469+
} else if elem_sym.kind == .map {
470+
g.write('${ptr_typ}_map_eq(')
471+
} else if elem_sym.kind == .interface_ {
472+
g.write('${ptr_typ}_interface_eq(')
473+
} else if elem_sym.kind == .array {
474+
g.write('${ptr_typ}_arr_eq(')
475+
} else if elem_sym.kind == .struct_ {
476+
g.write('${ptr_typ}_struct_eq(')
477+
}
478+
}
479+
g.expr(left)
480+
g.write(', ')
481+
g.expr(array_expr)
482+
g.write(')')
483+
}
484+
else { // works in function kind
485+
g.expr(left)
486+
g.write(' == ')
487+
g.expr(array_expr)
488+
}
476489
}
477490
if i < right.exprs.len - 1 {
478491
g.write(' || ')

vlib/v/tests/in_expression_test.v

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,25 @@ fn test_in_sumtype_array() {
287287
assert Foo1{} in foos
288288
assert Foo2{} !in foos
289289
}
290+
291+
fn test_in_struct_array() {
292+
assert Foo1{} == Foo1{}
293+
}
294+
295+
fn fn1() {}
296+
297+
fn fn2() {}
298+
299+
fn fn3() {}
300+
301+
fn test_in_func_array() {
302+
assert fn1 in [fn1, fn2, fn3]
303+
}
304+
305+
type Str = string
306+
type Struct = Foo1
307+
308+
fn test_in_alias_array() {
309+
assert Str('') in [Str(''), Str('a')]
310+
assert Struct{} == Struct{}
311+
}

0 commit comments

Comments
 (0)