Skip to content

Commit 3ce38f2

Browse files
authored
fixes #24997; {.global.} variable in recursive function (#25016)
fixes #24997 handles functions in recursive order
1 parent b6491e7 commit 3ce38f2

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

compiler/cgen.nim

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,20 @@ proc addHcrInitGuards(p: BProc, n: PNode, inInitGuard: var bool, init: var IfBui
24202420

24212421
genStmts(p, n)
24222422

2423+
proc handleProcGlobals(m: BModule) =
2424+
var procGlobals: seq[PNode] = move m.g.graph.procGlobals
2425+
2426+
for i in 0..<procGlobals.len:
2427+
var stmts = newBuilder("")
2428+
2429+
# fixes recursive calls #24997
2430+
swap stmts, m.preInitProc.s(cpsStmts)
2431+
genStmts(m.preInitProc, procGlobals[i])
2432+
swap stmts, m.preInitProc.s(cpsStmts)
2433+
2434+
handleProcGlobals(m)
2435+
m.preInitProc.s(cpsStmts).add stmts.extract()
2436+
24232437
proc genTopLevelStmt*(m: BModule; n: PNode) =
24242438
## Also called from `ic/cbackend.nim`.
24252439
if pipelineutils.skipCodegen(m.config, n): return
@@ -2435,13 +2449,7 @@ proc genTopLevelStmt*(m: BModule; n: PNode) =
24352449
else:
24362450
genProcBody(m.initProc, transformedN)
24372451

2438-
var procGloals = move m.g.graph.procGlobals
2439-
while true:
2440-
if procGloals.len == 0:
2441-
procGloals = move m.g.graph.procGlobals
2442-
if procGloals.len == 0:
2443-
break
2444-
genStmts(m.preInitProc, procGloals.pop())
2452+
handleProcGlobals(m)
24452453

24462454
proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool =
24472455
if optForceFullMake notin m.config.globalOptions:

tests/global/tglobal3.nim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,17 @@ block: # bug #24981
4141
type Foo = object
4242
i: int
4343
m(Foo)
44+
45+
block: # bug #24997
46+
type R = ref object
47+
type B = object
48+
j: int
49+
proc y(T: type): R
50+
proc u(T: type): R =
51+
let res {.global.} = y(T)
52+
res
53+
proc y(T: type): R =
54+
when T is object:
55+
doAssert not isNil(u(typeof(B.j)))
56+
R()
57+
discard u(B)

0 commit comments

Comments
 (0)