From 1d0f3abadf5cde7d34281cfc47a23dbd1711b520 Mon Sep 17 00:00:00 2001
From: Joe Conigliaro <joe.conigliaro@gmail.com>
Date: Thu, 26 Mar 2020 22:40:14 +1100
Subject: [PATCH] cgen/checker: variadic method call fix

---
 vlib/v/checker/checker.v | 2 +-
 vlib/v/gen/cgen.v        | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v
index ec953e564a..1fe3df2646 100644
--- a/vlib/v/checker/checker.v
+++ b/vlib/v/checker/checker.v
@@ -327,7 +327,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
 		// println('CLONE nr args=$method.args.len')
 		// }
 		for i, arg in method_call_expr.args {
-			c.expected_type = method.args[i + 1].typ
+			c.expected_type = if method.is_variadic && i >= method.args.len-1 { method.args[method.args.len - 1].typ } else { method.args[i + 1].typ }
 			method_call_expr.args[i].typ = c.expr(arg.expr)
 		}
 		// TODO: typ optimize.. this node can get processed more than once
diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v
index 19c0e3555d..7091c61c23 100644
--- a/vlib/v/gen/cgen.v
+++ b/vlib/v/gen/cgen.v
@@ -1006,7 +1006,8 @@ fn (g mut Gen) expr(node ast.Expr) {
 				g.write('/*rec*/*')
 			}
 			g.expr(it.expr)
-			if it.args.len > 0 {
+			is_variadic := it.exp_arg_types.len > 0 && table.type_is_variadic(it.exp_arg_types[it.exp_arg_types.len-1])
+			if it.args.len > 0 || is_variadic {
 				g.write(', ')
 			}
 			// /////////