Skip to content

Commit 9becd14

Browse files
authored
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
1 parent f608e10 commit 9becd14

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

compiler/cgen.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2449,7 +2449,10 @@ proc handleProcGlobals(m: BModule) =
24492449

24502450
# fixes recursive calls #24997
24512451
swap stmts, m.preInitProc.s(cpsStmts)
2452-
genStmts(m.preInitProc, procGlobals[i])
2452+
var transformedN = procGlobals[i]
2453+
if sfInjectDestructors in m.module.flags:
2454+
transformedN = injectDestructorCalls(m.g.graph, m.idgen, m.module, transformedN)
2455+
genStmts(m.preInitProc, transformedN)
24532456
swap stmts, m.preInitProc.s(cpsStmts)
24542457

24552458
handleProcGlobals(m)

compiler/injectdestructors.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -968,10 +968,10 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
968968
{sfPure, sfGlobal} <= v.sym.flags and
969969
isInProc
970970

971-
let value = moveOrCopy(v, ri, c, s, if v.kind == nkSym: {IsDecl} else: {})
972971
if isGlobalPragma:
973-
c.graph.procGlobals.add value
972+
c.graph.procGlobals.add n
974973
else:
974+
let value = moveOrCopy(v, ri, c, s, if v.kind == nkSym: {IsDecl} else: {})
975975
result.add value
976976
elif ri.kind == nkEmpty and c.inLoop > 0:
977977
let skipInit = v.kind == nkDotExpr and # Closure var

tests/global/tglobal3.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,10 @@ block: # bug #24997
5555
doAssert not isNil(u(typeof(B.j)))
5656
R()
5757
discard u(B)
58+
59+
proc f2(str: string): string = str
60+
proc m2() =
61+
let v {.global, used.}: string = f2(f2("123"))
62+
assert v == "123"
63+
64+
m2()

0 commit comments

Comments
 (0)