From fcba14707a06271f6e14b6c5641d6d5fbc96ff0d Mon Sep 17 00:00:00 2001 From: metagn Date: Sun, 23 Mar 2025 06:59:06 +0300 Subject: [PATCH] disable "dest register is set" for vm statements (#24797) closes #24780 This proc `genStmt` is only called to run the VM in `vm.evalStmt`, otherwise it's not used in vmgen. Now it acts the same as `proc gen(PCtx, PNode)`, used by `discard` statements, which just calls `freeTemp` on the dest if it was set rather than erroring. --- compiler/vmgen.nim | 6 ++++-- tests/test_nimscript.nims | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 6e47f6fe44..68ef99395e 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -2313,9 +2313,11 @@ proc genStmt*(c: PCtx; n: PNode): int = result = c.code.len var d: TDest = -1 c.gen(n, d) - c.gABC(n, opcEof) if d >= 0: - globalError(c.config, n.info, "VM problem: dest register is set") + # for discardable calls etc, otherwise not valid + freeTemp(c, d) + #globalError(c.config, n.info, "VM problem: dest register is set") + c.gABC(n, opcEof) proc genExpr*(c: PCtx; n: PNode, requiresValue = true): int = c.removeLastEof diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index 32b7d1416e..15e9d878d8 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -136,3 +136,10 @@ block: # cpDir, cpFile, dirExists, fileExists, mkDir, mvDir, mvFile, rmDir, rmF block: # check parseopt can get command line: discard initOptParser() + +# issue #24780: + +proc discardableCall(cmd: string): int {.discardable.} = + result = 123 + +discardableCall "echo hi"