new .redefine pragma for templates, warn on redefinition without it (#20211)
* test CI for template redefinitions * adapt asyncmacro * fix quote * fix again * try something else * revert * fix ioselectors_select, disable packages CI * adapt more tests & simplify * more * more * more * rename to redefine, warn on implicit redefinition * basic documentation [skip ci] * Update compiler/lineinfos.nim Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com> Co-authored-by: konsumlamm <44230978+konsumlamm@users.noreply.github.com>
This commit is contained in:
parent
14656154ef
commit
d1d141b135
19 changed files with 85 additions and 25 deletions
|
|
@ -4,7 +4,7 @@ foo(1, "test")
|
|||
proc bar(a: int, b: string) = discard
|
||||
bar(1, "test")
|
||||
|
||||
template foo(a: int, b: string) = bar(a, b)
|
||||
template foo(a: int, b: string) {.redefine.} = bar(a, b)
|
||||
foo(1, "test")
|
||||
|
||||
block:
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ macro mergeScopes(scopeHolders: typed, newBindings: untyped): untyped =
|
|||
newScopeDefinition.add newAssignment(newIdentNode(k), v)
|
||||
|
||||
result = quote:
|
||||
template scopeHolder = `newScopeDefinition`
|
||||
template scopeHolder {.redefine.} = `newScopeDefinition`
|
||||
|
||||
template scope(newBindings: untyped) {.dirty.} =
|
||||
mergeScopes(bindSym"scopeHolder", newBindings)
|
||||
|
|
|
|||
|
|
@ -158,7 +158,7 @@ template dim2: int =
|
|||
else:
|
||||
int.high)
|
||||
|
||||
template dim: int =
|
||||
template dim3: int =
|
||||
(
|
||||
if int.high == 0:
|
||||
int.high
|
||||
|
|
|
|||
|
|
@ -274,10 +274,10 @@ parse9:
|
|||
block gensym1:
|
||||
template x: untyped = -1
|
||||
template t1() =
|
||||
template x: untyped {.gensym.} = 1
|
||||
template x: untyped {.gensym, redefine.} = 1
|
||||
echo x() # 1
|
||||
template t2() =
|
||||
template x: untyped = 1 # defaults to {.inject.}
|
||||
template x: untyped {.redefine.} = 1 # defaults to {.inject.}
|
||||
echo x() # -1 injected x not available during template definition
|
||||
t1()
|
||||
t2()
|
||||
|
|
|
|||
33
tests/template/tredefinition_override.nim
Normal file
33
tests/template/tredefinition_override.nim
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
{.push warningAsError[TemplateRedefinition]: on.}
|
||||
|
||||
doAssert not (compiles do:
|
||||
template foo(): int = 1
|
||||
template foo(): int = 2)
|
||||
doAssert (compiles do:
|
||||
template foo(): int = 1
|
||||
template foo(): int {.redefine.} = 2)
|
||||
doAssert not (compiles do:
|
||||
block:
|
||||
template foo() =
|
||||
template bar: string {.gensym.} = "a"
|
||||
template bar: string {.gensym.} = "b"
|
||||
foo())
|
||||
doAssert (compiles do:
|
||||
block:
|
||||
template foo() =
|
||||
template bar: string {.gensym.} = "a"
|
||||
template bar: string {.gensym, redefine.} = "b"
|
||||
foo())
|
||||
|
||||
block:
|
||||
template foo(): int = 1
|
||||
template foo(): int {.redefine.} = 2
|
||||
doAssert foo() == 2
|
||||
block:
|
||||
template foo(): string =
|
||||
template bar: string {.gensym.} = "a"
|
||||
template bar: string {.gensym, redefine.} = "b"
|
||||
bar()
|
||||
doAssert foo() == "b"
|
||||
|
||||
{.pop.}
|
||||
|
|
@ -22,7 +22,7 @@ block: # templates can be redefined multiple times
|
|||
if not cond: fail(msg)
|
||||
|
||||
template assertionFailed(body: untyped) {.dirty.} =
|
||||
template fail(msg: string): typed =
|
||||
template fail(msg: string): typed {.redefine.} =
|
||||
body
|
||||
|
||||
assertionFailed:
|
||||
|
|
|
|||
|
|
@ -33,8 +33,8 @@ block tpartial:
|
|||
proc p(x, y: int; cond: bool): int =
|
||||
result = if cond: x + y else: x - y
|
||||
|
||||
template optP{p(x, y, true)}(x, y): untyped = x - y
|
||||
template optP{p(x, y, false)}(x, y): untyped = x + y
|
||||
template optPTrue{p(x, y, true)}(x, y): untyped = x - y
|
||||
template optPFalse{p(x, y, false)}(x, y): untyped = x + y
|
||||
|
||||
echo p(2, 4, true)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue