fix for template redefinition. test cases added.

This commit is contained in:
Zahary Karadjov 2012-03-15 14:15:37 +02:00
commit 6975ba401b
8 changed files with 99 additions and 15 deletions

29
tests/compile/tredef.nim Normal file
View file

@ -0,0 +1,29 @@
template foo(a: int, b: string) = nil
foo(1, "test")
proc bar(a: int, b: string) = nil
bar(1, "test")
template foo(a: int, b: string) = bar(a, b)
foo(1, "test")
block:
proc bar(a: int, b: string) = nil
template foo(a: int, b: string) = nil
foo(1, "test")
bar(1, "test")
proc baz =
proc foo(a: int, b: string) = nil
proc foo(b: string) =
template bar(a: int, b: string) = nil
bar(1, "test")
foo("test")
block:
proc foo(b: string) = nil
foo("test")
foo(1, "test")
baz()

View file

@ -0,0 +1,9 @@
discard """
file: "tprocredef.nim"
line: 8
errormsg: "redefinition of \'foo\'"
"""
proc foo(a: int, b: string) = nil
proc foo(a: int, b: string) = nil

View file

@ -1,6 +1,6 @@
discard """
file: "toverl2.nim"
output: "true012"
output: "true012innertrue"
"""
# Test new overloading resolution rules
@ -14,14 +14,20 @@ iterator toverl2(x: int): int =
while res < x:
yield res
inc(res)
var
pp: proc (x: bool): string = toverl2
stdout.write(pp(true))
for x in toverl2(3):
stdout.write(toverl2(x))
block:
proc toverl2(x: int): string = return "inner"
stdout.write(toverl2(5))
stdout.write(true)
stdout.write("\n")
#OUT true012
#OUT true012innertrue

View file

@ -1,2 +1,2 @@
import uclosures
import uclosures, utemplates

32
tests/run/utemplates.nim Normal file
View file

@ -0,0 +1,32 @@
import unittest
template t(a: int): expr = "int"
template t(a: string): expr = "string"
test "templates can be overloaded":
check t(10) == "int"
check t("test") == "string"
test "previous definitions can be further overloaded or hidden in local scopes":
template t(a: bool): expr = "bool"
check t(true) == "bool"
check t(10) == "int"
template t(a: int): expr = "inner int"
check t(10) == "inner int"
check t("test") == "string"
test "templates can be redefined multiple times":
template customAssert(cond: bool, msg: string): stmt =
if not cond: fail(msg)
template assertion_failed(body: stmt) =
template fail(msg: string): stmt = body
assertion_failed: check msg == "first fail path"
customAssert false, "first fail path"
assertion_failed: check msg == "second fail path"
customAssert false, "second fail path"