feat(imports): buildImportFacts raw-fact store (ModuleRawDeclIndex + NamespaceEdges) [stdlib A]
Phase A of the unified resolver (R5 locked design). Additive infrastructure
with NO behavior change — builds the import-side raw-fact store; nothing
consumes it yet.
- imports.zig: add RawDeclRef / RawAuthor / ModuleRawDeclIndex / ModuleDecls /
NamespaceTarget / NamespaceEdges, plus buildImportFacts (mirrors
buildModuleFns) producing a scalar per-module name→RawDeclRef index + the
namespace edges. Callable without IR lowering (LSP reuses it later).
- ast.zig: NamespaceDecl gains target_module_path, captured at resolution time
(the resolved_path otherwise lost on the node) so the namespace edge records
the alias target.
- imports.zig: same-module duplicate top-level name is now DIAGNOSED
("duplicate top-level declaration 'X'") where addOwnDecl would silently drop
the second author — replaces the discarded `_ =` at the three call sites.
- program_index.zig: borrowed views module_decls / namespace_edges (like
module_fns); deinit does not free them.
- core.zig: build the facts alongside buildModuleFns and point the borrowed
views at them.
- imports.test.zig: index unit tests (flat / directory / namespaced file /
namespaced directory / C-import namespace / same-name fn / same-name struct /
value-vs-type same spelling / raw const_decl) + the duplicate-name diagnostic
regression (fails pre-fix, passes after).
Gate (worktree): zig build, zig build test (incl. LSP corpus sweep), and
run_examples (471, byte-identical) all green; m3te ios-sim build exits 0.
This commit is contained in:
@@ -4,6 +4,7 @@ const std = @import("std");
|
||||
const ast = @import("ast.zig");
|
||||
const parser = @import("parser.zig");
|
||||
const imports = @import("imports.zig");
|
||||
const errors = @import("errors.zig");
|
||||
|
||||
var g_test_threaded: ?std.Io.Threaded = null;
|
||||
fn testIo() std.Io {
|
||||
@@ -13,6 +14,61 @@ fn testIo() std.Io {
|
||||
return g_test_threaded.?.io();
|
||||
}
|
||||
|
||||
// ── buildImportFacts unit tests (Phase A: import-side raw facts) ──
|
||||
|
||||
const Facts = struct {
|
||||
decls: imports.ModuleDecls,
|
||||
ns_edges: imports.NamespaceEdges,
|
||||
diags: errors.DiagnosticList,
|
||||
};
|
||||
|
||||
/// Parse `main_path`, resolve its imports, then build the raw import facts —
|
||||
/// the exact path `core.zig` drives. `alloc` must be an arena that outlives the
|
||||
/// returned views (they point into AST + cache memory it owns).
|
||||
fn buildFacts(alloc: std.mem.Allocator, io: std.Io, absdir: []const u8, main_path: []const u8) !Facts {
|
||||
const main_bytes = try std.Io.Dir.readFileAlloc(.cwd(), io, main_path, alloc, .limited(1 << 20));
|
||||
const main_source = try alloc.dupeZ(u8, main_bytes);
|
||||
var p = parser.Parser.init(alloc, main_source);
|
||||
const root = p.parse() catch return error.ParseFailed;
|
||||
|
||||
var diags = errors.DiagnosticList.init(alloc, main_source, main_path);
|
||||
var chain = std.StringHashMap(void).init(alloc);
|
||||
var cache = imports.ModuleCache.init(alloc);
|
||||
var import_graph = std.StringHashMap(std.StringHashMap(void)).init(alloc);
|
||||
var flat_import_graph = std.StringHashMap(std.StringHashMap(void)).init(alloc);
|
||||
const stdlib_paths = [_][]const u8{};
|
||||
|
||||
const mod = try imports.resolveImports(
|
||||
alloc,
|
||||
io,
|
||||
root,
|
||||
absdir,
|
||||
main_path,
|
||||
&chain,
|
||||
&cache,
|
||||
null,
|
||||
&diags,
|
||||
&stdlib_paths,
|
||||
&import_graph,
|
||||
&flat_import_graph,
|
||||
.{},
|
||||
);
|
||||
|
||||
const facts = try imports.buildImportFacts(alloc, main_path, mod, &cache);
|
||||
return .{ .decls = facts.decls, .ns_edges = facts.ns_edges, .diags = diags };
|
||||
}
|
||||
|
||||
fn expectTag(ref: imports.RawDeclRef, expected: std.meta.Tag(imports.RawDeclRef)) !void {
|
||||
try std.testing.expectEqual(expected, std.meta.activeTag(ref));
|
||||
}
|
||||
|
||||
fn hasErr(diags: *const errors.DiagnosticList, needle: []const u8) bool {
|
||||
for (diags.items.items) |d| {
|
||||
if (d.level == .err and std.mem.indexOf(u8, d.message, needle) != null) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Two flat-imported modules each author `greet`; a third is namespaced. The
|
||||
// step retains BOTH `greet` authors under their own paths in `module_fns` and
|
||||
// records the namespaced import in `import_graph` but NOT in `flat_import_graph`
|
||||
@@ -186,3 +242,210 @@ test "imports: mixed non-fn/fn same-name collision stays first-wins in merged sc
|
||||
try std.testing.expectEqual(@as(usize, 1), widget_count);
|
||||
try std.testing.expect(merged_is_struct);
|
||||
}
|
||||
|
||||
// Flat imports: each module's authored decls land in ITS OWN scalar index keyed
|
||||
// by path. Two modules authoring the same `fn`, the same `struct`, and a
|
||||
// value-vs-type same spelling are ALL retained per-source — no cross-module
|
||||
// first-wins at the index level. A `const_decl` is stored raw (`.const_decl`),
|
||||
// not pre-classified into value/fn.
|
||||
test "buildImportFacts: flat imports keep same-name fn/struct + value-vs-type per source; const stays raw" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
// a.sx: dup() fn, Box struct, Shape as a VALUE const.
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "a.sx", .data = "dup :: () -> s64 { 1 }\nBox :: struct { x: s64 }\nShape :: 7;\n" });
|
||||
// b.sx: dup() fn, Box struct, Shape as a TYPE (same spelling as a.sx's value).
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "b.sx", .data = "dup :: () -> s64 { 2 }\nBox :: struct { y: s64 }\nShape :: struct { z: s64 }\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "#import \"a.sx\";\n#import \"b.sx\";\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
const a_path = try std.fmt.allocPrint(alloc, "{s}/a.sx", .{absdir});
|
||||
const b_path = try std.fmt.allocPrint(alloc, "{s}/b.sx", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
const a_idx = facts.decls.get(a_path) orelse return error.MissingAIndex;
|
||||
const b_idx = facts.decls.get(b_path) orelse return error.MissingBIndex;
|
||||
const m_idx = facts.decls.get(main_path) orelse return error.MissingMainIndex;
|
||||
|
||||
// The index records its own source path.
|
||||
try std.testing.expectEqualStrings(a_path, a_idx.source);
|
||||
|
||||
// main authors `main` as a fn.
|
||||
try expectTag(m_idx.names.get("main") orelse return error.MissingMain, .fn_decl);
|
||||
|
||||
// Same-name fn retained per source — two DISTINCT FnDecls.
|
||||
const a_dup = a_idx.names.get("dup") orelse return error.MissingADup;
|
||||
const b_dup = b_idx.names.get("dup") orelse return error.MissingBDup;
|
||||
try expectTag(a_dup, .fn_decl);
|
||||
try expectTag(b_dup, .fn_decl);
|
||||
try std.testing.expect(a_dup.fn_decl != b_dup.fn_decl);
|
||||
|
||||
// Same-name struct retained per source — two DISTINCT StructDecls.
|
||||
const a_box = a_idx.names.get("Box") orelse return error.MissingABox;
|
||||
const b_box = b_idx.names.get("Box") orelse return error.MissingBBox;
|
||||
try expectTag(a_box, .struct_decl);
|
||||
try expectTag(b_box, .struct_decl);
|
||||
try std.testing.expect(a_box.struct_decl != b_box.struct_decl);
|
||||
|
||||
// Value-vs-type same spelling across modules: a.sx's `Shape` is a raw const
|
||||
// (NOT pre-classified), b.sx's `Shape` is a struct. Both coexist by source.
|
||||
try expectTag(a_idx.names.get("Shape") orelse return error.MissingAShape, .const_decl);
|
||||
try expectTag(b_idx.names.get("Shape") orelse return error.MissingBShape, .struct_decl);
|
||||
|
||||
// No spurious diagnostics — these are distinct files, not same-module dups.
|
||||
try std.testing.expect(!hasErr(&facts.diags, "duplicate top-level"));
|
||||
}
|
||||
|
||||
// Directory import: the combined module (keyed by the directory path) carries
|
||||
// the UNION of every file's authored decls in its scalar index.
|
||||
test "buildImportFacts: directory import unions member-file decls under the dir path" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
try tmp.dir.createDirPath(io, "lib");
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "lib/one.sx", .data = "from_one :: () -> s64 { 1 }\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "lib/two.sx", .data = "Two :: struct { v: s64 }\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "#import \"lib\";\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
const lib_path = try std.fmt.allocPrint(alloc, "{s}/lib", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
const lib_idx = facts.decls.get(lib_path) orelse return error.MissingLibIndex;
|
||||
try expectTag(lib_idx.names.get("from_one") orelse return error.MissingFromOne, .fn_decl);
|
||||
try expectTag(lib_idx.names.get("Two") orelse return error.MissingTwo, .struct_decl);
|
||||
}
|
||||
|
||||
// Namespaced file import (`g :: #import "point.sx"`): recorded as a namespace
|
||||
// edge whose `target_module_path` is the aliased file (the fact lost today),
|
||||
// AND as a `.namespace_decl` in the importer's scalar index.
|
||||
test "buildImportFacts: namespaced file import captures target_module_path" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "point.sx", .data = "Point :: struct { x: s64 }\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "g :: #import \"point.sx\";\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
const point_path = try std.fmt.allocPrint(alloc, "{s}/point.sx", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
const main_edges = facts.ns_edges.get(main_path) orelse return error.MissingMainEdges;
|
||||
const g = main_edges.get("g") orelse return error.MissingGEdge;
|
||||
try std.testing.expectEqualStrings("g", g.alias);
|
||||
try std.testing.expectEqualStrings(main_path, g.importer_source);
|
||||
try std.testing.expectEqualStrings(point_path, g.target_module_path);
|
||||
try std.testing.expect(g.own_decls.len >= 1);
|
||||
try std.testing.expect(!g.is_pub);
|
||||
|
||||
// The alias is also a `.namespace_decl` in the importer's scalar index.
|
||||
const m_idx = facts.decls.get(main_path) orelse return error.MissingMainIndex;
|
||||
try expectTag(m_idx.names.get("g") orelse return error.MissingGRef, .namespace_decl);
|
||||
}
|
||||
|
||||
// Namespaced directory import: same edge capture, target is the directory path.
|
||||
test "buildImportFacts: namespaced directory import captures dir path as target" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
try tmp.dir.createDirPath(io, "pkg");
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "pkg/m.sx", .data = "helper :: () -> s64 { 9 }\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "pkg :: #import \"pkg\";\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
const pkg_path = try std.fmt.allocPrint(alloc, "{s}/pkg", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
const main_edges = facts.ns_edges.get(main_path) orelse return error.MissingMainEdges;
|
||||
const pkg = main_edges.get("pkg") orelse return error.MissingPkgEdge;
|
||||
try std.testing.expectEqualStrings(pkg_path, pkg.target_module_path);
|
||||
}
|
||||
|
||||
// C-import namespace (`c :: #import c { #include ... }`): recorded as a namespace
|
||||
// edge. With no separate sx module, the target is the importing file itself.
|
||||
test "buildImportFacts: c-import namespace recorded as an edge" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "ch.h", .data = "int cm_add(int a, int b);\n" });
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "cmod :: #import c {\n #include \"ch.h\";\n};\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
const main_edges = facts.ns_edges.get(main_path) orelse return error.MissingMainEdges;
|
||||
const cmod = main_edges.get("cmod") orelse return error.MissingCmodEdge;
|
||||
try std.testing.expectEqualStrings("cmod", cmod.alias);
|
||||
try std.testing.expectEqualStrings(main_path, cmod.target_module_path);
|
||||
|
||||
const m_idx = facts.decls.get(main_path) orelse return error.MissingMainIndex;
|
||||
try expectTag(m_idx.names.get("cmod") orelse return error.MissingCmodRef, .namespace_decl);
|
||||
}
|
||||
|
||||
// Duplicate-name invariant (R5 #2): a same-module authored duplicate top-level
|
||||
// name is DIAGNOSED, not silently dropped. The parser/decl-checker does not
|
||||
// catch this today (verified: `sx run` of a same-file double decl exits 0 with
|
||||
// no diagnostic), so `resolveImports` surfaces it where `addOwnDecl` refuses the
|
||||
// second author. This test FAILS on the pre-diagnostic code and PASSES after.
|
||||
test "buildImportFacts: same-module duplicate top-level name is diagnosed" {
|
||||
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const alloc = arena.allocator();
|
||||
const io = testIo();
|
||||
|
||||
var tmp = std.testing.tmpDir(.{});
|
||||
defer tmp.cleanup();
|
||||
|
||||
try tmp.dir.writeFile(io, .{ .sub_path = "main.sx", .data = "foo :: () -> s64 { 1 }\nfoo :: () -> s64 { 2 }\nmain :: () -> s32 { 0 }\n" });
|
||||
|
||||
var dirbuf: [4096]u8 = undefined;
|
||||
const absdir = dirbuf[0..try tmp.dir.realPath(io, &dirbuf)];
|
||||
const main_path = try std.fmt.allocPrint(alloc, "{s}/main.sx", .{absdir});
|
||||
|
||||
var facts = try buildFacts(alloc, io, absdir, main_path);
|
||||
|
||||
try std.testing.expect(hasErr(&facts.diags, "duplicate top-level declaration 'foo'"));
|
||||
// The surviving author is still in the scalar index (first-wins, not lost).
|
||||
const m_idx = facts.decls.get(main_path) orelse return error.MissingMainIndex;
|
||||
try expectTag(m_idx.names.get("foo") orelse return error.MissingFoo, .fn_decl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user