From c9f6a969363feb3118d9d2b099a32b1779472b5e Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 17 Dec 2021 20:58:17 +0800 Subject: [PATCH] cgen: fix array of interfaces equality error (#12874) --- vlib/v/gen/c/auto_eq_methods.v | 3 ++ .../tests/array_of_interfaces_equality_test.v | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 vlib/v/tests/array_of_interfaces_equality_test.v diff --git a/vlib/v/gen/c/auto_eq_methods.v b/vlib/v/gen/c/auto_eq_methods.v index f21280b59f..4703ede9e1 100644 --- a/vlib/v/gen/c/auto_eq_methods.v +++ b/vlib/v/gen/c/auto_eq_methods.v @@ -236,6 +236,9 @@ fn (mut g Gen) gen_array_equality_fn(left_type ast.Type) string { } else if elem.sym.kind == .struct_ && !elem.typ.is_ptr() { eq_fn := g.gen_struct_equality_fn(elem.typ) fn_builder.writeln('\t\tif (!${eq_fn}_struct_eq((($ptr_elem_styp*)a.data)[i], (($ptr_elem_styp*)b.data)[i])) {') + } else if elem.sym.kind == .interface_ && !elem.typ.is_ptr() { + eq_fn := g.gen_interface_equality_fn(elem.typ) + fn_builder.writeln('\t\tif (!${eq_fn}_interface_eq((($ptr_elem_styp*)a.data)[i], (($ptr_elem_styp*)b.data)[i])) {') } else if elem.sym.kind == .array && !elem.typ.is_ptr() { eq_fn := g.gen_array_equality_fn(elem.typ) fn_builder.writeln('\t\tif (!${eq_fn}_arr_eq((($ptr_elem_styp*)a.data)[i], (($ptr_elem_styp*)b.data)[i])) {') diff --git a/vlib/v/tests/array_of_interfaces_equality_test.v b/vlib/v/tests/array_of_interfaces_equality_test.v new file mode 100644 index 0000000000..0ea77fd50c --- /dev/null +++ b/vlib/v/tests/array_of_interfaces_equality_test.v @@ -0,0 +1,31 @@ +interface IObject { + foo() +} + +struct Foo {} + +fn (f Foo) foo() { +} + +struct Array { +mut: + array []IObject +} + +fn (a Array) contains(x IObject) bool { + for element in a.array { + if element == x { + return true + } + } + return false +} + +fn test_array_of_interfaces_equality() { + foo := Foo{} + mut ary := Array{} + ary.array << foo + ret := ary.contains(foo) + println(ret) + assert ret +}