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

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

This commit is contained in:
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 z int
} }
fn test__array_struct_contains() { fn test_array_struct_contains() {
mut coords := []Coord{} mut coords := []Coord{}
coord_1 := Coord{ coord_1 := Coord{
x: 1 x: 1
@ -1265,3 +1265,16 @@ fn test__array_struct_contains() {
assert exists == true assert exists == true
assert not_exists == false 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) 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('static bool ${fn_name}($left_type_str a, $elem_type_str v) {')
fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {') fn_builder.writeln('\tfor (int i = 0; i < a.len; ++i) {')
match elem_sym.kind { if elem_sym.kind == .string {
.string { fn_builder.writeln('\t\tif (string_eq((*(string*)array_get(a, i)), v)) {')
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)
.array { fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*($elem_type_str*)array_get(a, i), v)) {')
ptr_typ := g.gen_array_equality_fn(left_info.elem_type) } else if elem_sym.kind == .function {
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*($elem_type_str*)array_get(a, i), v)) {') fn_builder.writeln('\t\tif ((*(voidptr*)array_get(a, i)) == v) {')
} } else if elem_sym.kind == .map && left_info.elem_type.nr_muls() == 0 {
.function { ptr_typ := g.gen_map_equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif ((*(voidptr*)array_get(a, i)) == v) {') 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 {
.map { ptr_typ := g.gen_struct_equality_fn(left_info.elem_type)
ptr_typ := g.gen_map_equality_fn(left_info.elem_type) fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq(*($elem_type_str*)array_get(a, i), v)) {')
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq(*($elem_type_str*)array_get(a, i), v)) {') } else {
} fn_builder.writeln('\t\tif ((*($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) {')
}
} }
fn_builder.writeln('\t\t\treturn true;') fn_builder.writeln('\t\t\treturn true;')
fn_builder.writeln('\t\t}') fn_builder.writeln('\t\t}')