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.
This commit is contained in:
agra
2026-06-07 09:07:27 +03:00
parent b62223edaf
commit 8875a28641
7 changed files with 76 additions and 1 deletions

View File

@@ -29,6 +29,20 @@
> bare reach into a namespaced-only import there errors. **No `#insert`
> exemption** (attempt-2's `in_insert_expansion` flag is deleted): the fix is
> the absence of an exemption, not a narrower one.
> 3. **Substituted caller `$`-args resolve in the CALLER's context** (attempt-5).
> The point-2 defining-module pin covers the metaprogram body's OWN code only.
> A caller-provided comptime `$`-arg (e.g. a caller-owned helper passed to an
> imported metaprogram) is spliced into the body by `substituteComptimeNodes`;
> those nodes are CALLER-authored and must resolve in the caller's visibility
> context, not the callee's. **Fix:** the `$`-arg node is stamped with the
> caller's `source_file` at the `cpn` build site (`lowerComptimeCall` /
> `monomorphizePackFn`, `stampCallerSource`), and `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. Regression:
> `examples/0738-modules-comptime-arg-caller-context.sx` (caller-owned helper
> as a comptime-only `$`-arg through a namespaced import; fail-before
> "'caller_name' is not visible" → pass-after "hello world").
>
> Root cause: `isNameVisible` walked `import_graph` (flat AND namespaced edges)
> where a bare name should join only over `flat_import_graph`; and the pack /
@@ -40,7 +54,7 @@
> `#insert secret()` into a namespaced-only import errors (fail-before exit 0 on
> the attempt-2 exemption / pass-after exit 1). Face #2 restored WITHOUT an
> exemption: `examples/0015 / 0700 / 0718 / 1030` pass again (`run_examples`
> 471 → 473). Fix in `src/ir/lower.zig` (`monomorphizePackFn` +
> 471 → 474, incl. the attempt-5 caller-context regression `0738`). Fix in `src/ir/lower.zig` (`monomorphizePackFn` +
> `lowerComptimeCall` source-context pin; exemption removed) + `src/imports.zig`
> (`stampFnBodySource`) + `src/ir/resolver.zig` (`VisibilityMode` modes, landed in
> attempt-1).