Skip to content

Commit

Permalink
checker: fix generic array append (#19658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChAoSUnItY committed Oct 26, 2023
1 parent 029909d commit f705897
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion vlib/v/checker/checker.v
Expand Up @@ -2057,7 +2057,8 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
if mut node.expr is ast.InfixExpr {
if node.expr.op == .left_shift {
left_sym := c.table.final_sym(node.expr.left_type)
if left_sym.kind != .array {
if left_sym.kind != .array
&& c.table.final_sym(c.unwrap_generic(node.expr.left_type)).kind != .array {
c.error('unused expression', node.pos)
}
}
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/checker/infix.v
Expand Up @@ -514,7 +514,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
return c.check_like_operator(node)
}
.left_shift {
if left_final_sym.kind == .array {
if left_final_sym.kind == .array
|| c.table.sym(c.unwrap_generic(left_type)).kind == .array {
if !node.is_stmt {
c.error('array append cannot be used in an expression', node.pos)
}
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/generics_array_append_test.v
Expand Up @@ -8,4 +8,17 @@ fn test_generic_array_append() {
g([1, 2, 3])
g([1.1, 2.2, 3.3])
g(['aa', 'bb', 'cc'])
gs[[]string]()!
}

fn gs[T]() !T {
mut typ := T{
cap: 10
}
if T.name == '[]string' {
typ << 'strings'
} else {
return error('only string arrays are supported')
}
return typ
}

0 comments on commit f705897

Please sign in to comment.