imports: dedup flat decl list by node identity (issue 0056 FIXED)

Impl blocks are anonymous (no declName), so a parameterised-protocol impl in a module reached via a diamond import was appended once per path and registered twice — 'duplicate impl Into for source s64'. mergeFlat and the directory-import merge loop now also dedup by node pointer; a physical AST node is lowered once regardless of how many import paths reach it.

Regression: examples/issue-0056-diamond-param-impl.sx.
This commit is contained in:
agra
2026-05-30 17:36:35 +03:00
parent ac7f1d10e5
commit 29a4891374
8 changed files with 135 additions and 2 deletions

View File

@@ -315,20 +315,29 @@ pub const ResolvedModule = struct {
/// `self.scope` — visibility joins per-file scopes at lookup time via
/// `import_graph`. We only need to append `other.decls` (the full
/// transitive list) to the global `list` so the lowering pass can
/// still resolve transitively-imported callees. Deduped by name.
/// still resolve transitively-imported callees.
///
/// Deduped two ways: named decls by name (first-wins on cross-module
/// collisions), and EVERY decl by node identity. The latter matters for
/// anonymous decls — `impl` blocks have no `declName`, so under a diamond
/// import the same cached node would otherwise be appended once per path
/// and registered twice (e.g. `duplicate impl 'Into'`).
pub fn mergeFlat(
self: *ResolvedModule,
allocator: std.mem.Allocator,
list: *std.ArrayList(*Node),
seen_list: *std.StringHashMap(void),
seen_nodes: *std.AutoHashMap(*Node, void),
other: ResolvedModule,
) !void {
_ = self;
for (other.decls) |decl| {
if (seen_nodes.contains(decl)) continue;
if (decl.data.declName()) |name| {
if (seen_list.contains(name)) continue;
try seen_list.put(name, {});
}
try seen_nodes.put(decl, {});
try list.append(allocator, decl);
}
}
@@ -420,6 +429,9 @@ pub fn resolveImports(
// by `mergeFlat` to dedupe across diamond imports now that `mod.scope`
// is non-transitive and can no longer serve as the dedup key.
var seen_in_list = std.StringHashMap(void).init(allocator);
// Node-identity set for the same purpose, covering anonymous decls
// (impl blocks) that carry no name to dedupe on.
var seen_nodes = std.AutoHashMap(*Node, void).init(allocator);
for (flat_decls) |decl| {
if (decl.data == .c_import_decl) {
@@ -559,7 +571,7 @@ pub fn resolveImports(
if (imp.name) |ns_name| {
try mod.addNamespace(allocator, &decl_list, &own_decl_list, &seen_in_list, ns_name, imported_mod, decl.span);
} else {
try mod.mergeFlat(allocator, &decl_list, &seen_in_list, imported_mod);
try mod.mergeFlat(allocator, &decl_list, &seen_in_list, &seen_nodes, imported_mod);
}
}
@@ -622,6 +634,7 @@ fn resolveDirectoryImport(
var decl_list = std.ArrayList(*Node).empty;
var own_decl_list = std.ArrayList(*Node).empty;
var seen_in_list = std.StringHashMap(void).init(allocator);
var seen_nodes = std.AutoHashMap(*Node, void).init(allocator);
for (file_names.items) |file_name| {
const file_path = try std.fmt.allocPrint(allocator, "{s}/{s}", .{ dir_path, file_name });
@@ -668,10 +681,12 @@ fn resolveDirectoryImport(
// register `Event` (a tagged_union) before `handle_event(e: *Event)`
// triggers the placeholder-struct fallback in `resolveTypeName`.
for (file_mod.decls) |decl| {
if (seen_nodes.contains(decl)) continue;
if (decl.data.declName()) |name| {
if (seen_in_list.contains(name)) continue;
try seen_in_list.put(name, {});
}
try seen_nodes.put(decl, {});
try decl_list.append(allocator, decl);
}
// Separately track which decls the directory `re-exports` to its