From d5c7abe3d2f158f919a58476a347c925b78d1d3c Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Wed, 18 Dec 2024 01:16:34 +0800 Subject: [PATCH] fixes #17681; enforce codegen for exportc consts (#24546) fixes #17681 --- compiler/ccgexprs.nim | 6 ++++++ tests/consts/taddressable_consts.nim | 26 ++++++++++++++++++++++++++ tests/consts/taddressable_consts2.nim | 24 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/consts/taddressable_consts.nim create mode 100644 tests/consts/taddressable_consts2.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index 0cd2e74148..2d4a46412c 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -3591,6 +3591,12 @@ proc expr(p: BProc, n: PNode, d: var TLoc) = of nkConstSection: if useAliveDataFromDce in p.module.flags: genConstStmt(p, n) + else: # enforce addressable consts for exportc + let m = p.module + for it in n: + let symNode = skipPragmaExpr(it[0]) + if symNode.kind == nkSym and sfExportc in symNode.sym.flags: + requestConstImpl(p, symNode.sym) # else: consts generated lazily on use of nkForStmt: internalError(p.config, n.info, "for statement not eliminated") of nkCaseStmt: genCase(p, n, d) diff --git a/tests/consts/taddressable_consts.nim b/tests/consts/taddressable_consts.nim new file mode 100644 index 0000000000..73282c12fe --- /dev/null +++ b/tests/consts/taddressable_consts.nim @@ -0,0 +1,26 @@ +discard """ + joinable: false +""" + +block: + proc magics(): array[3, int] = + result = [1, 2, 3] + + + const magic_arrays {.exportc.} = magics() + + let sss {.importc: "magic_arrays", nodecl.} : array[3, int] + + + doAssert sss[2] == 3 + +block: + proc magics(): int = + result = 12 + + + const magic_numbers {.exportc.} = magics() + + let sss {.importc: "magic_numbers", nodecl.} : int + + doAssert sss == 12 diff --git a/tests/consts/taddressable_consts2.nim b/tests/consts/taddressable_consts2.nim new file mode 100644 index 0000000000..d8ae02dc48 --- /dev/null +++ b/tests/consts/taddressable_consts2.nim @@ -0,0 +1,24 @@ +discard """ + joinable: false +""" + +block: + + const magic_numbers {.exportc.} = 12 + + let sss {.importc: "magic_numbers", nodecl.} : int + + doAssert magic_numbers == 12 + doAssert sss == 12 + +block: + proc magics(): array[3, int] = + result = [1, 2, 3] + + + const magic_arrays {.exportc.} = magics() + + let sss {.importc: "magic_arrays", nodecl.} : array[3, int] + + doAssert magic_arrays[1] == 2 + doAssert sss[2] == 3