fix(lower): qualified alias lowers in its own source context [0100 F1]
The 0100 identity fix registers a namespaced import's own functions under a module-qualified name (ns.fn) in fn_ast_map WITHOUT an eager declareFunction, so the alias is lowered through lazyLowerFunction's null-FuncId lowerFunction path. That path had no Function.source_file to restore (the non-null path does setCurrentSourceFile(func.source_file)), so the alias lowered in the CALLER's visibility context. A qualified function that called a helper from its OWN module's flat import was then rejected "not visible". Fix: - ProgramIndex.qualified_fn_source maps each ns.fn alias to its declaring source file, populated in registerQualifiedFn (current_source_file is pinned to the decl's source by registerNamespaceQualifiedFns). - lazyLowerFunction's null-FuncId branch restores that source before lowerFunction, so ns.fn's body lowers in its own module's context and its intra-module / own-import callees resolve. - lowerFunction records Function.source_file = current_source_file on the freshly-begun function (matching declareFunction), so the lowered alias carries its own module for diagnostics/emit. Regression: examples/0720-modules-qualified-own-import.sx — calc.compute (a qualified alias) calls triple/base from calc.sx's own flat import; reports "'triple' is not visible" on the attempt-1 code, passes after. 0719's cross-module dual-parse assertion stays green. issues/0100 RESOLVED banner extended with the F1 follow-up.
This commit is contained in:
@@ -1478,6 +1478,14 @@ pub const Lowering = struct {
|
||||
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 {};
|
||||
// Carry the alias's OWN declaring source file (the caller in
|
||||
// `registerNamespaceQualifiedFns` pins `current_source_file` to the
|
||||
// decl's source before each call). `lazyLowerFunction`'s null-FuncId
|
||||
// path restores this so `ns.fn`'s body lowers in its own module's
|
||||
// visibility context, not the call site's (issue 0100 F1).
|
||||
if (self.current_source_file) |src| {
|
||||
self.program_index.qualified_fn_source.put(qualified, src) 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
|
||||
@@ -1630,7 +1638,17 @@ pub const Lowering = struct {
|
||||
}
|
||||
|
||||
if (func_id == null) {
|
||||
// Function not yet declared — create it fresh via lowerFunction
|
||||
// Function not yet declared — create it fresh via lowerFunction.
|
||||
// A module-qualified alias (`ns.fn`, issue 0100) is registered in
|
||||
// `fn_ast_map` without an eager `declareFunction`, so there's no
|
||||
// `Function.source_file` to switch to (the path above). Restore the
|
||||
// alias's OWN declaring source before lowering its body, otherwise
|
||||
// it lowers in the caller's visibility context and an own-import
|
||||
// callee (`foo` calling `helper` from `foo`'s module's flat import)
|
||||
// is reported "not visible" (issue 0100 F1).
|
||||
if (self.program_index.qualified_fn_source.get(name)) |src| {
|
||||
self.setCurrentSourceFile(src);
|
||||
}
|
||||
self.lowerFunction(fd, name, false);
|
||||
// Restore builder state
|
||||
self.setCurrentSourceFile(saved_source_file);
|
||||
@@ -1803,6 +1821,11 @@ pub const Lowering = struct {
|
||||
);
|
||||
_ = func_id;
|
||||
self.builder.currentFunc().has_implicit_ctx = wants_ctx;
|
||||
// Record the declaring source so the function carries its own module
|
||||
// for diagnostics/emit and for any later `lazyLowerFunction` re-entry
|
||||
// that switches to `func.source_file`. The caller sets
|
||||
// `current_source_file` to the decl's source before lowering (issue 0100 F1).
|
||||
self.builder.currentFunc().source_file = self.current_source_file;
|
||||
|
||||
// Set linkage. Default for fn defs is `internal` (LLVM DCE-friendly,
|
||||
// matches C `static`). isExportedEntryName lists the names the OS
|
||||
|
||||
@@ -591,6 +591,14 @@ pub const ProgramIndex = struct {
|
||||
// ── Declaration maps ──
|
||||
/// Function name → AST decl.
|
||||
fn_ast_map: std.StringHashMap(*const ast.FnDecl),
|
||||
/// Module-qualified function name (`ns.fn`) → its declaring source file.
|
||||
/// A qualified alias is registered in `fn_ast_map` WITHOUT an eager
|
||||
/// `declareFunction`, so `lazyLowerFunction` lowers it through the
|
||||
/// null-FuncId `lowerFunction` path with no `Function.source_file` to
|
||||
/// restore. This carries the alias's OWN module source so its body lowers
|
||||
/// in the right visibility context — its intra-module / own-import callees
|
||||
/// resolve (issue 0100 F1). Keyed/allocated with the lowering allocator.
|
||||
qualified_fn_source: std.StringHashMap([]const u8),
|
||||
/// sx alias → ForeignClassDecl (jni_class / objc_class / swift_class / ... — registered in scan pass).
|
||||
foreign_class_map: std.StringHashMap(*const ast.ForeignClassDecl) = std.StringHashMap(*const ast.ForeignClassDecl).init(std.heap.page_allocator),
|
||||
/// `#run` global name → GlobalId.
|
||||
@@ -613,6 +621,7 @@ pub const ProgramIndex = struct {
|
||||
return .{
|
||||
.import_flags = std.StringHashMap(bool).init(alloc),
|
||||
.fn_ast_map = std.StringHashMap(*const ast.FnDecl).init(alloc),
|
||||
.qualified_fn_source = std.StringHashMap([]const u8).init(alloc),
|
||||
.global_names = std.StringHashMap(GlobalInfo).init(alloc),
|
||||
};
|
||||
}
|
||||
@@ -621,6 +630,7 @@ pub const ProgramIndex = struct {
|
||||
// Owned maps only — module_scopes / import_graph are borrowed.
|
||||
self.import_flags.deinit();
|
||||
self.fn_ast_map.deinit();
|
||||
self.qualified_fn_source.deinit();
|
||||
self.foreign_class_map.deinit();
|
||||
self.global_names.deinit();
|
||||
self.type_alias_map.deinit();
|
||||
|
||||
Reference in New Issue
Block a user