From d98861ea7a984bae585b9b54e662f7c508d9dffb Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 23 Apr 2026 19:36:04 +0800 Subject: [PATCH] fixes #25656; nim ic: Using a top level variable Co-authored-by: Copilot --- compiler/ast2nif.nim | 18 +++++++++++++----- tests/ic/tmainmodule.nim | 5 +++++ 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 tests/ic/tmainmodule.nim diff --git a/compiler/ast2nif.nim b/compiler/ast2nif.nim index 8847af32b7..891a704618 100644 --- a/compiler/ast2nif.nim +++ b/compiler/ast2nif.nim @@ -826,11 +826,15 @@ type types: Table[string, (PType, NifIndexEntry)] syms: Table[string, (PSym, NifIndexEntry)] mods: Table[FileIndex, NifModule] + mainModuleIdx: FileIndex cache: IdentCache proc createDecodeContext*(config: ConfigRef; cache: IdentCache): DecodeContext = ## Supposed to be a global variable - result = DecodeContext(infos: LineInfoWriter(config: config), cache: cache) + result = DecodeContext( + infos: LineInfoWriter(config: config), + mainModuleIdx: InvalidFileIdx, + cache: cache) proc cursorFromIndexEntry(c: var DecodeContext; module: FileIndex; entry: NifIndexEntry; buf: var TokenBuf): Cursor = @@ -881,8 +885,11 @@ proc readEmbeddedIndex(s: var Stream): Table[string, NifIndexEntry] = s.r.jumpTo(contentPos) # Restore position proc moduleId(c: var DecodeContext; suffix: string; flags: set[LoadFlag] = {}): FileIndex = - var isKnownFile = false - result = c.infos.config.registerNifSuffix(suffix, isKnownFile) + if c.mainModuleIdx != InvalidFileIdx and suffix == moduleSuffix(c.infos.config, c.mainModuleIdx): + result = c.mainModuleIdx + else: + var isKnownFile = false + result = c.infos.config.registerNifSuffix(suffix, isKnownFile) # Always load the module's index if it's not already in c.mods # This is needed when resolving symbols from modules that were registered elsewhere # but haven't had their NIF index loaded yet @@ -1176,8 +1183,7 @@ proc loadSymFromCursor(c: var DecodeContext; s: PSym; n: var Cursor; thisModule: if s.kindImpl == skModule: expect n, DotToken inc n - var isKnownFile = false - s.positionImpl = int c.infos.config.registerNifSuffix(thisModule, isKnownFile) + s.positionImpl = int moduleId(c, thisModule) # do to the precompiled mechanism things end up as main modules which are not! excl s.flagsImpl, sfMainModule else: @@ -1654,6 +1660,8 @@ proc loadNifModule*(c: var DecodeContext; suffix: ModuleSuffix; interf, interfHi proc loadNifModule*(c: var DecodeContext; f: FileIndex; interf, interfHidden: var TStrTable; flags: set[LoadFlag] = {}): PrecompiledModule = let suffix = ModuleSuffix(moduleSuffix(c.infos.config, f)) + if f == c.infos.config.projectMainIdx: + c.mainModuleIdx = f result = loadNifModule(c, suffix, interf, interfHidden, flags) when isMainModule: diff --git a/tests/ic/tmainmodule.nim b/tests/ic/tmainmodule.nim new file mode 100644 index 0000000000..318257ed8f --- /dev/null +++ b/tests/ic/tmainmodule.nim @@ -0,0 +1,5 @@ +var x = 1 +proc foo() = + x = 2 + +foo() \ No newline at end of file