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:
@@ -674,6 +674,12 @@ pub const SpreadExpr = struct {
|
||||
pub const NamespaceDecl = struct {
|
||||
name: []const u8,
|
||||
decls: []const *Node,
|
||||
/// Decls AUTHORED in the namespaced module itself (its `own_decls`), a
|
||||
/// subset of `decls` (which also carries the module's transitive flat
|
||||
/// imports). Lowering registers these under their module-qualified name
|
||||
/// (`ns.fn`) so `pkg.fn(...)` resolves to a unique FuncId distinct from a
|
||||
/// same-named function in another module (issue 0100).
|
||||
own_decls: []const *Node = &.{},
|
||||
/// True when the namespace NAME was a backtick raw identifier — exempt
|
||||
/// from the reserved-type-name decl check (issue 0089).
|
||||
is_raw: bool = false,
|
||||
|
||||
@@ -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,
|
||||
} },
|
||||
};
|
||||
|
||||
@@ -594,6 +594,7 @@ pub const Lowering = struct {
|
||||
.namespace_decl => |ns| {
|
||||
self.registerNamespacedForeignClasses(ns);
|
||||
if (self.main_file != null) {
|
||||
self.registerNamespaceQualifiedFns(ns.name, ns.own_decls);
|
||||
self.lowerDecls(ns.decls);
|
||||
}
|
||||
},
|
||||
@@ -691,15 +692,26 @@ pub const Lowering = struct {
|
||||
false;
|
||||
switch (decl.data) {
|
||||
.fn_decl => |fd| {
|
||||
self.program_index.fn_ast_map.put(fd.name, &decl.data.fn_decl) catch {};
|
||||
self.program_index.import_flags.put(fd.name, is_imported) catch {};
|
||||
// First-wins on a bare-name collision, matching `mergeFlat`
|
||||
// and `resolveFuncByName`. A later namespace recursion that
|
||||
// re-introduces a same-named function (e.g. a second module
|
||||
// also exporting `parse`) must NOT clobber the AST while the
|
||||
// function table keeps the first — that split lowers one
|
||||
// signature against the other's body (issue 0100). The
|
||||
// shadowed function stays reachable via its qualified name.
|
||||
if (!self.program_index.fn_ast_map.contains(fd.name)) {
|
||||
self.program_index.fn_ast_map.put(fd.name, &decl.data.fn_decl) catch {};
|
||||
self.program_index.import_flags.put(fd.name, is_imported) catch {};
|
||||
}
|
||||
// Declare extern stub for all functions (bodies lowered lazily)
|
||||
self.declareFunction(&fd, fd.name);
|
||||
},
|
||||
.const_decl => |cd| {
|
||||
if (cd.value.data == .fn_decl) {
|
||||
self.program_index.fn_ast_map.put(cd.name, &cd.value.data.fn_decl) catch {};
|
||||
self.program_index.import_flags.put(cd.name, is_imported) catch {};
|
||||
if (!self.program_index.fn_ast_map.contains(cd.name)) {
|
||||
self.program_index.fn_ast_map.put(cd.name, &cd.value.data.fn_decl) catch {};
|
||||
self.program_index.import_flags.put(cd.name, is_imported) catch {};
|
||||
}
|
||||
self.declareFunction(&cd.value.data.fn_decl, cd.name);
|
||||
} else if (cd.value.data == .struct_decl) {
|
||||
self.registerStructDecl(&cd.value.data.struct_decl);
|
||||
@@ -884,6 +896,7 @@ pub const Lowering = struct {
|
||||
self.registerNamespacedForeignClasses(ns);
|
||||
if (self.main_file != null) {
|
||||
self.scanDecls(ns.decls);
|
||||
self.registerNamespaceQualifiedFns(ns.name, ns.own_decls);
|
||||
}
|
||||
},
|
||||
.ufcs_alias => |ua| {
|
||||
@@ -1421,6 +1434,57 @@ pub const Lowering = struct {
|
||||
func.has_implicit_ctx = wants_ctx;
|
||||
}
|
||||
|
||||
/// Register a namespaced import's OWN functions under their module-qualified
|
||||
/// name (`ns.fn`), giving each a UNIQUE FuncId in the function table. Two
|
||||
/// modules each exporting a top-level `parse` otherwise collide in the
|
||||
/// bare-name `fn_ast_map` / function table (last-wins) while `resolveFuncByName`
|
||||
/// picks the first declared, so `lazyLowerFunction` lowers one signature
|
||||
/// against the other's body and trips its param-count assert (issue 0100).
|
||||
/// The bare recursion in `scanDecls` still registers intra-module bare calls;
|
||||
/// this adds the qualified identity the `pkg.fn(...)` resolution paths in
|
||||
/// `CallResolver.plan` / `lowerCall` already prefer.
|
||||
fn registerNamespaceQualifiedFns(self: *Lowering, ns_name: []const u8, own_decls: []const *Node) void {
|
||||
const saved_source = self.current_source_file;
|
||||
defer self.setCurrentSourceFile(saved_source);
|
||||
for (own_decls) |decl| {
|
||||
self.setCurrentSourceFile(decl.source_file);
|
||||
switch (decl.data) {
|
||||
.fn_decl => self.registerQualifiedFn(ns_name, &decl.data.fn_decl, decl.data.fn_decl.name),
|
||||
.const_decl => |cd| {
|
||||
if (cd.value.data == .fn_decl) {
|
||||
self.registerQualifiedFn(ns_name, &cd.value.data.fn_decl, cd.name);
|
||||
}
|
||||
},
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn registerQualifiedFn(self: *Lowering, ns_name: []const u8, fd: *const ast.FnDecl, short: []const u8) void {
|
||||
// Only PLAIN free functions need a qualified identity. Generic /
|
||||
// comptime / pack functions (`Vector`, `print`, `any_to_string`) are
|
||||
// dispatched by monomorphization off their BARE template name, not the
|
||||
// plain `resolveFuncByName` / `lazyLowerFunction` path that trips the
|
||||
// collision assert (issue 0100); registering a qualified alias for them
|
||||
// would divert that machinery and strand a per-call type binding.
|
||||
if (fd.type_params.len > 0 or hasComptimeParams(fd) or isPackFn(fd)) return;
|
||||
// Foreign / builtin / #compiler bodies keep their literal name; a
|
||||
// qualified alias has no distinct symbol to resolve to.
|
||||
switch (fd.body.data) {
|
||||
.foreign_expr, .builtin_expr, .compiler_expr => return,
|
||||
else => {},
|
||||
}
|
||||
const qualified = std.fmt.allocPrint(self.alloc, "{s}.{s}", .{ ns_name, short }) catch return;
|
||||
if (self.program_index.fn_ast_map.contains(qualified)) return;
|
||||
self.program_index.fn_ast_map.put(qualified, fd) catch {};
|
||||
self.program_index.import_flags.put(qualified, true) catch {};
|
||||
// No eager `declareFunction` here: the extern stub's param/return types
|
||||
// would be resolved now, before the forward-alias fixpoint, caching an
|
||||
// `.unresolved` for any type declared later in the module. The qualified
|
||||
// function is declared + lowered on demand by `lazyLowerFunction`'s
|
||||
// null-FuncId path (`lowerFunction`), which runs after all types resolve.
|
||||
}
|
||||
|
||||
/// Check if a C-imported function is visible from the current source file.
|
||||
/// Returns true for non-C functions (always visible) or if no scoping info available.
|
||||
fn isCImportVisible(self: *Lowering, fn_name: []const u8) bool {
|
||||
|
||||
Reference in New Issue
Block a user