Skip to content

Commit 7cfbc1c

Browse files
authored
checker: fix generic array of sumtype push operation (#17268)
1 parent 3aecd01 commit 7cfbc1c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

vlib/v/checker/infix.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
476476
right_pos)
477477
}
478478
} else {
479-
right_value_type := c.table.value_type(right_type)
479+
right_value_type := c.table.value_type(c.unwrap_generic(right_type))
480480
if !c.table.is_sumtype_or_in_variant(left_value_type, ast.mktyp(right_value_type)) {
481481
c.error('cannot append `${right_sym.name}` to `${left_sym.name}`',
482482
right_pos)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type SumType = int | string
2+
3+
fn template_fn[T](a []T) []T {
4+
mut b := []T{}
5+
b << a
6+
return b
7+
}
8+
9+
fn test_generic_array_of_sumtype_push() {
10+
ret1 := template_fn([SumType(1)])
11+
println(ret1)
12+
assert ret1.len == 1
13+
assert ret1[0] == SumType(1)
14+
15+
ret2 := template_fn([SumType('hello')])
16+
println(ret2)
17+
assert ret2.len == 1
18+
assert ret2[0] == SumType('hello')
19+
}

0 commit comments

Comments
 (0)