fixes #25949; nimvm + staticRead code executed in runtime context (#25950)

fixes #25949

track still walks both branches of preserved when nimvm, but it now sets
a small inNimvmBranch flag while visiting the VM branch, and trackCall
skips sfCompileTime marking only when that flag is set.
This commit is contained in:
ringabout
2026-07-02 18:02:00 +08:00
committed by GitHub
parent fa4f9c9759
commit 3b9100178e
2 changed files with 18 additions and 1 deletions

View File

@@ -93,6 +93,7 @@ type
graph: ModuleGraph
c: PContext
escapingParams: IntSet
inNimvmBranch: int
PEffects = var TEffects
const
@@ -1127,7 +1128,7 @@ proc trackCall(tracked: PEffects; n: PNode) =
# sfCompileTime` path in `semProcAux`.
if a.kind == nkSym and a.sym.magic in {mNLen..mNError, mSlurp..mQuoteAst} and
tracked.owner != nil and tracked.owner.kind in routineKinds and
tracked.config.cmd != cmdNimscript:
tracked.config.cmd != cmdNimscript and tracked.inNimvmBranch == 0:
# ...but NOT under `nim e`: nimscript has no codegen backend to protect, and
# marking a routine `sfCompileTime` makes `semExpr` eagerly fold calls to it
# at sem time (emConst), where module-level globals it reads have no VM slot
@@ -1483,7 +1484,9 @@ proc track(tracked: PEffects, n: PNode) =
of nkCaseStmt: trackCase(tracked, n)
of nkWhen: # This should be a "when nimvm" node.
let oldState = tracked.init.len
inc tracked.inNimvmBranch
track(tracked, n[0][1])
dec tracked.inNimvmBranch
tracked.init.setLen(oldState)
track(tracked, n[1][0])
of nkIfStmt, nkIfExpr: trackIf(tracked, n)

View File

@@ -855,3 +855,17 @@ block:
var r = Obj(x: 10)
r.value = 42
doAssert r.x == 42
block: # bug #25949
template loadFile(filename: string): auto =
when nimvm:
staticRead(filename)
else:
"something"
proc roundTrip(): bool =
let content = loadFile("tests/tomls/case.toml")
content == "something"
doAssert roundTrip()