From cf1fc6f090db351458caa34f44251b11600bdd4d Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 20 Jun 2022 17:23:53 +0800 Subject: [PATCH] ast: fix array of reference sumtype appending (#14797) --- vlib/v/ast/table.v | 2 +- .../array_of_reference_sumtype_append_test.v | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/array_of_reference_sumtype_append_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 35836502d6..45480f6620 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1342,7 +1342,7 @@ pub fn (t &Table) is_sumtype_or_in_variant(parent Type, typ Type) bool { if typ == 0 { return false } - if t.type_kind(typ) == .sum_type && parent.idx() == typ.idx() + if t.sym(typ).kind == .sum_type && parent.idx() == typ.idx() && parent.nr_muls() == typ.nr_muls() { return true } diff --git a/vlib/v/tests/array_of_reference_sumtype_append_test.v b/vlib/v/tests/array_of_reference_sumtype_append_test.v new file mode 100644 index 0000000000..581027886b --- /dev/null +++ b/vlib/v/tests/array_of_reference_sumtype_append_test.v @@ -0,0 +1,35 @@ +struct Element { + AbstractNode + name string + attributes []&Attribute +} + +struct Attribute { + AbstractNode + name string + value string +} + +pub type Node = Attribute | Element + +struct AbstractNode { +pub mut: + child_nodes []&Node +} + +pub fn (mut n AbstractNode) append_child(child &Node) { + n.child_nodes << child +} + +fn test_array_of_reference_sumtype_append() { + mut parent := &Element{ + name: 'parent' + } + mut child := &Element{ + name: 'child' + } + parent.append_child(child) + + dump(parent) + assert parent.child_nodes[0].name == 'child' +}