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

@@ -1590,6 +1590,25 @@ pub const Lowering = struct {
const saved_block_terminated = self.block_terminated;
const saved_force_block_value = self.force_block_value;
const saved_source_file = self.current_source_file;
// Lowering a callee must be transparent to the caller's lowering
// state: restore the FULL saved context on EVERY exit path through one
// defer so the three exits (non-null branch, already-promoted early
// return, null-FuncId `ns.fn` alias branch) cannot drift. Notably
// `block_terminated` — a qualified alias whose body terminates (e.g. a
// constant-folded `if true { return … }`) leaves it true, and leaking
// that into the caller marks the caller's own trailing statements
// dead-after-terminator (issue 0100 F2). The jni/pack/foreign-class
// fields keep their own defers above.
defer {
self.setCurrentSourceFile(saved_source_file);
self.scope = saved_scope;
self.func_defer_base = saved_defer_base;
self.block_terminated = saved_block_terminated;
self.force_block_value = saved_force_block_value;
self.builder.func = saved_func;
self.builder.current_block = saved_block;
self.builder.inst_counter = saved_counter;
}
// The `#jni_env` Ref stack is lexical within ONE function's instruction
// stream — Refs from the caller don't dereference correctly in this
// callee's body. Move the visible base to the current top so
@@ -1650,15 +1669,7 @@ pub const Lowering = struct {
self.setCurrentSourceFile(src);
}
self.lowerFunction(fd, name, false);
// Restore builder state
self.setCurrentSourceFile(saved_source_file);
self.scope = saved_scope;
self.func_defer_base = saved_defer_base;
self.force_block_value = saved_force_block_value;
self.builder.func = saved_func;
self.builder.current_block = saved_block;
self.builder.inst_counter = saved_counter;
return;
return; // caller state restored by the top-level defer
}
if (func_id) |fid| {
@@ -1667,15 +1678,8 @@ pub const Lowering = struct {
const func = &self.module.functions.items[@intFromEnum(fid)];
self.setCurrentSourceFile(func.source_file);
if (!func.is_extern) {
// Already promoted (e.g., via lowerComptimeDeps) — skip
self.setCurrentSourceFile(saved_source_file);
self.scope = saved_scope;
self.func_defer_base = saved_defer_base;
self.block_terminated = saved_block_terminated;
self.force_block_value = saved_force_block_value;
self.builder.func = saved_func;
self.builder.current_block = saved_block;
self.builder.inst_counter = saved_counter;
// Already promoted (e.g., via lowerComptimeDeps) — skip.
// Caller state restored by the top-level defer.
return;
}
func.is_extern = false; // promote from extern stub to real function
@@ -1745,16 +1749,7 @@ pub const Lowering = struct {
self.builder.finalize();
}
// Restore builder state
self.setCurrentSourceFile(saved_source_file);
self.scope = saved_scope;
self.func_defer_base = saved_defer_base;
self.block_terminated = saved_block_terminated;
self.force_block_value = saved_force_block_value;
self.builder.func = saved_func;
self.builder.current_block = saved_block;
self.builder.inst_counter = saved_counter;
// Caller state restored by the top-level defer.
}
/// Lower a single function declaration.