cgen: fix `in array_of_ref_structs` (fix #7623) (#7640)

pull/7649/head
yuyi 2020-12-28 18:29:56 +08:00 committed by GitHub
parent 07459a77e3
commit d46b930c71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 23 deletions

View File

@ -1251,7 +1251,7 @@ struct Coord {
z int
}
fn test__array_struct_contains() {
fn test_array_struct_contains() {
mut coords := []Coord{}
coord_1 := Coord{
x: 1
@ -1265,3 +1265,16 @@ fn test__array_struct_contains() {
assert exists == true
assert not_exists == false
}
fn test_array_struct_ref_contains() {
mut coords := []&Coord{}
coord_1 := &Coord{
x: 1
y: 2
z: -1
}
coords << coord_1
exists := coord_1 in coords
println(exists)
assert exists == true
}

View File

@ -406,28 +406,21 @@ fn (mut g Gen) gen_array_contains_method(left_type table.Type) string {
mut fn_builder := strings.new_builder(512)
fn_builder.writeln('static bool ${fn_name}($left_type_str a, $elem_type_str v) {')
fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {')
match elem_sym.kind {
.string {
fn_builder.writeln('\t\tif (string_eq((*(string*)array_get(a, i)), v)) {')
}
.array {
ptr_typ := g.gen_array_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*($elem_type_str*)array_get(a, i), v)) {')
}
.function {
fn_builder.writeln('\t\tif ((*(voidptr*)array_get(a, i)) == v) {')
}
.map {
ptr_typ := g.gen_map_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq(*($elem_type_str*)array_get(a, i), v)) {')
}
.struct_ {
ptr_typ := g.gen_struct_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq(*($elem_type_str*)array_get(a, i), v)) {')
}
else {
fn_builder.writeln('\t\tif ((*($elem_type_str*)array_get(a, i)) == v) {')
}
if elem_sym.kind == .string {
fn_builder.writeln('\t\tif (string_eq((*(string*)array_get(a, i)), v)) {')
} else if elem_sym.kind == .array && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.gen_array_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*($elem_type_str*)array_get(a, i), v)) {')
} else if elem_sym.kind == .function {
fn_builder.writeln('\t\tif ((*(voidptr*)array_get(a, i)) == v) {')
} else if elem_sym.kind == .map && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.gen_map_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq(*($elem_type_str*)array_get(a, i), v)) {')
} else if elem_sym.kind == .struct_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.gen_struct_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq(*($elem_type_str*)array_get(a, i), v)) {')
} else {
fn_builder.writeln('\t\tif ((*($elem_type_str*)array_get(a, i)) == v) {')
}
fn_builder.writeln('\t\t\treturn true;')
fn_builder.writeln('\t\t}')