From 90b323ab6a94cbdf8e8b85b756311827626e292f Mon Sep 17 00:00:00 2001 From: araq Date: Wed, 17 Dec 2025 11:39:19 +0100 Subject: [PATCH] progress --- compiler/ast2nif.nim | 3 +++ compiler/astdef.nim | 2 +- compiler/ccgexprs.nim | 2 +- compiler/modulegraphs.nim | 10 ++++++++++ compiler/seminst.nim | 4 ++++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/ast2nif.nim b/compiler/ast2nif.nim index 1cc8ff2cbe..c419a934d8 100644 --- a/compiler/ast2nif.nim +++ b/compiler/ast2nif.nim @@ -182,6 +182,7 @@ proc toNifSymName(w: var Writer; sym: PSym): string = let module = if sym.kindImpl == skPackage: w.currentModule else: sym.itemId.module result.add modname(module, w.infos.config) + proc globalName(sym: PSym; config: ConfigRef): string = result = sym.name.s result.add '.' @@ -682,6 +683,8 @@ proc writeOp(w: var Writer; content: var TokenBuf; op: LogEntry) = discard "to implement" of EnumToStrEntry: discard "to implement" + of GenericInstEntry: + discard "will only be written later to ensure it is materialized" proc writeNifModule*(config: ConfigRef; thisModule: int32; n: PNode; opsLog: seq[LogEntry]; diff --git a/compiler/astdef.nim b/compiler/astdef.nim index 411fbcd71c..30b2298fb2 100644 --- a/compiler/astdef.nim +++ b/compiler/astdef.nim @@ -996,7 +996,7 @@ proc newStrNode*(strVal: string; info: TLineInfo): PNode = type LogEntryKind* = enum - HookEntry, ConverterEntry, MethodEntry, EnumToStrEntry + HookEntry, ConverterEntry, MethodEntry, EnumToStrEntry, GenericInstEntry LogEntry* = object kind*: LogEntryKind op*: TTypeAttachedOp diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 4bc8193ecb..ddf2ebe7fd 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -3363,7 +3363,7 @@ proc genConstSetup(p: BProc; sym: PSym): bool = useHeader(m, sym) if sym.loc.k == locNone: fillBackendName(p.module, sym) - ensureMutable sym + backendEnsureMutable sym fillLoc(sym.locImpl, locData, sym.astdef, OnStatic) if m.hcrOn: incl(sym, lfIndirect) result = lfNoDecl notin sym.loc.flags diff --git a/compiler/modulegraphs.nim b/compiler/modulegraphs.nim index 67abc12070..d338194ea5 100644 --- a/compiler/modulegraphs.nim +++ b/compiler/modulegraphs.nim @@ -451,6 +451,14 @@ proc addMethodToGeneric*(g: ModuleGraph; module: int; t: PType; col: int; m: PSy let ownerModule = if t.sym != nil: t.sym.itemId.module.int else: module g.opsLog.add LogEntry(kind: MethodEntry, module: ownerModule, key: key, sym: m) +proc logGenericInstance*(g: ModuleGraph; inst: PSym) = + ## Log a generic instance so it gets written to the NIF file. + ## This is needed when generic instances are created during compile-time + ## evaluation and may be referenced from other modules compiled in the same run. + if g.config.cmd in {cmdNifC, cmdM}: + let ownerModule = inst.itemId.module.int + g.opsLog.add LogEntry(kind: GenericInstEntry, module: ownerModule, sym: inst) + proc hasDisabledAsgn*(g: ModuleGraph; t: PType): bool = let op = getAttachedOp(g, t, attachedAsgn) result = op != nil and sfError in op.flags @@ -840,6 +848,8 @@ when not defined(nimKochBootstrap): discard "todo" of EnumToStrEntry: discard "todo" + of GenericInstEntry: + raiseAssert "GenericInstEntry should not be in the NIF index" # Register methods per type from NIF index discard "todo" cachedModules.add fileIdx diff --git a/compiler/seminst.nim b/compiler/seminst.nim index 9a2f3ac002..b34c7ef58e 100644 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -450,6 +450,10 @@ proc generateInstance(c: PContext, fn: PSym, pt: LayeredIdTable, entry.compilesId = c.compilesContextId addToGenericProcCache(c, fn, entry) c.generics.add(makeInstPair(fn, entry)) + # Log the generic instance so it gets written to the NIF file. + # This is needed for cyclic module dependencies where generic instances + # may be created in one module but referenced from another. + logGenericInstance(c.graph, result) # bug #12985 bug #22913 # TODO: use the context of the declaration of generic functions instead # TODO: consider fixing options as well