From 3c528c987c86b7b1089571a445c84bae4c1903f9 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sun, 27 Oct 2024 02:49:07 +0800 Subject: [PATCH] fixes #24359; VM problem: dest register is not set with const-bound proc (#24364) fixes #24359 follow up https://github.com/nim-lang/Nim/pull/11076 It should not try to evaluate the const proc if the proc doesn't have a return value. (cherry picked from commit 031ad957badd1d2f73318a3aec2fa50b4687634c) --- compiler/semexprs.nim | 4 ++-- tests/vm/tconstprocassignments.nim | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index a4f4c5927a..60a9c56f9c 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -1006,9 +1006,9 @@ proc evalAtCompileTime(c: PContext, n: PNode): PNode = n.typ.flags.incl tfUnresolved # optimization pass: not necessary for correctness of the semantic pass - if callee.kind == skConst or + if (callee.kind == skConst or {sfNoSideEffect, sfCompileTime} * callee.flags != {} and - {sfForward, sfImportc} * callee.flags == {} and n.typ != nil: + {sfForward, sfImportc} * callee.flags == {}) and n.typ != nil: if callee.kind != skConst and sfCompileTime notin callee.flags and diff --git a/tests/vm/tconstprocassignments.nim b/tests/vm/tconstprocassignments.nim index 0e2d2ed165..d38872104e 100644 --- a/tests/vm/tconstprocassignments.nim +++ b/tests/vm/tconstprocassignments.nim @@ -16,3 +16,18 @@ const G = proc ():int = y() echo G() + + +block: # bug #24359 + block: + proc h(_: bool) = discard + const m = h + static: m(true) # works + m(true) # does not work + +block: + block: + proc h(_: bool): int = result = 1 + const m = h + static: doAssert m(true) == 1 # works + doAssert m(true) == 1 # does not work