From ca1f97951c08a0f4b30b915274df4f2eb4e95232 Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 19 Feb 2021 12:30:39 +0100 Subject: [PATCH] fixes #17085 [backport:1.2] (#17101) (cherry picked from commit 4395a267646ec677b06e3b0687a64331a646c190) --- compiler/ccgstmts.nim | 4 ++-- compiler/cgen.nim | 2 +- testament/categories.nim | 1 + tests/gc/trace_globals.nim | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/gc/trace_globals.nim diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 025b1d269a..a03c466505 100644 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -25,10 +25,10 @@ proc getTraverseProc(p: BProc, v: PSym): Rope = proc registerTraverseProc(p: BProc, v: PSym, traverseProc: Rope) = if sfThread in v.flags: - appcg(p.module, p.module.initProc.procSec(cpsInit), + appcg(p.module, p.module.preInitProc.procSec(cpsInit), "$n\t#nimRegisterThreadLocalMarker($1);$n$n", [traverseProc]) else: - appcg(p.module, p.module.initProc.procSec(cpsInit), + appcg(p.module, p.module.preInitProc.procSec(cpsInit), "$n\t#nimRegisterGlobalMarker($1);$n$n", [traverseProc]) proc isAssignedImmediately(conf: ConfigRef; n: PNode): bool {.inline.} = diff --git a/compiler/cgen.nim b/compiler/cgen.nim index c67ad8ef86..e046d1298e 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1645,7 +1645,7 @@ proc genInitCode(m: BModule) = writeSection(preInitProc, cpsLocals) writeSection(preInitProc, cpsInit, m.hcrOn) writeSection(preInitProc, cpsStmts) - prc.addf("}$N", []) + prc.addf("}/* preInitProc end */$N", []) # add new scope for following code, because old vcc compiler need variable # be defined at the top of the block diff --git a/testament/categories.nim b/testament/categories.nim index e1519468fe..de3d53a710 100644 --- a/testament/categories.nim +++ b/testament/categories.nim @@ -217,6 +217,7 @@ proc gcTests(r: var TResults, cat: Category, options: string) = test "stackrefleak" test "cyclecollector" + test "trace_globals" proc longGCTests(r: var TResults, cat: Category, options: string) = when defined(windows): diff --git a/tests/gc/trace_globals.nim b/tests/gc/trace_globals.nim new file mode 100644 index 0000000000..d91bc5f355 --- /dev/null +++ b/tests/gc/trace_globals.nim @@ -0,0 +1,22 @@ +discard """ + output: '''10000000 +10000000 +10000000''' +""" + +# bug #17085 + +proc init(): string = + for a in 0..<10000000: + result.add 'c' + +proc f() = + var a {.global.} = init() + var b {.global.} = init() + var c {.global.} = init() + + echo a.len + echo b.len + echo c.len + +f()