fixes #25284; .global initialization inside method hoisted to preInitProc (#25285)

fixes #25284

```nim
proc m2()  =
  let v {.global, used.}: string = f2(f2("123"))
```

transform lifted `.global`statements in the top level scope
This commit is contained in:
ringabout
2025-11-14 23:20:42 +08:00
committed by GitHub
parent f608e109c9
commit 9becd1453d
3 changed files with 13 additions and 3 deletions

View File

@@ -2449,7 +2449,10 @@ proc handleProcGlobals(m: BModule) =
# fixes recursive calls #24997
swap stmts, m.preInitProc.s(cpsStmts)
genStmts(m.preInitProc, procGlobals[i])
var transformedN = procGlobals[i]
if sfInjectDestructors in m.module.flags:
transformedN = injectDestructorCalls(m.g.graph, m.idgen, m.module, transformedN)
genStmts(m.preInitProc, transformedN)
swap stmts, m.preInitProc.s(cpsStmts)
handleProcGlobals(m)

View File

@@ -968,10 +968,10 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
{sfPure, sfGlobal} <= v.sym.flags and
isInProc
let value = moveOrCopy(v, ri, c, s, if v.kind == nkSym: {IsDecl} else: {})
if isGlobalPragma:
c.graph.procGlobals.add value
c.graph.procGlobals.add n
else:
let value = moveOrCopy(v, ri, c, s, if v.kind == nkSym: {IsDecl} else: {})
result.add value
elif ri.kind == nkEmpty and c.inLoop > 0:
let skipInit = v.kind == nkDotExpr and # Closure var

View File

@@ -55,3 +55,10 @@ block: # bug #24997
doAssert not isNil(u(typeof(B.j)))
R()
discard u(B)
proc f2(str: string): string = str
proc m2() =
let v {.global, used.}: string = f2(f2("123"))
assert v == "123"
m2()