From ffb993d5bd9d4d7fae2bed2203f73721a9dfa7b7 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 10 Jun 2025 02:53:40 +0800 Subject: [PATCH] fixes #24981; the length of the seq changed of procGloals (#24984) fxies #24981 `m.g.graph.procGlobals` could change because the right side of `.global` assignment (e.g. `let a {.global.} = g(T)`) may trigger injections for unhandled procs --- compiler/cgen.nim | 9 +++++++-- tests/global/tglobal3.nim | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 49f4d68cfa..39be098663 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -2433,8 +2433,13 @@ proc genTopLevelStmt*(m: BModule; n: PNode) = else: genProcBody(m.initProc, transformedN) - for g in m.g.graph.procGlobals: - genStmts(m.preInitProc, g) + 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()) proc shouldRecompile(m: BModule; code: Rope, cfile: Cfile): bool = if optForceFullMake notin m.config.globalOptions: diff --git a/tests/global/tglobal3.nim b/tests/global/tglobal3.nim index 10a40798f2..e0f0d50ce0 100644 --- a/tests/global/tglobal3.nim +++ b/tests/global/tglobal3.nim @@ -27,4 +27,17 @@ proc f(v: static string): int = xxx[] doAssert f("1") == 1 -doAssert f("1") == 1 \ No newline at end of file +doAssert f("1") == 1 + +block: # bug #24981 + func p(T: type): T {.compileTime.} = default(ptr T)[] + type W = ref object + proc g(T: type): W + proc m(T: type) = + let a {.global.} = g(T) + proc g(T: type): W = + when T is object: + m(typeof(p(T).i)) + type Foo = object + i: int + m(Foo)