Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open scope for each when nimvm branch #24306

Draft
wants to merge 3 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2740,7 +2740,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 @@ -2771,10 +2773,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 @@ -3470,7 +3470,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
2 changes: 1 addition & 1 deletion tests/init/tlet.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ proc foo2 =
discard
else:
let x = 1
doAssert x == 1
doAssert not declared(x)

when false:
discard
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()
Loading