Skip to content

Commit

Permalink
fixes strictdefs with when nimvm (#24409)
Browse files Browse the repository at this point in the history
ref #24225
related #24306

> 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 test shouldn't have passed when
#24306
would be implemented somehow. Some third packages have already misused
`when nimvm` by defining symbols in the other branch of `when nimvm`.

e.g. in status-im/nim-unittest2#34

```nim
when nimvm:
  discard
else:
  let suiteName {.inject.} = nameParam

use(suiteName)
```
  • Loading branch information
ringabout authored Nov 5, 2024
1 parent f4b9fcc commit c71de10
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/sempass2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,10 @@ proc track(tracked: PEffects, n: PNode) =
let last = lastSon(child)
track(tracked, last)
of nkCaseStmt: trackCase(tracked, n)
of nkWhen, nkIfStmt, nkIfExpr: trackIf(tracked, n)
of nkWhen: # This should be a "when nimvm" node.
track(tracked, n[0][1])
track(tracked, n[1][0])
of nkIfStmt, nkIfExpr: trackIf(tracked, n)
of nkBlockStmt, nkBlockExpr: trackBlock(tracked, n[1])
of nkWhileStmt:
# 'while true' loop?
Expand Down
30 changes: 30 additions & 0 deletions tests/init/tlet.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
discard """
joinable: false
"""

{.experimental: "strictDefs".}

proc bar(x: out string) =
Expand Down Expand Up @@ -53,3 +57,29 @@ proc foo() =

static: foo()
foo()

proc foo2 =
when nimvm:
discard
else:
let x = 1
doAssert x == 1

when false:
discard
else:
let y = 2

doAssert y == 2

const e = 1
when e == 0:
discard
elif e == 1:
let z = 3
else:
discard

doAssert z == 3

foo2()

0 comments on commit c71de10

Please sign in to comment.