Skip to content

Commit

Permalink
open scope for each when nimvm branch
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Oct 14, 2024
1 parent 34c87de commit dd87f57
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,9 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
checkSonsLen(it, 2, c.config)
if whenNimvm:
if semCheck:
openScope(c)
it[1] = semExpr(c, it[1], flags)
closeScope(c)
typ = commonType(c, typ, it[1].typ)
result = n # when nimvm is not elimited until codegen
elif c.inGenericContext > 0:
Expand Down Expand Up @@ -2758,10 +2760,12 @@ proc semWhen(c: PContext, n: PNode, semCheck = true): PNode =
discard
elif result == nil or whenNimvm:
if semCheck:
if whenNimvm: openScope(c)
it[0] = semExpr(c, it[0], flags)
typ = commonType(c, typ, it[0].typ)
if typ != nil and typ.kind != tyUntyped:
it[0] = fitNode(c, typ, it[0], it[0].info)
if whenNimvm: closeScope(c)
if result == nil:
result = it[0]
else: illFormedAst(n, c.config)
Expand Down
2 changes: 1 addition & 1 deletion doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -3466,7 +3466,7 @@ A `when nimvm` statement must meet the following requirements:
* It must contain an `else` branch.
* Code in branches must not affect semantics of the code that follows the
`when nimvm` statement. E.g. it must not define symbols that are used in
the following code.
the following code. A new scope is opened for each branch to prevent this.

Return statement
----------------
Expand Down
13 changes: 13 additions & 0 deletions tests/vm/twhennimvmscope1.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# issue #23687

when nimvm:
proc mytest(a: int) =
echo a
else:
template mytest(a: int) =
echo a + 42


proc xxx() =
mytest(100) #[tt.Error
^ undeclared identifier: 'mytest']#
14 changes: 14 additions & 0 deletions tests/vm/twhennimvmscope2.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# issue #23688

when nimvm:
proc mytest(a: int) =
echo a
else:
template mytest(a: untyped) =
echo a + 42


proc xxx() =
mytest(100) #[tt.Error
^ undeclared identifier: 'mytest']#
xxx()
10 changes: 10 additions & 0 deletions tests/vm/twhennimvmscope3.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# issue #13450 example 3

proc bar() =
when nimvm:
let y = 1
else:
let y = 2
discard y #[tt.Error
^ undeclared identifier: 'y']#
bar()

0 comments on commit dd87f57

Please sign in to comment.