Skip to content

Commit

Permalink
checker: fix calling functions with variadic interface parameters (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
spaceface777 committed May 8, 2021
1 parent 23513ae commit ef7454b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
13 changes: 11 additions & 2 deletions vlib/v/checker/checker.v
Expand Up @@ -1854,7 +1854,12 @@ pub fn (mut c Checker) method_call(mut call_expr ast.CallExpr) ast.Type {
c.error('when forwarding a variadic variable, it must be the final argument',
arg.pos)
}
if exp_arg_sym.kind == .interface_ {
mut final_arg_sym := exp_arg_sym
if method.is_variadic && exp_arg_sym.info is ast.Array {
final_arg_sym = c.table.get_type_symbol(exp_arg_sym.array_info().elem_type)
}
// Handle expected interface
if final_arg_sym.kind == .interface_ {
c.type_implements(got_arg_typ, exp_arg_typ, arg.expr.position())
continue
}
Expand Down Expand Up @@ -2436,8 +2441,12 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type {
c.fail_if_unreadable(call_arg.expr, typ, 'argument')
}
}
mut final_arg_sym := arg_typ_sym
if func.is_variadic && arg_typ_sym.info is ast.Array {
final_arg_sym = c.table.get_type_symbol(arg_typ_sym.array_info().elem_type)
}
// Handle expected interface
if arg_typ_sym.kind == .interface_ {
if final_arg_sym.kind == .interface_ {
c.type_implements(typ, param.typ, call_arg.expr.position())
continue
}
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/interface_variadic_test.v
Expand Up @@ -23,3 +23,20 @@ fn test_variadic_multiple_args() {

assert a[0].method(0.0, 1.0) == '[0, 1]'
}

interface Animal {}

struct Cat {}

struct Dog {}

fn test_variadic_interface_fn_arg() {
c := Cat{}
d := Dog{}
check_animals(c, d)
}

fn check_animals(animals ...Animal) {
assert animals[0] is Cat
assert animals[1] is Dog
}

0 comments on commit ef7454b

Please sign in to comment.