Files
sx/examples/0738-modules-comptime-arg-caller-context.sx
agra 8875a28641 fix(lower): resolve substituted caller comptime $-args in caller context [stdlib B attempt-5]
attempt-3 pinned current_source_file to the metaprogram's defining module
across the whole body lowering (lowerComptimeCall / monomorphizePackFn). That
pin also covered caller-provided comptime $-arg nodes spliced into the body by
substituteComptimeNodes — but those are CALLER-authored and must resolve in the
caller's visibility context, not the callee's. Result: a caller-owned helper
passed to an imported metaprogram errored "'<name>' is not visible".

Fix: stamp each comptime $-arg node with the caller's source_file at the cpn
build site (stampCallerSource, in lowerComptimeCall + monomorphizePackFn);
lowerExpr switches current_source_file to a node's source_file when present, so
the substituted subtree resolves against the caller while the surrounding callee
code keeps the defining-module pin. No exemption / fall-open.

Regression: examples/0738-modules-comptime-arg-caller-context.sx — a caller-owned
helper passed as a comptime-ONLY $-arg through a namespaced import. Fail-before
(attempt-3 binary): "'caller_name' is not visible". Pass-after: prints
"hello world", exit 0. Comptime-only, so it does not exercise issue 0107.

0106 RESOLVED banner extended (point 3: body=defining context, substituted
$-args=caller context). run_examples 473 -> 474; zig build test 412/412.
2026-06-07 09:07:27 +03:00

25 lines
1.2 KiB
Plaintext

// A caller-owned helper passed as a comptime-ONLY `$`-arg to a NAMESPACED
// imported metaprogram resolves in the CALLER's visibility context — not the
// metaprogram's defining module (regression, issue 0106 follow-up).
//
// `emit` is reachable only as `m.emit`; the comptime arg `caller_name()` is
// authored HERE in the caller. When `emit` splices that arg into its `#insert`
// body and lowers it, the bare name `caller_name` must stay visible in the
// caller's context. Before the fix, the body's defining-module pin also covered
// the substituted caller arg, so `caller_name` was wrongly checked against
// `emit.sx` and rejected as "not visible". The metaprogram's OWN code still
// resolves in `emit.sx` (where `concat`/`print` are flat-imported), so this
// stays compatible with the 0106 defining-context pin.
//
// Comptime-ONLY: `caller_name()` is evaluated at compile time and its value is
// embedded as a literal in the generated `print(...)` statement — it is never
// materialized at runtime (so this does NOT exercise issue 0107).
m :: #import "0738-modules-comptime-arg-caller-context/emit.sx";
caller_name :: () -> string { return "world"; }
main :: () -> s32 {
m.emit(caller_name());
return 0;
}