From 41ec894cb0aa0b0d233a0f62ff4929d64ddad3a7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 29 Jun 2023 17:21:22 +0800 Subject: [PATCH] alternative to #22183; nimscript shares the same compileTime sym with VM (#22184) --- compiler/vmgen.nim | 6 +++++- tests/vm/mscriptcompiletime.nim | 7 +++++++ tests/vm/tscriptcompiletime.nims | 9 +++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/vm/mscriptcompiletime.nim create mode 100644 tests/vm/tscriptcompiletime.nims diff --git a/compiler/vmgen.nim b/compiler/vmgen.nim index 067965469c..50331b9710 100644 --- a/compiler/vmgen.nim +++ b/compiler/vmgen.nim @@ -1911,6 +1911,8 @@ proc genVarSection(c: PCtx; n: PNode) = let s = a[0].sym checkCanEval(c, a[0]) if s.isGlobal: + let runtimeAccessToCompileTime = c.mode == emRepl and + sfCompileTime in s.flags and s.position > 0 if s.position == 0: if importcCond(c, s): c.importcSym(a.info, s) else: @@ -1920,7 +1922,9 @@ proc genVarSection(c: PCtx; n: PNode) = assert sa.kind != nkCall c.globals.add(sa) s.position = c.globals.len - if a[2].kind != nkEmpty: + if runtimeAccessToCompileTime: + discard + elif a[2].kind != nkEmpty: let tmp = c.genx(a[0], {gfNodeAddr}) let val = c.genx(a[2]) c.genAdditionalCopy(a[2], opcWrDeref, tmp, 0, val) diff --git a/tests/vm/mscriptcompiletime.nim b/tests/vm/mscriptcompiletime.nim new file mode 100644 index 0000000000..ed7e7c029a --- /dev/null +++ b/tests/vm/mscriptcompiletime.nim @@ -0,0 +1,7 @@ +# bug.nim +var bar* {.compileTime.} = 1 + +proc dummy = discard + +static: + inc bar \ No newline at end of file diff --git a/tests/vm/tscriptcompiletime.nims b/tests/vm/tscriptcompiletime.nims new file mode 100644 index 0000000000..daec54bf7a --- /dev/null +++ b/tests/vm/tscriptcompiletime.nims @@ -0,0 +1,9 @@ +discard """ + cmd: "nim e $file" +""" + +import mscriptcompiletime + +macro foo = + doAssert bar == 2 +foo()