Skip to content

Commit 3617d2e

Browse files
authored
fixes lastRead uses the when nimvm branch (#24834)
```nim proc foo = var x = "1234" var y = x when nimvm: discard else: var s = x doAssert s == "1234" doAssert y == "1234" static: foo() foo() ``` `dfa` chooses the `nimvm` branch, `x` is misread as a last read and `wasMoved`. `injectDestructor` is used for codegen and is not used for vmgen. It's reasonable to choose the codegen path instead of the `nimvm` path so the code works for codegen. Though the problem is often hidden by `cursorinference` or `optimizer`. found in #24831
1 parent f9c8775 commit 3617d2e

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

compiler/dfa.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,8 @@ proc gen(c: var Con; n: PNode) =
439439
genUse(c, n)
440440
of nkIfStmt, nkIfExpr: genIf(c, n)
441441
of nkWhenStmt:
442-
# This is "when nimvm" node. Chose the first branch.
443-
gen(c, n[0][1])
442+
# This is "when nimvm" node. Chose the second branch.
443+
gen(c, n[1][0])
444444
of nkCaseStmt: genCase(c, n)
445445
of nkWhileStmt: genWhile(c, n)
446446
of nkBlockExpr, nkBlockStmt: genBlock(c, n)

tests/destructor/t23837.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,18 @@ proc main() =
4848
let s = leakyWrapper()
4949
echo s
5050

51-
main()
51+
main()
52+
53+
block:
54+
proc foo =
55+
var x = "1234"
56+
var y = x
57+
when nimvm:
58+
discard
59+
else:
60+
var s = x
61+
doAssert s == "1234"
62+
doAssert y == "1234"
63+
64+
static: foo()
65+
foo()

0 commit comments

Comments
 (0)