Files
sx/examples/0719-modules-cli-and-json.sx
agra 7188481761 feat(resolver): complete source-aware nominal-TYPE leaf — bare ns-only types not visible [stdlib E1 attempt-2]
Completes the F1 deliverable the reviewer flagged: the bare TYPE leaf still
returned the global `findByName` match BEFORE any visibility check, so a type
declared only behind a namespaced import leaked bare. Now the registered-type
branch of `selectNominalLeaf` is gated on bare-flat visibility (the type analog
of Phase B's value/function tightening): a bare reference to a namespaced-only
import's TYPE errors ("type 'X' is not visible; #import the module that declares
it") and poisons to `.unresolved` — never the leaked global match, never a
silent empty-struct stub.

Visibility gate is the TRANSITIVE flat-import closure (`typeBareVisible`), not
the single-hop `collectVisibleAuthors`/`isNameVisible`: a flat import is
transitive for resolution, so a type two flat hops away (`CAllocator`, via
`main → std.sx → allocators.sx`) stays bare-visible while a namespaced-only type
(reached solely over a namespace edge) does not. The gate applies ONLY to a
TOP-LEVEL author (`module_decls`) — a LOCAL type / generic-param / fabricated
empty-struct stub is findByName-registered but authored in no module, so it
resolves ungated and byte-identically (its own diagnostics still fire). The
compiler-synthesized default-Context emission falls open (`CAllocator` is
infrastructure, independent of the program's import style). The closure walk
lives in lower.zig, so resolver.zig keeps its single graph-walk.

A namespaced callee's declared return type now resolves in the callee's own
module context (`resolveTypeInSource` over `qualified_fn_source`) — a `Value`
returned by `json.parse` is visible inside `json.sx`, not at the call site
(issue-0100-F1 source-pin analog).

Migrates 0719 (flat-imports `cli` for its types, keeps `cli` namespaced for the
same-name `cli.parse`). Adds 0743 (bare ns-only struct → not visible) and 0744
(bare ns-only enum → not visible) regressions. 0742 (ns-only const) + 0210
(generics stay legacy) unchanged. readme updated.

Gate: zig build / zig build test (LSP sweep, no crash) / run_examples 481/0;
m3te ios-sim exit 0; 0743/0744 fail-before on 7cd12b0 (compiled, no diagnostic)
/ pass-after (clean "not visible").
2026-06-07 16:49:15 +03:00

65 lines
2.7 KiB
Plaintext

// Two modules that each export a top-level `parse` — `std.cli.parse`
// (3-param subcommand dispatch) and `std.json.parse` (2-param document
// reader) — imported into ONE program under DISTINCT namespaces, with
// BOTH `parse`s actually called.
//
// Regression (issue 0100): same-name cross-module functions collided in
// the bare-name function table during IR lowering. Lowering re-resolved a
// call by SHORT name, so importing both modules and calling one bound the
// wrong-arity same-named function and tripped `lazyLowerFunction`'s
// param-count assert (panic). The fix resolves each `pkg.parse(...)` to a
// UNIQUE module-qualified FuncId, so `cli.parse` and `json.parse` are
// independent identities.
#import "modules/std.sx";
// `cli` is imported BOTH flat (so its types — `FlagSpec` / `Command` / `Diag` —
// are bare-visible) AND namespaced (so the same-name `cli.parse` stays a
// distinct qualified identity from `json.parse`). Post-E1 a bare reference to a
// namespaced-ONLY type is a "not visible" error, so the flat import is what makes
// the bare type names below resolve; `json` stays namespaced-only (its `Value`
// reaches `main` only as `json.parse`'s return type, resolved in `json.sx`'s own
// context).
#import "modules/std/cli.sx";
cli :: #import "modules/std/cli.sx";
json :: #import "modules/std/json.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> ! {
gpa := GPA.init();
arena := Arena.init(xx gpa, 8192);
defer arena.deinit();
// ── cli.parse: dispatch <group> <command> + a value flag ─────────
publish_flags : []FlagSpec = .[
FlagSpec.{ name = "out", takes_value = true, required = true },
];
cmds : []Command = .[
Command.{ group = "ci", command = "publish", flags = publish_flags },
Command.{ group = "ci", command = "status", flags = .[] },
];
argv : []string = .["ci", "publish", "--out", "dist"];
d : Diag = .{};
p := try cli.parse(argv, cmds, @d);
report("cli-group", p.group == "ci");
report("cli-command", p.command == "publish");
report("cli-index", p.cmd_index == 0);
report("cli-flag", p.value_of("out") == "dist");
// ── json.parse: read a small document into the value model ───────
doc := "{\"name\":\"sx\",\"xs\":[1,2,3]}";
root := try json.parse(doc, xx arena);
o := root.object;
report("json-members", o.len == 2);
report("json-key0", o.items[0].key == "name");
report("json-str", o.items[0].val.str == "sx");
xs := o.items[1].val.array;
report("json-arr-len", xs.len == 3);
report("json-arr-2", xs.items[2].int_ == 3);
print("=== DONE ===\n");
return;
}