1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix generic array of sumtype push operation (#17268)

This commit is contained in:
yuyi 2023-02-10 06:27:07 +08:00 committed by GitHub
parent 3aecd01d05
commit 7cfbc1cc5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -476,7 +476,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
right_pos) right_pos)
} }
} else { } 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)) { 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}`', c.error('cannot append `${right_sym.name}` to `${left_sym.name}`',
right_pos) right_pos)

View File

@ -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')
}