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

cgen: fix call method with an empty args on an interface with variadic (fix #16286) (#16290)

This commit is contained in:
shove 2022-11-02 21:45:39 +08:00 committed by GitHub
parent 4e05e07b94
commit 0fa6f60fac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -847,7 +847,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.expr(node.left)
}
g.write('${dot}_object')
if node.args.len > 0 {
is_variadic := node.expected_arg_types.len > 0
&& node.expected_arg_types[node.expected_arg_types.len - 1].has_flag(.variadic)
if node.args.len > 0 || is_variadic {
g.write(', ')
g.call_args(node)
}

View File

@ -40,3 +40,29 @@ fn check_animals(animals ...Animal) {
assert animals[0] is Cat
assert animals[1] is Dog
}
// For issue: 16286 passing nothing to a variatic parameter on an interface method gives builder error
interface Bar {
get(...int) []int
}
struct Baz {}
fn (b Baz) get(n ...int) []int {
if n.len == 0 {
return [-1]
} else {
return n
}
}
fn test_empty_args_call_interface_methods() {
b := Baz{}
assert b.get() == [-1]
b_values := Bar(Baz{})
assert b_values.get(1, 2, 3) == [1, 2, 3]
b_empty := Bar(Baz{})
assert b_empty.get() == [-1]
}