fixes #26015; Multiple definition error when using codegenDecl regression

This commit is contained in:
ringabout
2026-07-17 17:39:48 +08:00
parent 74cd4cbf3c
commit 11ef897c1a
3 changed files with 30 additions and 4 deletions

View File

@@ -1854,10 +1854,20 @@ proc genVarPrototype(m: BModule, n: PNode) =
typ = ptrType(typ)
if lfDynamicLib in sym.loc.flags:
typ = ptrType(typ)
m.s[cfsVars].addVar(m, sym,
name = sym.loc.snippet,
typ = typ,
visibility = vis)
if sfCodegenDecl in sym.flags:
# `codegenDecl` customizes the definition, but a reference from a
# different module still needs a plain declaration. Applying the
# custom format here can turn the declaration into a tentative
# definition (and thus cause duplicate symbols at link time).
m.s[cfsVars].addDeclWithVisibility(vis):
m.s[cfsVars].addVar(kind = Local,
name = sym.loc.snippet,
typ = typ)
else:
m.s[cfsVars].addVar(m, sym,
name = sym.loc.snippet,
typ = typ,
visibility = vis)
if m.hcrOn:
m.initProc.procSec(cpsLocals).add('\t')
m.initProc.procSec(cpsLocals).addAssignment(sym.loc.snippet,

View File

@@ -0,0 +1,4 @@
var codegenDeclGlobal* {.codegenDecl: "$# /* custom declaration */ $#".} = 123
proc readCodegenDeclGlobal*(): int {.inline.} =
codegenDeclGlobal

View File

@@ -0,0 +1,12 @@
discard """
output: '''
123
123
'''
targets: "c cpp"
"""
import ./mcodegendeclglobal
echo codegenDeclGlobal
echo readCodegenDeclGlobal()