IC: remember toplevel .emits

This commit is contained in:
Araq
2026-07-03 09:25:45 +02:00
parent 1c181ec6f7
commit 24ef33d23c
5 changed files with 39 additions and 3 deletions

View File

@@ -0,0 +1,2 @@
converter toBool*(x: int): bool = x != 0

14
tests/ic/memit.nim Normal file
View File

@@ -0,0 +1,14 @@
# Helper for temit.nim: a NON-main module with a module-scope `{.emit.}` that
# introduces a C macro consumed by an `{.importc, nodecl.}` const. The IC backend
# reload used to drop top-level emit pragmas — writeToplevelNode wrote them to the
# by-symbol-index implementation section, where a symbol-less pragma is never
# reloaded — so the generated C lost the `#define` and failed to compile with
# "use of undeclared identifier". Mirrors lib/pure/concurrency/cpuinfo.nim's
# `#include <sys/sysctl.h>` + `CTL_HW`/`HW_NCPU` importc pattern.
{.emit: """/*TYPESECTION*/
#define NIM_IC_EMIT_ANSWER 42
""".}
let icEmitAnswer {.importc: "NIM_IC_EMIT_ANSWER", nodecl.}: cint
proc emitAnswer*(): int = int(icEmitAnswer)

7
tests/ic/temit.nim Normal file
View File

@@ -0,0 +1,7 @@
# Regression: a module-scope `{.emit.}` in an imported module must survive the
# `nim ic` backend reload (see memit.nim). Before the fix the dropped `#define`
# made the generated C fail to compile, so `nim ic` exited non-zero.
import memit
doAssert emitAnswer() == 42
echo emitAnswer()