fixes #24997; {.global.} variable in recursive function (#25016)

fixes #24997

handles functions in recursive order

(cherry picked from commit 3ce38f2959)
This commit is contained in:
ringabout
2025-06-27 16:17:16 +08:00
committed by narimiran
parent a5ade112cb
commit 1a2ee566e3
2 changed files with 29 additions and 7 deletions

View File

@@ -2170,6 +2170,20 @@ proc addHcrInitGuards(p: BProc, n: PNode, inInitGuard: var bool) =
genStmts(p, n)
proc handleProcGlobals(m: BModule) =
var procGlobals: seq[PNode] = move m.g.graph.procGlobals
for i in 0..<procGlobals.len:
var stmts = newBuilder("")
# fixes recursive calls #24997
swap stmts, m.preInitProc.s(cpsStmts)
genStmts(m.preInitProc, procGlobals[i])
swap stmts, m.preInitProc.s(cpsStmts)
handleProcGlobals(m)
m.preInitProc.s(cpsStmts).add stmts.extract()
proc genTopLevelStmt*(m: BModule; n: PNode) =
## Also called from `ic/cbackend.nim`.
if pipelineutils.skipCodegen(m.config, n): return
@@ -2185,13 +2199,7 @@ proc genTopLevelStmt*(m: BModule; n: PNode) =
else:
genProcBody(m.initProc, transformedN)
var procGloals = move m.g.graph.procGlobals
while true:
if procGloals.len == 0:
procGloals = move m.g.graph.procGlobals
if procGloals.len == 0:
break
genStmts(m.preInitProc, procGloals.pop())
handleProcGlobals(m)
proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
if optForceFullMake notin m.config.globalOptions:

View File

@@ -41,3 +41,17 @@ block: # bug #24981
type Foo = object
i: int
m(Foo)
block: # bug #24997
type R = ref object
type B = object
j: int
proc y(T: type): R
proc u(T: type): R =
let res {.global.} = y(T)
res
proc y(T: type): R =
when T is object:
doAssert not isNil(u(typeof(B.j)))
R()
discard u(B)