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

@@ -0,0 +1,24 @@
// 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;
}

View File

@@ -0,0 +1,9 @@
// Library metaprogram: builds a `print(...)` statement at compile time from the
// comptime `$who` value. `concat`/`print` are flat-imported here and resolve in
// THIS module's context (the 0106 defining-module pin). The substituted caller
// `$`-arg, by contrast, resolves in the CALLER's context.
#import "modules/std.sx";
emit :: ($who: string) {
#insert concat(concat("print(\"hello ", who), "\\n\");");
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1 @@
hello world

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).

View File

@@ -3126,6 +3126,17 @@ pub const Lowering = struct {
const saved_span = self.builder.current_span;
defer self.builder.current_span = saved_span;
if (node.span.start != 0 or node.span.end != 0) self.builder.current_span = .{ .start = node.span.start, .end = node.span.end };
// A node carrying an explicit `source_file` is one spliced into a body
// from another module — a substituted caller comptime-`$`-arg (stamped
// at the `cpn` build site in lowerComptimeCall / monomorphizePackFn).
// Resolve its bare names in THAT module's visibility context, overriding
// the body's defining-module pin, then restore so sibling callee nodes
// keep the enclosing context. Ordinary expression nodes never carry a
// `source_file`, so this is a no-op on the hot path.
const restore_source = node.source_file != null;
const saved_source = self.current_source_file;
if (node.source_file) |sf| self.setCurrentSourceFile(sf);
defer if (restore_source) self.setCurrentSourceFile(saved_source);
return switch (node.data) {
// Bare `$<pack>` in expression position → an `[]Type` slice
// value where each element is a `const_type(arg_types[i])`.
@@ -9625,6 +9636,7 @@ pub const Lowering = struct {
}
if (call_arg_idx >= call_node.args.len) break;
if (param.is_comptime) {
self.stampCallerSource(call_node.args[call_arg_idx]);
cpn.put(param.name, call_node.args[call_arg_idx]) catch {};
call_arg_idx += 1;
} else {
@@ -10867,6 +10879,7 @@ pub const Lowering = struct {
if (p.is_comptime) {
if (ct_arg_idx < call_node.args.len) {
const call_arg = call_node.args[ct_arg_idx];
self.stampCallerSource(call_arg);
cpn.put(p.name, call_arg) catch return;
// Bind as a runtime local for bare-name access.
// Lower the call arg as a value, then alloca + store.
@@ -15145,6 +15158,18 @@ pub const Lowering = struct {
if (self.diagnostics) |d| d.current_source_file = source_file;
}
/// Stamp a caller-provided comptime `$`-arg node with the caller's source
/// file. When the node is later substituted into the (defining-module-pinned)
/// metaprogram body and lowered, lowerExpr's per-node source switch resolves
/// its bare names in the CALLER's visibility context — not the callee's — so
/// a caller-owned helper passed to an imported metaprogram stays visible.
/// Only stamps a node with no source yet, and only when the caller context
/// is known; an unknown caller source leaves the node's fall-open intact.
fn stampCallerSource(self: *Lowering, node: *Node) void {
if (node.source_file != null) return;
if (self.current_source_file) |src| node.source_file = src;
}
fn emitError(self: *Lowering, name: []const u8, span: ?ast.Span) Ref {
if (self.diagnostics) |diags| {
// The literal message carries the lowering's `current_source_file`