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:
26
examples/0720-modules-qualified-own-import.sx
Normal file
26
examples/0720-modules-qualified-own-import.sx
Normal file
@@ -0,0 +1,26 @@
|
||||
// Regression (issue 0100 F1): a QUALIFIED imported function that calls a
|
||||
// function from its OWN flat import.
|
||||
//
|
||||
// `calc :: #import …` registers `calc.compute` as a module-qualified alias
|
||||
// with a unique FuncId (the identity fix that resolves the cross-module
|
||||
// same-name `parse` collision, issue 0100 / example 0719). That alias is
|
||||
// lowered through `lazyLowerFunction`'s null-FuncId `lowerFunction` path,
|
||||
// which has no declared `Function.source_file` to restore. Before the fix it
|
||||
// lowered `calc.compute`'s body in the CALLER's (this file's) visibility
|
||||
// context, so `compute`'s calls to `triple` / `base` — visible only from
|
||||
// calc.sx's own `#import "util.sx"` — were rejected "not visible". The fix
|
||||
// carries the alias's declaring source so it lowers in calc.sx's context.
|
||||
|
||||
#import "modules/std.sx";
|
||||
calc :: #import "0720-modules-qualified-own-import/calc.sx";
|
||||
|
||||
report :: (label: string, ok: bool) {
|
||||
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
// 14 * 3 = 42, computed by calc.compute -> triple(base()), both of which
|
||||
// live in calc.sx's own flat import.
|
||||
report("qualified-own-import", calc.compute() == 42);
|
||||
0
|
||||
}
|
||||
8
examples/0720-modules-qualified-own-import/calc.sx
Normal file
8
examples/0720-modules-qualified-own-import/calc.sx
Normal file
@@ -0,0 +1,8 @@
|
||||
// `calc` is pulled in under a QUALIFIED namespace by the consumer
|
||||
// (`calc :: #import …`), yet its own body calls `triple` / `base` from
|
||||
// calc.sx's OWN flat `#import "util.sx"`. The qualified alias `calc.compute`
|
||||
// must lower in calc.sx's source context so those own-import callees stay
|
||||
// visible — issue 0100 F1.
|
||||
#import "util.sx";
|
||||
|
||||
compute :: () -> s64 { return triple(base()); }
|
||||
2
examples/0720-modules-qualified-own-import/util.sx
Normal file
2
examples/0720-modules-qualified-own-import/util.sx
Normal file
@@ -0,0 +1,2 @@
|
||||
triple :: (x: s64) -> s64 { return x * 3; }
|
||||
base :: () -> s64 { return 14; }
|
||||
1
examples/expected/0720-modules-qualified-own-import.exit
Normal file
1
examples/expected/0720-modules-qualified-own-import.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
qualified-own-import: ok
|
||||
@@ -38,6 +38,37 @@ and `std.json` under distinct namespaces and calls both `cli.parse`
|
||||
(dispatch) and `json.parse` (document read), asserting correct results.
|
||||
Panics on pre-fix code; passes after.
|
||||
|
||||
## F1 follow-up — qualified alias must lower in its own source context
|
||||
|
||||
The identity fix above registered `ns.fn` in `fn_ast_map` WITHOUT an eager
|
||||
`declareFunction`, so the qualified alias is lowered through
|
||||
`lazyLowerFunction`'s **null-FuncId** `lowerFunction` path — which had no
|
||||
`Function.source_file` to restore (the non-null path does
|
||||
`setCurrentSourceFile(func.source_file)`). The alias therefore lowered in the
|
||||
**caller's** visibility context, and a qualified function calling a helper
|
||||
from **its own module's flat import** was rejected:
|
||||
|
||||
```
|
||||
m :: #import "m.sx"; // m.sx: `#import "helper.sx"; foo :: () { helper() }`
|
||||
main :: () -> s32 { print("{}\n", m.foo()); 0 } // → 'helper' is not visible
|
||||
```
|
||||
|
||||
**Fix** (`src/ir/program_index.zig`, `src/ir/lower.zig`):
|
||||
|
||||
- New `ProgramIndex.qualified_fn_source` (qualified name → declaring source
|
||||
file), populated in `registerQualifiedFn` from the decl's own source.
|
||||
- `lazyLowerFunction`'s null-FuncId branch restores that source via
|
||||
`setCurrentSourceFile` before calling `lowerFunction`, so `ns.fn`'s body
|
||||
lowers in its own module's context and its own-import callees resolve.
|
||||
- `lowerFunction` now 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.
|
||||
|
||||
## Symptom
|
||||
|
||||
- **Observed:** a program that imports two modules each exporting a
|
||||
@@ -86,8 +117,11 @@ machinery already existed but was never fed module-qualified entries.
|
||||
## Fix verification
|
||||
|
||||
- `zig build` → 0
|
||||
- `zig build test` → 0 (incl. LSP corpus sweep, 471 examples)
|
||||
- `bash tests/run_examples.sh` → 454 passed, 0 failed
|
||||
- `zig build test` → 0 (incl. LSP corpus sweep, 472 examples)
|
||||
- `bash tests/run_examples.sh` → 455 passed, 0 failed
|
||||
- `examples/0719-modules-cli-and-json.sx`: panics pre-fix, passes post-fix.
|
||||
- `examples/0720-modules-qualified-own-import.sx`: `'… is not visible'` on
|
||||
the attempt-1 code, passes after the F1 fix.
|
||||
|
||||
Regression test: `examples/0719-modules-cli-and-json.sx`.
|
||||
Regression tests: `examples/0719-modules-cli-and-json.sx` (collision),
|
||||
`examples/0720-modules-qualified-own-import.sx` (F1 own-import visibility).
|
||||
|
||||
@@ -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