1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix array of interfaces equality error (#12874)

This commit is contained in:
yuyi 2021-12-17 20:58:17 +08:00 committed by GitHub
parent d80dd77adf
commit c9f6a96936
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -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])) {')

View File

@ -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
}