fix for template redefinition. test cases added.
This commit is contained in:
parent
d1d5fc8254
commit
6975ba401b
8 changed files with 99 additions and 15 deletions
|
|
@ -567,6 +567,19 @@ proc StrTableRawInsert(data: var TSymSeq, n: PSym) =
|
||||||
assert(data[h] == nil)
|
assert(data[h] == nil)
|
||||||
data[h] = n
|
data[h] = n
|
||||||
|
|
||||||
|
proc SymTabReplaceRaw(data: var TSymSeq, prevSym: PSym, newSym: PSym) =
|
||||||
|
assert prevSym.name.h == newSym.name.h
|
||||||
|
var h: THash = prevSym.name.h and high(data)
|
||||||
|
while data[h] != nil:
|
||||||
|
if data[h] == prevSym:
|
||||||
|
data[h] = newSym
|
||||||
|
return
|
||||||
|
h = nextTry(h, high(data))
|
||||||
|
assert false
|
||||||
|
|
||||||
|
proc SymTabReplace*(t: var TStrTable, prevSym: PSym, newSym: PSym) =
|
||||||
|
SymTabReplaceRaw(t.data, prevSym, newSym)
|
||||||
|
|
||||||
proc StrTableEnlarge(t: var TStrTable) =
|
proc StrTableEnlarge(t: var TStrTable) =
|
||||||
var n: TSymSeq
|
var n: TSymSeq
|
||||||
newSeq(n, len(t.data) * growthFactor)
|
newSeq(n, len(t.data) * growthFactor)
|
||||||
|
|
|
||||||
|
|
@ -1269,7 +1269,6 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
|
||||||
if s != nil:
|
if s != nil:
|
||||||
case s.kind
|
case s.kind
|
||||||
of skMacro:
|
of skMacro:
|
||||||
echo c.filename
|
|
||||||
if c.filename.endsWith("hello.nim") and sfImmediate notin s.flags:
|
if c.filename.endsWith("hello.nim") and sfImmediate notin s.flags:
|
||||||
result = semDirectOp(c, n, flags)
|
result = semDirectOp(c, n, flags)
|
||||||
else:
|
else:
|
||||||
|
|
|
||||||
|
|
@ -191,14 +191,10 @@ proc semTemplateDef(c: PContext, n: PNode): PNode =
|
||||||
result = n
|
result = n
|
||||||
if n.sons[bodyPos].kind == nkEmpty:
|
if n.sons[bodyPos].kind == nkEmpty:
|
||||||
LocalError(n.info, errImplOfXexpected, s.name.s)
|
LocalError(n.info, errImplOfXexpected, s.name.s)
|
||||||
var proto = SearchForProc(c, s, c.tab.tos-2) # -2 because we have a scope
|
let curScope = c.tab.tos - 2 # -2 because we have a scope open for parameters
|
||||||
# open for parameters
|
var proto = SearchForProc(c, s, curScope)
|
||||||
if proto == nil:
|
if proto == nil:
|
||||||
# add identifier of template as a last step to not allow recursive templates:
|
|
||||||
addInterfaceOverloadableSymAt(c, s, c.tab.tos - 2)
|
addInterfaceOverloadableSymAt(c, s, c.tab.tos - 2)
|
||||||
else:
|
else:
|
||||||
# overwrite template
|
SymTabReplace(c.tab.stack[curScope], proto, s)
|
||||||
proto.info = s.info
|
|
||||||
proto.ast = s.ast
|
|
||||||
proto.typ = s.typ
|
|
||||||
proto.flags = s.flags
|
|
||||||
|
|
|
||||||
29
tests/compile/tredef.nim
Normal file
29
tests/compile/tredef.nim
Normal 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()
|
||||||
9
tests/reject/tprocredef.nim
Normal file
9
tests/reject/tprocredef.nim
Normal 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
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
discard """
|
discard """
|
||||||
file: "toverl2.nim"
|
file: "toverl2.nim"
|
||||||
output: "true012"
|
output: "true012innertrue"
|
||||||
"""
|
"""
|
||||||
# Test new overloading resolution rules
|
# Test new overloading resolution rules
|
||||||
|
|
||||||
|
|
@ -14,14 +14,20 @@ iterator toverl2(x: int): int =
|
||||||
while res < x:
|
while res < x:
|
||||||
yield res
|
yield res
|
||||||
inc(res)
|
inc(res)
|
||||||
|
|
||||||
var
|
var
|
||||||
pp: proc (x: bool): string = toverl2
|
pp: proc (x: bool): string = toverl2
|
||||||
|
|
||||||
stdout.write(pp(true))
|
stdout.write(pp(true))
|
||||||
|
|
||||||
for x in toverl2(3):
|
for x in toverl2(3):
|
||||||
stdout.write(toverl2(x))
|
stdout.write(toverl2(x))
|
||||||
|
|
||||||
|
block:
|
||||||
|
proc toverl2(x: int): string = return "inner"
|
||||||
|
stdout.write(toverl2(5))
|
||||||
|
stdout.write(true)
|
||||||
|
|
||||||
stdout.write("\n")
|
stdout.write("\n")
|
||||||
#OUT true012
|
#OUT true012innertrue
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,2 @@
|
||||||
import uclosures
|
import uclosures, utemplates
|
||||||
|
|
||||||
|
|
|
||||||
32
tests/run/utemplates.nim
Normal file
32
tests/run/utemplates.nim
Normal 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"
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue