fix(lower): null-FuncId path restores full caller state [0100 F2]

lazyLowerFunction's three exit paths (non-null branch, already-promoted
early return, null-FuncId `ns.fn` qualified-alias branch) each duplicated
the caller-state restore, and the null branch's copy had drifted: it
restored every saved field EXCEPT `block_terminated`. A qualified alias
whose body terminates (e.g. a constant-folded `if true { return ... }`)
leaves `block_terminated = true` after lowerFunction; the null path
returned without resetting it, so the flag leaked into the CALLER's body
lowering and the caller's own trailing statements / `return` were rejected
as dead-after-terminator ("function ... body produces no value").

Fix: collapse the three restores into a single `defer` registered right
after the state is saved, so every exit path restores the identical full
set and the class cannot diverge again. Fields restored on all paths:
current_source_file (F1), scope, func_defer_base, block_terminated (F2),
force_block_value, builder.func/current_block/inst_counter. The
foreign-class / jni-env / pack-mono / inline-return fields already had
their own defers and are unchanged.

Regression: examples/0721-modules-qualified-terminating-callee.sx — a
qualified alias `m.foo` folds `if true { return helper(); }` (helper from
m.sx's own import) and is followed by caller statements + the caller's own
`return 0`. Reports "body produces no value" pre-fix; prints
"terminating-callee: ok" / "after" and exits 0 after. 0719 (collision) and
0720 (F1 own-import visibility) stay green. issues/0100 RESOLVED banner
extended with the F2 follow-up.
This commit is contained in:
agra
2026-06-06 03:15:29 +03:00
parent 9274d47adf
commit d11f4c84b6
8 changed files with 125 additions and 31 deletions

View File

@@ -0,0 +1,31 @@
// Regression (issue 0100 F2): lowering a QUALIFIED imported function whose
// body terminates must leave the CALLER's lowering state untouched.
//
// `m :: #import …` registers `m.foo` as a module-qualified alias with a unique
// FuncId (the identity fix, issue 0100 / example 0719) and lowers it through
// `lazyLowerFunction`'s null-FuncId `lowerFunction` path. `foo`'s body folds
// `if true { return helper(); }` to an unconditional return, so its lowering
// ends with `block_terminated = true`. The null-FuncId path used to restore
// every saved caller field EXCEPT `block_terminated`, so that flag leaked back
// into `main`, and `main`'s own trailing `print` / `return 0` were treated as
// dead-after-terminator — the compiler rejected `return 0` with "body produces
// no value". The fix routes all exit paths through one save/restore defer, so
// the qualified alias is transparent to the caller. (`helper` also lives in
// m.sx's own flat import, exercising the F1 source-context restore too.)
#import "modules/std.sx";
m :: #import "0721-modules-qualified-terminating-callee/m.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> s32 {
// Qualified callee whose body terminates via a constant-folded `if true`.
x := m.foo();
// Caller statements AFTER the call must still be emitted (not dead).
report("terminating-callee", x == 7);
print("after\n");
// The caller's OWN return — rejected pre-fix because block_terminated leaked.
return 0;
}

View File

@@ -0,0 +1,3 @@
// Lives in m.sx's OWN flat import — reachable from `foo` but not from the
// top-level consumer that imports m.sx under a qualified namespace.
helper :: () -> s64 { return 7; }

View File

@@ -0,0 +1,10 @@
// `foo` is pulled in QUALIFIED by the consumer (`m :: #import …`). Its body
// terminates via a constant-folded `if true { return … }`, and the `return`
// calls `helper` from m.sx's OWN flat import. Lowering `foo` as a qualified
// alias must be transparent to the caller's lowering state — issue 0100 F2.
#import "helper.sx";
foo :: () -> s64 {
if true { return helper(); }
return 0;
}

View File

@@ -0,0 +1 @@
0

View File

@@ -0,0 +1,2 @@
terminating-callee: ok
after