diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 34d641ea09..212d3abdd8 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -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 { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index 14ea105905..bbc292f27e 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -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 { diff --git a/vlib/v/tests/array_methods_test.v b/vlib/v/tests/array_methods_test.v index 13c24012d5..6f9ad4cd44 100644 --- a/vlib/v/tests/array_methods_test.v +++ b/vlib/v/tests/array_methods_test.v @@ -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 +}