From 3617d2e077757373fdc3757565fd644a336f4f95 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 2 Apr 2025 15:29:15 +0800 Subject: [PATCH] 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 https://github.com/nim-lang/Nim/pull/24831 --- compiler/dfa.nim | 4 ++-- tests/destructor/t23837.nim | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/compiler/dfa.nim b/compiler/dfa.nim index 5534d07e7c..ef6a767f07 100644 --- a/compiler/dfa.nim +++ b/compiler/dfa.nim @@ -439,8 +439,8 @@ proc gen(c: var Con; n: PNode) = genUse(c, n) of nkIfStmt, nkIfExpr: genIf(c, n) of nkWhenStmt: - # This is "when nimvm" node. Chose the first branch. - gen(c, n[0][1]) + # This is "when nimvm" node. Chose the second branch. + gen(c, n[1][0]) of nkCaseStmt: genCase(c, n) of nkWhileStmt: genWhile(c, n) of nkBlockExpr, nkBlockStmt: genBlock(c, n) diff --git a/tests/destructor/t23837.nim b/tests/destructor/t23837.nim index e219dd6b55..7ee20fee41 100644 --- a/tests/destructor/t23837.nim +++ b/tests/destructor/t23837.nim @@ -48,4 +48,18 @@ proc main() = let s = leakyWrapper() echo s -main() \ No newline at end of file +main() + +block: + proc foo = + var x = "1234" + var y = x + when nimvm: + discard + else: + var s = x + doAssert s == "1234" + doAssert y == "1234" + + static: foo() + foo()