fix(lower): resolve cross-module same-name functions by identity [0100]
Two modules each exporting a top-level function with the same short name (std.cli.parse 3-param, std.json.parse 2-param) collided in IR lowering's bare-name function table. fn_ast_map (name -> AST) was last-wins while module.functions / resolveFuncByName are first-wins, so importing both and calling one bound one function's AST against the other's FuncId and tripped lazyLowerFunction's param-count assert (lower.zig:1606) — reached unreachable code. Fix: - Register a namespaced import's OWN plain functions under their qualified name (ns.fn) in fn_ast_map, giving cli.parse / json.parse independent identities. The qualified resolution paths in CallResolver.plan / lowerCall already prefer ns.fn. NamespaceDecl now carries own_decls (populated in imports.addNamespace). Generic/comptime/pack/foreign functions are excluded (they dispatch by monomorphization off the bare template name); no eager declareFunction (it would resolve types before the forward-alias fixpoint). - Make scanDecls' bare fn_ast_map registration first-wins so a later namespace recursion cannot clobber an earlier (flat) entry, aligning it with mergeFlat / resolveFuncByName. Regression: examples/0719-modules-cli-and-json.sx imports both std.cli and std.json under distinct namespaces and calls both parses; panics pre-fix, passes after. issues/0100 marked RESOLVED.
This commit is contained in:
56
examples/0719-modules-cli-and-json.sx
Normal file
56
examples/0719-modules-cli-and-json.sx
Normal file
@@ -0,0 +1,56 @@
|
||||
// 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 :: #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;
|
||||
}
|
||||
1
examples/expected/0719-modules-cli-and-json.exit
Normal file
1
examples/expected/0719-modules-cli-and-json.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0719-modules-cli-and-json.stderr
Normal file
1
examples/expected/0719-modules-cli-and-json.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
10
examples/expected/0719-modules-cli-and-json.stdout
Normal file
10
examples/expected/0719-modules-cli-and-json.stdout
Normal file
@@ -0,0 +1,10 @@
|
||||
cli-group: ok
|
||||
cli-command: ok
|
||||
cli-index: ok
|
||||
cli-flag: ok
|
||||
json-members: ok
|
||||
json-key0: ok
|
||||
json-str: ok
|
||||
json-arr-len: ok
|
||||
json-arr-2: ok
|
||||
=== DONE ===
|
||||
Reference in New Issue
Block a user