summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authormetagn <metagngn@gmail.com>2024-09-09 12:29:30 +0300
committerGitHub <noreply@github.com>2024-09-09 11:29:30 +0200
commita6595e5b49ffa94947a2e5767263f523cd827c44 (patch)
tree59d0af7aa0d049257c1d82ef5d554cd5202486ab /compiler
parent9ff0333a4ccffb253f03fc113d086bbc81b566e9 (diff)
downloadNim-a6595e5b49ffa94947a2e5767263f523cd827c44.tar.gz
open new scope for const values (#24084)
fixes #5395

Previously values of `const` statements used the same scope as the
`const` statement itself, meaning variables could be declared inside
them and referred to in other statements in the same block. Now each
`const` value opens its own scope, so any variable declared in the value
of a constant can only be accessed for that constant.

We could change this to open a new scope for the `const` *section*
rather than each constant, so the variables can be used in other
constants, but I'm not sure if this is sound.
Diffstat (limited to 'compiler')
-rw-r--r--compiler/semstmts.nim2
1 files changed, 2 insertions, 0 deletions
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index ce928eef9..334d10be7 100644
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -985,6 +985,7 @@ proc semConst(c: PContext, n: PNode): PNode =
     var typFlags: TTypeAllowedFlags = {}
 
     # don't evaluate here since the type compatibility check below may add a converter
+    openScope(c)
     var def = semExprWithType(c, a[^1], {efTypeAllowed}, typ)
 
     if def.kind == nkSym and def.sym.kind in {skTemplate, skMacro}:
@@ -1011,6 +1012,7 @@ proc semConst(c: PContext, n: PNode): PNode =
       if c.matchedConcept != nil:
         typFlags.incl taConcept
       typeAllowedCheck(c, a.info, typ, skConst, typFlags)
+    closeScope(c)
 
     if a.kind == nkVarTuple:
       # generate new section from tuple unpacking and embed it into this one