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

cgen: support index and in for sumtype/interface arrays (#12051)

This commit is contained in:
spaceface 2021-10-03 14:44:13 +02:00 committed by GitHub
parent 10caf4a84a
commit 9b6e07e2a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 3 deletions

View File

@ -530,6 +530,12 @@ fn (mut g Gen) gen_array_contains_methods() {
} else if elem_sym.kind == .struct_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .interface_ && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq((($elem_type_str*)a.data)[i], v)) {')
} else if elem_sym.kind == .sum_type && left_info.elem_type.nr_muls() == 0 {
ptr_typ := g.equality_fn(left_info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq((($elem_type_str*)a.data)[i], v)) {')
} else {
fn_builder.writeln('\t\tif ((($elem_type_str*)a.data)[i] == v) {')
}
@ -591,15 +597,21 @@ fn (mut g Gen) gen_array_index_methods() {
fn_builder.writeln('\t\tif (fast_string_eq(*pelem, v)) {')
} else if elem_sym.kind == .array && !info.elem_type.is_ptr() {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq( *pelem, v)) {')
fn_builder.writeln('\t\tif (${ptr_typ}_arr_eq(*pelem, v)) {')
} else if elem_sym.kind == .function && !info.elem_type.is_ptr() {
fn_builder.writeln('\t\tif ( pelem == v) {')
} else if elem_sym.kind == .map && !info.elem_type.is_ptr() {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq(( *pelem, v))) {')
fn_builder.writeln('\t\tif (${ptr_typ}_map_eq((*pelem, v))) {')
} else if elem_sym.kind == .struct_ && !info.elem_type.is_ptr() {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq( *pelem, v)) {')
fn_builder.writeln('\t\tif (${ptr_typ}_struct_eq(*pelem, v)) {')
} else if elem_sym.kind == .interface_ {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq(*pelem, v)) {')
} else if elem_sym.kind == .sum_type {
ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(*pelem, v)) {')
} else {
fn_builder.writeln('\t\tif (*pelem == v) {')
}

View File

@ -0,0 +1,36 @@
interface IA {
a int
}
struct AA {
a int
}
struct AB {
a int
}
fn test_interface_array_index() {
mut ia_ary := []IA{}
aa, ab := AA{12}, AB{13}
ia_ary << aa
ia_ary << ab
abi := IA(AB{13})
aci := IA(AB{14})
assert ia_ary.index(abi) == 1
assert ia_ary.index(aci) == -1
}
fn test_interface_array_contains() {
mut ia_ary := []IA{}
aa, ab := AA{12}, AB{13}
ia_ary << aa
ia_ary << ab
abi := IA(AB{13})
aci := IA(AB{14})
assert abi in ia_ary
assert aci !in ia_ary
}

View File

@ -0,0 +1,34 @@
type IA = AA | AB
struct AA {
a int
}
struct AB {
a int
}
fn test_interface_array_index() {
mut ia_ary := []IA{}
aa, ab := AA{12}, AB{13}
ia_ary << aa
ia_ary << ab
abi := IA(AB{13})
aci := IA(AB{14})
assert ia_ary.index(abi) == 1
assert ia_ary.index(aci) == -1
}
fn test_interface_array_contains() {
mut ia_ary := []IA{}
aa, ab := AA{12}, AB{13}
ia_ary << aa
ia_ary << ab
abi := IA(AB{13})
aci := IA(AB{14})
assert abi in ia_ary
assert aci !in ia_ary
}