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

@@ -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`