From 097a745e72d7ff5b6b542fbea76771f84f0323f9 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Thu, 2 Jul 2020 23:55:03 +0200 Subject: [PATCH] fix: variadic interface conversion in call/callBin --- _test/variadic9.go | 14 ++++++++++++++ interp/run.go | 17 +++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 _test/variadic9.go diff --git a/_test/variadic9.go b/_test/variadic9.go new file mode 100644 index 00000000..2f903be5 --- /dev/null +++ b/_test/variadic9.go @@ -0,0 +1,14 @@ +package main + +import "fmt" + +func Sprintf(format string, a ...interface{}) string { + return fmt.Sprintf(format, a...) +} + +func main() { + fmt.Println(Sprintf("Hello %s", "World!")) +} + +// Output: +// Hello World! diff --git a/interp/run.go b/interp/run.go index 44324be6..0d5fa74f 100644 --- a/interp/run.go +++ b/interp/run.go @@ -801,17 +801,18 @@ func call(n *node) { values = append(values, func(f *frame) reflect.Value { return f.data[ind] }) } default: + var arg *itype + if variadic >= 0 && i >= variadic { + arg = n.child[0].typ.arg[variadic].val + } else { + arg = n.child[0].typ.arg[i] + } if c.kind == basicLit || c.rval.IsValid() { - var argType reflect.Type - if variadic >= 0 && i >= variadic { - argType = n.child[0].typ.arg[variadic].val.TypeOf() - } else { - argType = n.child[0].typ.arg[i].TypeOf() - } + argType := arg.TypeOf() convertLiteralValue(c, argType) } switch { - case len(n.child[0].typ.arg) > i && n.child[0].typ.arg[i].cat == interfaceT: + case arg.cat == interfaceT: values = append(values, genValueInterface(c)) case isRecursiveType(c.typ, c.typ.rtype): values = append(values, genValueRecursiveInterfacePtrValue(c)) @@ -1072,7 +1073,7 @@ func callBin(n *node) { values = append(values, genFunctionWrapper(c)) case interfaceT: values = append(values, genValueInterfaceValue(c)) - case arrayT: + case arrayT, variadicT: switch c.typ.val.cat { case interfaceT: values = append(values, genValueInterfaceArray(c))