From 7cfbc1cc5fb934fbd5345c1e4f6640999d34e265 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 10 Feb 2023 06:27:07 +0800 Subject: [PATCH] checker: fix generic array of sumtype push operation (#17268) --- vlib/v/checker/infix.v | 2 +- .../generic_array_of_sumtype_push_test.v | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/generic_array_of_sumtype_push_test.v diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index b611630bab..56d00d7860 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -476,7 +476,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { right_pos) } } else { - right_value_type := c.table.value_type(right_type) + 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)) { c.error('cannot append `${right_sym.name}` to `${left_sym.name}`', right_pos) diff --git a/vlib/v/tests/generic_array_of_sumtype_push_test.v b/vlib/v/tests/generic_array_of_sumtype_push_test.v new file mode 100644 index 0000000000..1975ffaea0 --- /dev/null +++ b/vlib/v/tests/generic_array_of_sumtype_push_test.v @@ -0,0 +1,19 @@ +type SumType = int | string + +fn template_fn[T](a []T) []T { + mut b := []T{} + b << a + return b +} + +fn test_generic_array_of_sumtype_push() { + ret1 := template_fn([SumType(1)]) + println(ret1) + assert ret1.len == 1 + assert ret1[0] == SumType(1) + + ret2 := template_fn([SumType('hello')]) + println(ret2) + assert ret2.len == 1 + assert ret2[0] == SumType('hello') +}