cgen: add interface case for gen_struct_equality_fn (#12094)

pull/12112/head
05st 2021-10-08 11:04:24 -05:00 committed by GitHub
parent a8ace2c41c
commit cd5b304cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions

View File

@ -155,6 +155,9 @@ fn (mut g Gen) gen_struct_equality_fn(left_type ast.Type) string {
fn_builder.write_string('${eq_fn}_alias_eq(a.$field_name, b.$field_name)') fn_builder.write_string('${eq_fn}_alias_eq(a.$field_name, b.$field_name)')
} else if field_type.sym.kind == .function { } else if field_type.sym.kind == .function {
fn_builder.write_string('*((voidptr*)(a.$field_name)) == *((voidptr*)(b.$field_name))') fn_builder.write_string('*((voidptr*)(a.$field_name)) == *((voidptr*)(b.$field_name))')
} else if field_type.sym.kind == .interface_ {
eq_fn := g.gen_interface_equality_fn(field.typ)
fn_builder.write_string('${eq_fn}_interface_eq(a.$field_name, b.$field_name)')
} else { } else {
fn_builder.write_string('a.$field_name == b.$field_name') fn_builder.write_string('a.$field_name == b.$field_name')
} }

View File

@ -0,0 +1,23 @@
interface IExample {
thing() bool
}
struct Foo {}
fn (n Foo) thing() bool {
return true
}
struct Test {
a IExample
}
fn new() Test {
return Test{Foo{}}
}
fn test_struct_auto_eq_gen_interface_case() {
w1 := new()
w2 := new()
assert w1 == w2
}