Skip to content

Commit

Permalink
checker, cgen: fix generics call with interface arg (fix #19976) (#20002
Browse files Browse the repository at this point in the history
)
  • Loading branch information
shove70 committed Nov 27, 2023
1 parent 63ba05b commit fc99781
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions vlib/v/checker/check_types.v
Expand Up @@ -279,8 +279,8 @@ fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, lan
}
} else {
got_typ_sym := c.table.sym(c.unwrap_generic(got))
expected_typ_sym := c.table.sym(c.unwrap_generic(expected_))
if expected_typ_sym.kind == .interface_ && c.type_implements(got, expected_, token.Pos{}) {
expected_typ_sym := c.table.sym(c.unwrap_generic(expected))
if expected_typ_sym.kind == .interface_ && c.type_implements(got, expected, token.Pos{}) {
return
}

Expand Down
6 changes: 3 additions & 3 deletions vlib/v/gen/c/infix.v
Expand Up @@ -828,7 +828,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
} else {
g.write(', (')
}
g.expr_with_cast(node.right, node.right_type, left.unaliased.clear_flag(.shared_f))
g.expr_with_cast(node.right, right.typ, left.unaliased.clear_flag(.shared_f))
styp := g.typ(expected_push_many_atype)
g.write('), ${tmp_var}, ${styp})')
} else {
Expand All @@ -854,7 +854,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
g.write(', _MOV((${elem_type_str}[]){ ')
}
if array_info.elem_type.has_flag(.option) {
g.expr_with_opt(node.right, node.right_type, array_info.elem_type)
g.expr_with_opt(node.right, right.typ, array_info.elem_type)
} else {
// if g.autofree
needs_clone := !g.is_builtin_mod
Expand All @@ -863,7 +863,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
if needs_clone {
g.write('string_clone(')
}
g.expr_with_cast(node.right, node.right_type, array_info.elem_type)
g.expr_with_cast(node.right, right.typ, array_info.elem_type)
if needs_clone {
g.write(')')
}
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/tests/generics_call_with_interface_arg_test.v
@@ -0,0 +1,13 @@
import arrays

interface Foo {}

fn test_main() {
mut arr := []Foo{}
str := 'abc'
i := 0
arr = arrays.concat(arr, str, i)
assert arr == [Foo('abc'), Foo(0)]
arr = arrays.concat(arr, Foo(str), Foo(i))
assert arr == [Foo('abc'), Foo(0), Foo('abc'), Foo(0)]
}

0 comments on commit fc99781

Please sign in to comment.