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:
agra
2026-06-06 02:30:19 +03:00
parent fb3fdaf454
commit 3edc67521b
8 changed files with 244 additions and 5 deletions

View File

@@ -362,6 +362,10 @@ pub const ResolvedModule = struct {
.data = .{ .namespace_decl = .{
.name = name,
.decls = other.decls,
// The module's OWN authored decls — what `ns.fn` should bind
// to (issue 0100). `decls` stays the full transitive list so
// the lowering pass can still resolve transitive callees.
.own_decls = other.own_decls,
// Carry the backtick raw escape from the `name :: #import …`
// form so a reserved-name namespace is exempt from the decl
// check, symmetric to every other decl site (issue 0089).
@@ -486,12 +490,16 @@ pub fn resolveImports(
// Keep c_import_decl inside namespace so codegen can find sources
try ns_decls.append(allocator, decl);
const ns_slice = try ns_decls.toOwnedSlice(allocator);
const ns_node = try allocator.create(Node);
ns_node.* = .{
.span = decl.span,
.data = .{ .namespace_decl = .{
.name = ns_name,
.decls = try ns_decls.toOwnedSlice(allocator),
.decls = ns_slice,
// A C-import namespace authors exactly the wrapped fn
// decls — they ARE its own decls (issue 0100).
.own_decls = ns_slice,
.is_raw = ci.is_raw,
} },
};