Skip to content

Commit

Permalink
cgen: fix array contains method with interface(fix #19670) (#19675)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 authored Oct 29, 2023
1 parent 4a99b1e commit 0148914
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
9 changes: 6 additions & 3 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -979,15 +979,18 @@ fn (mut g Gen) gen_array_contains(left_type ast.Type, left ast.Expr, right_type
g.write('->val')
}
g.write(', ')
if right.is_auto_deref_var() {
g.write('*')
}
left_sym := g.table.final_sym(left_type)
elem_typ := if left_sym.kind == .array {
left_sym.array_info().elem_type
} else {
left_sym.array_fixed_info().elem_type
}
if right.is_auto_deref_var()
|| (g.table.sym(elem_typ).kind !in [.interface_, .sum_type, .struct_] && right is ast.Ident
&& right.info is ast.IdentVar
&& g.table.sym(right.obj.typ).kind in [.interface_, .sum_type]) {
g.write('*')
}
if g.table.sym(elem_typ).kind in [.interface_, .sum_type] {
g.expr_with_cast(right, right_type, elem_typ)
} else {
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, right ast.ArrayInit) {
.string, .alias, .sum_type, .map, .interface_, .array, .struct_ {
if elem_sym.kind == .string {
g.write('string__eq(')
if left.is_auto_deref_var() || (left is ast.Ident && left.info is ast.IdentVar
&& g.table.sym(left.obj.typ).kind in [.interface_, .sum_type]) {
g.write('*')
}
} else {
ptr_typ := g.equality_fn(right.elem_type)
if elem_sym.kind == .alias {
Expand Down
34 changes: 34 additions & 0 deletions vlib/v/tests/array_methods_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,37 @@ fn test_any_called_with_opt_bool_fn() ? {
_ := [1, 2, 3].any(opt_bool_fn()?)
assert true
}

interface Args {}

const some_strings = ['one', 'two', 'three']

// For test `gen array contains method`
fn test_array_contains_method_with_interface() {
arg := Args('one')
match arg {
string {
if arg in some_strings {
assert true
return
}
}
else {}
}
assert false
}

// For test `gen string_eq method`
fn test_string_eq_method_with_interface() {
arg := Args('three')
match arg {
string {
if arg in ['one', 'two', 'three'] {
assert true
return
}
}
else {}
}
assert false
}

0 comments on commit 0148914

Please sign in to comment.