Lock semchecked ast for macros (#11883) [bugfix]
* reject to modify type checked AST * add flag to back out * Introduce legacy feature set.
This commit is contained in:
parent
fda51b6ca2
commit
44e7a7b6c2
8 changed files with 83 additions and 24 deletions
12
tests/macros/tlocktypednode1.nim
Normal file
12
tests/macros/tlocktypednode1.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
errormsg: "typechecked nodes may not be modified"
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
macro doSomething(arg: typed): untyped =
|
||||
echo arg.treeREpr
|
||||
result = arg
|
||||
result.add newCall(bindSym"echo", newLit(1))
|
||||
|
||||
doSomething((echo(1); echo(2)))
|
||||
12
tests/macros/tlocktypednode2.nim
Normal file
12
tests/macros/tlocktypednode2.nim
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
discard """
|
||||
errormsg: "typechecked nodes may not be modified"
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
macro doSomething(arg: typed): untyped =
|
||||
echo arg.treeREpr
|
||||
result = arg
|
||||
result[0] = newCall(bindSym"echo", newLit(1))
|
||||
|
||||
doSomething((echo(1); echo(2)))
|
||||
15
tests/macros/tlocktypednode3.nim
Normal file
15
tests/macros/tlocktypednode3.nim
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
discard """
|
||||
errormsg: "typechecked nodes may not be modified"
|
||||
"""
|
||||
|
||||
import macros
|
||||
|
||||
macro doSomething(arg: typed): untyped =
|
||||
echo arg.treeREpr
|
||||
result = arg
|
||||
result.add(
|
||||
newCall(bindSym"echo", newLit(3)),
|
||||
newCall(bindSym"echo", newLit(1))
|
||||
)
|
||||
|
||||
doSomething((echo(1); echo(2)))
|
||||
|
|
@ -1,19 +1,20 @@
|
|||
discard """
|
||||
output: '''calling!stuff
|
||||
output: '''
|
||||
calling!stuff
|
||||
calling!stuff
|
||||
'''
|
||||
joinable: false
|
||||
disabled: true
|
||||
"""
|
||||
|
||||
# this test modifies an already semchecked ast (bad things happen)
|
||||
# this test relies on the bug #4547
|
||||
# issue #7792
|
||||
|
||||
import macros
|
||||
|
||||
|
||||
proc callProc(str: string) =
|
||||
echo "calling!" & str
|
||||
|
||||
|
||||
macro testMacro(code: typed): untyped =
|
||||
let stmtList = newNimNode(nnkStmtList)
|
||||
|
||||
|
|
@ -27,11 +28,9 @@ macro testMacro(code: typed): untyped =
|
|||
|
||||
result = newEmptyNode()
|
||||
|
||||
|
||||
proc main() {.testMacro.} =
|
||||
echo "test"
|
||||
echo "test2"
|
||||
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue