summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2018-07-03 15:10:12 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-07-03 15:10:12 +0200
commitab47a870bce94cb33c66f2b27ecb1c62f48ae783 (patch)
treeb4d86f431110363c4962ba5fd343eb17e8143180
parent426e5c2d1f0c0a607fb5384f9ef700c45d3332f8 (diff)
downloadNim-ab47a870bce94cb33c66f2b27ecb1c62f48ae783.tar.gz
Error out if vararg match isn't an exact one (#8186)
Fixes #8172
-rw-r--r--compiler/sigmatch.nim12
-rw-r--r--tests/typerel/t8172.nim11
2 files changed, 22 insertions, 1 deletions
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 84e59349e..523783b36 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -2308,6 +2308,7 @@ proc matchesAux(c: PContext, n, nOrig: PNode,
             m.firstMismatch = f
             return
           if m.baseTypeMatch:
+            assert formal.typ.kind == tyVarargs
             #assert(container == nil)
             if container.isNil:
               container = newNodeIT(nkBracket, n.sons[a].info, arrayConstr(c, arg))
@@ -2321,10 +2322,19 @@ proc matchesAux(c: PContext, n, nOrig: PNode,
 
             # pick the formal from the end, so that 'x, y, varargs, z' works:
             f = max(f, formalLen - n.len + a + 1)
-          else:
+          elif formal.typ.kind != tyVarargs or container == nil:
             setSon(m.call, formal.position + 1, arg)
             inc(f)
             container = nil
+          else:
+            # we end up here if the argument can be converted into the varargs
+            # formal (eg. seq[T] -> varargs[T]) but we have already instantiated
+            # a container
+            assert arg.kind == nkHiddenStdConv
+            localError(c.config, n.sons[a].info, "cannot convert $1 to $2" % [
+              typeToString(n.sons[a].typ), typeToString(formal.typ) ])
+            m.state = csNoMatch
+            return
         checkConstraint(n.sons[a])
     inc(a)
 
diff --git a/tests/typerel/t8172.nim b/tests/typerel/t8172.nim
new file mode 100644
index 000000000..8e0b32932
--- /dev/null
+++ b/tests/typerel/t8172.nim
@@ -0,0 +1,11 @@
+discard """
+  line: 11
+  errormsg: "cannot convert array[0..0, string] to varargs[string]"
+"""
+
+proc f(v: varargs[string]) =
+  echo(v)
+
+f("b", "c")   # Works
+f(["b", "c"]) # Works
+f("b", ["c"]) # Fails