fixes #25656; nim ic: Using a top level variable

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
ringabout
2026-04-23 19:36:04 +08:00
parent 8b44b9d9ae
commit d98861ea7a
2 changed files with 18 additions and 5 deletions

View File

@@ -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:

5
tests/ic/tmainmodule.nim Normal file
View File

@@ -0,0 +1,5 @@
var x = 1
proc foo() =
x = 2
foo()