From 4e4d466d060f9b55cb2adb3ad02e212281df4962 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Sun, 5 Nov 2017 16:53:05 +0100 Subject: [PATCH] minor breaking change: for loop bodies now get their own scope --- changelog.md | 8 ++++++++ compiler/semgnrc.nim | 2 ++ compiler/semstmts.nim | 2 ++ compiler/semtempl.nim | 2 ++ 4 files changed, 14 insertions(+) diff --git a/changelog.md b/changelog.md index 5947c49e5..7aa52ece2 100644 --- a/changelog.md +++ b/changelog.md @@ -41,6 +41,14 @@ what to return if the environment variable does not exist. - Removed PDCurses wrapper from the stdlib and published it as a separate Nimble package. +- Bodies of ``for`` loops now get their own scope: + +.. code-block:: nim + # now compiles: + for i in 0..4: + let i = i + 1 + echo i + - The parsing rules of ``if`` expressions were changed so that multiple statements are allowed in the branches. We found few code examples that now fail because of this change, but here is one: diff --git a/compiler/semgnrc.nim b/compiler/semgnrc.nim index 3cdb68df6..e28a7851f 100644 --- a/compiler/semgnrc.nim +++ b/compiler/semgnrc.nim @@ -335,8 +335,10 @@ proc semGenericStmt(c: PContext, n: PNode, n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx) for i in countup(0, L - 3): addTempDecl(c, n.sons[i], skForVar) + openScope(c) n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx) closeScope(c) + closeScope(c) of nkBlockStmt, nkBlockExpr, nkBlockType: checkSonsLen(n, 2) openScope(c) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 540ef4c07..80fad3f23 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -697,7 +697,9 @@ proc semForVars(c: PContext, n: PNode): PNode = if sfGenSym notin v.flags and not isDiscardUnderscore(v): addForVarDecl(c, v) inc(c.p.nestedLoopCounter) + openScope(c) n.sons[length-1] = semStmt(c, n.sons[length-1]) + closeScope(c) dec(c.p.nestedLoopCounter) proc implicitIterator(c: PContext, it: string, arg: PNode): PNode = diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index 426290c78..1c9d8271a 100644 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -366,8 +366,10 @@ proc semTemplBody(c: var TemplCtx, n: PNode): PNode = n.sons[L-2] = semTemplBody(c, n.sons[L-2]) for i in countup(0, L - 3): addLocalDecl(c, n.sons[i], skForVar) + openScope(c) n.sons[L-1] = semTemplBody(c, n.sons[L-1]) closeScope(c) + closeScope(c) of nkBlockStmt, nkBlockExpr, nkBlockType: checkSonsLen(n, 2) openScope(c)