diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2016-04-09 13:06:38 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2016-04-09 13:06:38 +0200 |
commit | 1d5c28fc14b92a2b2df4e9d6fa4b1fee0e213685 (patch) | |
tree | 9a237aba08cc8a156b7a625afc8866ac9b6f6c8e | |
parent | 7d3f47cef91a07549bb568d5ac538df3bf312887 (diff) | |
parent | 3922a0871dfafa7a57155db19efa15b73e3d7fb7 (diff) | |
download | Nim-1d5c28fc14b92a2b2df4e9d6fa4b1fee0e213685.tar.gz |
Merge pull request #4050 from yglukhov/tr-varargs
Fixed tr pattern matching for varargs
-rw-r--r-- | compiler/patterns.nim | 2 | ||||
-rw-r--r-- | compiler/types.nim | 12 | ||||
-rw-r--r-- | tests/trmacros/tpatterns.nim | 8 |
3 files changed, 20 insertions, 2 deletions
diff --git a/compiler/patterns.nim b/compiler/patterns.nim index 604d3521d..2336e44e7 100644 --- a/compiler/patterns.nim +++ b/compiler/patterns.nim @@ -129,7 +129,7 @@ proc matchNested(c: PPatternContext, p, n: PNode, rpn: bool): bool = result = bindOrCheck(c, p.sons[2].sym, arglist) proc matches(c: PPatternContext, p, n: PNode): bool = - # hidden conversions (?) + let n = skipHidden(n) if nfNoRewrite in n.flags: result = false elif isPatternParam(c, p): diff --git a/compiler/types.nim b/compiler/types.nim index 9aa991086..c9cbfedb1 100644 --- a/compiler/types.nim +++ b/compiler/types.nim @@ -1448,6 +1448,18 @@ proc skipConv*(n: PNode): PNode = result = n.sons[1] else: discard +proc skipHidden*(n: PNode): PNode = + result = n + while true: + case result.kind + of nkHiddenStdConv, nkHiddenSubConv: + if result.sons[1].typ.classify == result.typ.classify: + result = result.sons[1] + else: break + of nkHiddenDeref, nkHiddenAddr: + result = result.sons[0] + else: break + proc skipConvTakeType*(n: PNode): PNode = result = n.skipConv result.typ = n.typ diff --git a/tests/trmacros/tpatterns.nim b/tests/trmacros/tpatterns.nim index 6bc8772e3..907973637 100644 --- a/tests/trmacros/tpatterns.nim +++ b/tests/trmacros/tpatterns.nim @@ -1,6 +1,7 @@ discard """ output: '''48 -hel''' +hel +lo''' """ template optZero{x+x}(x: int): int = x*3 @@ -15,3 +16,8 @@ s[0] = "hello" s[0] = substr(s[0], 0, 2) echo s[0] + +# Test varargs matching +proc someVarargProc(k: varargs[string]) = doAssert(false) # this should not get called +template someVarargProcSingleArg{someVarargProc([a])}(a: string) = echo a +someVarargProc("lo") |