From 95436893061176d51b1c11fdf5418b3ed17a8455 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 24 Jun 2018 18:27:40 +0200 Subject: [PATCH 1/2] Make `static` blocks introduce their own scope Treat the static block as a normal block, don't leak any identifier in the outer scope. Fixes #5958 --- compiler/semstmts.nim | 2 ++ tests/global/t5958.nim | 9 +++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/global/t5958.nim diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 292238dc9e..fe4318de50 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1772,7 +1772,9 @@ proc semStaticStmt(c: PContext, n: PNode): PNode = #echo "semStaticStmt" #writeStackTrace() inc c.inStaticContext + openScope(c) let a = semStmt(c, n.sons[0]) + closeScope(c) dec c.inStaticContext n.sons[0] = a evalStaticStmt(c.module, c.graph, a, c.p.owner) diff --git a/tests/global/t5958.nim b/tests/global/t5958.nim new file mode 100644 index 0000000000..5abcad4a9c --- /dev/null +++ b/tests/global/t5958.nim @@ -0,0 +1,9 @@ +discard """ + line: 9 + errormsg: "undeclared identifier: 'a'" +""" + +static: + var a = 1 + +echo a From f559e62e45b4be227d494e75fe82f571ee410840 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 25 Jun 2018 15:56:13 +0200 Subject: [PATCH 2/2] Adjust some tests to make them pass The non-scoped behaviour of static blocks was exploited by those tests, replace all the variables declared whithin one with compileTime marked ones. --- tests/pragmas/treorder.nim | 3 +-- tests/vm/tnimnode.nim | 20 +++++++++----------- tests/vm/tvmmisc.nim | 4 ++-- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/pragmas/treorder.nim b/tests/pragmas/treorder.nim index 6a6bbff4db..1006af5274 100644 --- a/tests/pragmas/treorder.nim +++ b/tests/pragmas/treorder.nim @@ -71,5 +71,4 @@ macro make(arg: untyped): untyped = proc first(i: int): void = make(second) -static: - var ss: string = "" \ No newline at end of file +var ss {.compileTime.}: string = "" \ No newline at end of file diff --git a/tests/vm/tnimnode.nim b/tests/vm/tnimnode.nim index 188bac9bf3..4210ab41dd 100644 --- a/tests/vm/tnimnode.nim +++ b/tests/vm/tnimnode.nim @@ -4,14 +4,12 @@ proc assertEq(arg0,arg1: string): void = if arg0 != arg1: raiseAssert("strings not equal:\n" & arg0 & "\n" & arg1) -static: - # a simple assignment of stmtList to another variable - var node: NimNode - # an assignment of stmtList into an array - var nodeArray: array[1, NimNode] - # an assignment of stmtList into a seq - var nodeSeq = newSeq[NimNode](2) - +# a simple assignment of stmtList to another variable +var node {.compileTime.}: NimNode +# an assignment of stmtList into an array +var nodeArray {.compileTime.}: array[1, NimNode] +# an assignment of stmtList into a seq +var nodeSeq {.compileTime.} = newSeq[NimNode](2) proc checkNode(arg: NimNode; name: string): void {. compileTime .} = echo "checking ", name @@ -35,10 +33,10 @@ proc checkNode(arg: NimNode; name: string): void {. compileTime .} = echo "OK" -static: - # the root node that is used to generate the Ast - var stmtList: NimNode +# the root node that is used to generate the Ast +var stmtList {.compileTime.}: NimNode +static: stmtList = newStmtList(nnkDiscardStmt.newTree(newEmptyNode())) checkNode(stmtList, "direct construction") diff --git a/tests/vm/tvmmisc.nim b/tests/vm/tvmmisc.nim index 4af824cf47..80f5aeee00 100644 --- a/tests/vm/tvmmisc.nim +++ b/tests/vm/tvmmisc.nim @@ -19,9 +19,9 @@ block: var x = default(type(0)) # #6379 -static: - import algorithm +import algorithm +static: var numArray = [1, 2, 3, 4, -1] numArray.sort(cmp) assert numArray == [-1, 1, 2, 3, 4]