cgen: fix array of reference sumtype index() (#14812)

master
yuyi 2022-06-21 17:37:54 +08:00 committed by GitHub
parent f08c768c8e
commit cab6355a38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View File

@ -858,10 +858,18 @@ fn (mut g Gen) gen_array_index_methods() {
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_ { } else if elem_sym.kind == .interface_ {
ptr_typ := g.equality_fn(info.elem_type) ptr_typ := g.equality_fn(info.elem_type)
if info.elem_type.is_ptr() {
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq(**pelem, *v)) {')
} else {
fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq(*pelem, v)) {') fn_builder.writeln('\t\tif (${ptr_typ}_interface_eq(*pelem, v)) {')
}
} else if elem_sym.kind == .sum_type { } else if elem_sym.kind == .sum_type {
ptr_typ := g.equality_fn(info.elem_type) ptr_typ := g.equality_fn(info.elem_type)
if info.elem_type.is_ptr() {
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(**pelem, *v)) {')
} else {
fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(*pelem, v)) {') fn_builder.writeln('\t\tif (${ptr_typ}_sumtype_eq(*pelem, v)) {')
}
} else if elem_sym.kind == .alias { } else if elem_sym.kind == .alias {
ptr_typ := g.equality_fn(info.elem_type) ptr_typ := g.equality_fn(info.elem_type)
fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq(*pelem, v)) {') fn_builder.writeln('\t\tif (${ptr_typ}_alias_eq(*pelem, v)) {')

View File

@ -1,11 +1,14 @@
struct Element { struct Element {
AbstractNode AbstractNode
mut:
name string name string
value string
attributes []&Attribute attributes []&Attribute
} }
struct Attribute { struct Attribute {
AbstractNode AbstractNode
mut:
name string name string
value string value string
} }
@ -21,7 +24,11 @@ pub fn (mut n AbstractNode) append_child(child &Node) {
n.child_nodes << child n.child_nodes << child
} }
fn test_array_of_reference_sumtype_append() { pub fn (n &AbstractNode) has_child(child &Node) int {
return n.child_nodes.index(child)
}
fn test_array_of_reference_sumtype() {
mut parent := &Element{ mut parent := &Element{
name: 'parent' name: 'parent'
} }
@ -30,6 +37,9 @@ fn test_array_of_reference_sumtype_append() {
} }
parent.append_child(child) parent.append_child(child)
ret := parent.has_child(child)
println(ret)
dump(parent) dump(parent)
assert parent.child_nodes[0].name == 'child' assert parent.child_nodes[0].name == 'child'
assert ret == 0
} }