From b7a9d7d3b08068ef1f8e87a211937b7144e0aa13 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 11 Mar 2026 11:28:20 +0300 Subject: [PATCH] checker: array returned by function bypasses illegal sumtype conversion checker error (fixes #23271) --- vlib/v/checker/containers.v | 4 ++- .../tests/array_of_sumtype_append_err.out | 29 ++++++++++++------- .../tests/array_of_sumtype_append_err.vv | 5 ++++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index b58ecde20..cf1fbb587 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -856,7 +856,9 @@ fn (mut c Checker) check_append(mut node ast.InfixExpr, left_type ast.Type, righ } } else { right_value_type := c.table.value_type(c.unwrap_generic(right_type)) - if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_value_type)) { + left_sumtype := c.table.unaliased_type(c.unwrap_generic(left_value_type)) + right_sumtype := c.table.unaliased_type(c.unwrap_generic(right_value_type)) + if left_sumtype != right_sumtype { c.error('cannot append `${right_sym.name}` to `${left_sym.name}`', right_pos) } } diff --git a/vlib/v/checker/tests/array_of_sumtype_append_err.out b/vlib/v/checker/tests/array_of_sumtype_append_err.out index db60a7772..586fda8b8 100644 --- a/vlib/v/checker/tests/array_of_sumtype_append_err.out +++ b/vlib/v/checker/tests/array_of_sumtype_append_err.out @@ -1,13 +1,20 @@ -vlib/v/checker/tests/array_of_sumtype_append_err.vv:9:9: error: cannot append `[]string` to `[]Bar` - 7 | fn main() { - 8 | mut bar := []Bar{} - 9 | bar << ['hey'] +vlib/v/checker/tests/array_of_sumtype_append_err.vv:13:9: error: cannot append `[]string` to `[]Bar` + 11 | fn main() { + 12 | mut bar := []Bar{} + 13 | bar << ['hey'] | ~~~~~~~ - 10 | bar << 'hey' - 11 | } -vlib/v/checker/tests/array_of_sumtype_append_err.vv:10:9: error: cannot append `string` to `[]Bar` - 8 | mut bar := []Bar{} - 9 | bar << ['hey'] - 10 | bar << 'hey' + 14 | bar << 'hey' + 15 | bar << produce_bar_a_array() +vlib/v/checker/tests/array_of_sumtype_append_err.vv:14:9: error: cannot append `string` to `[]Bar` + 12 | mut bar := []Bar{} + 13 | bar << ['hey'] + 14 | bar << 'hey' | ~~~~~ - 11 | } + 15 | bar << produce_bar_a_array() + 16 | } +vlib/v/checker/tests/array_of_sumtype_append_err.vv:15:9: error: cannot append `[]BarA` to `[]Bar` + 13 | bar << ['hey'] + 14 | bar << 'hey' + 15 | bar << produce_bar_a_array() + | ~~~~~~~~~~~~~~~~~~~~~ + 16 | } diff --git a/vlib/v/checker/tests/array_of_sumtype_append_err.vv b/vlib/v/checker/tests/array_of_sumtype_append_err.vv index 5239a1ab7..90216a85a 100644 --- a/vlib/v/checker/tests/array_of_sumtype_append_err.vv +++ b/vlib/v/checker/tests/array_of_sumtype_append_err.vv @@ -4,8 +4,13 @@ struct BarA {} struct BarB {} +fn produce_bar_a_array() []BarA { + return [BarA{}] +} + fn main() { mut bar := []Bar{} bar << ['hey'] bar << 'hey' + bar << produce_bar_a_array() } -- 2.39.5