Skip to content

Commit d35697b

Browse files
fix(compiler): address review feedback
1 parent 9036da1 commit d35697b

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

packages/compiler/src/frontend/lowering/lower-calls.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* function/lambda lowering and signature collection, and monomorphizing
44
* generic instantiation (bounded by MAX_GENERIC_INSTANCES). */
55
import * as ts from "../ts7/adapter.js";
6+
import { InternalCompilerError } from "../../errors.js";
67
import type { Lowerer } from "./lowerer.js";
78
import { lowerGenMethodCall } from "./lower-generators.js";
89
import { BOOL, CAUGHT, DYN, F64, IrExpr, IrFunction, IrLocal, IrParam, IrStmt, IrType, JSVAL, STRING, SYMBOL_T, SrcLoc, UNDEFINED_T, VOID, arrayOf, canBoxFuncIntoDyn, canConvertToDyn, canDynCheckTo, canMarshalTypedFuncIntoIsland, ffiClassType, ffiSourceParamTypes, funcOf, isFfiCallbackParam, isFfiContextParam, isFfiReleaseParam, isUnitType, shapeHasAccessorSlots, typeEquals } from "../../ir/ir.js";
@@ -5502,20 +5503,19 @@ const inliningPredicates = new Set<ts.Symbol>();
55025503
);
55035504
}
55045505

5505-
/** Nested `function name(...) {...}`: lowered as `const name = <lambda>`
5506-
* at the declaration's statement position (JS hoists function declarations
5507-
* to the top of the enclosing function — calling one before this statement
5508-
* is a compile error here, not a silent divergence). Self-references inside
5509-
* the body lower to `selfRef`, not a capture: a box holding its own
5510-
* closure would be an RC cycle. Reserve the box before lowering the body so
5511-
* mutually recursive declarations can capture each other's live boxes. */
5506+
/** Nested `function name(...) {...}` reserves its binding first, then lowers
5507+
* the lambda and assigns the resulting closure to that binding. The local is
5508+
* mutable in IR because this two-phase form lets mutually recursive
5509+
* declarations capture each other's live boxes before either closure is
5510+
* initialized. Self-references inside the body lower to `selfRef`, not a
5511+
* capture: a box holding its own closure would be an RC cycle. */
55125512
export function lowerNestedFunctionDecl(lowerer: Lowerer, stmt: ts.FunctionDeclaration): IrStmt {
55135513
if (!stmt.name) lowerer.unsupported("SC1090", stmt, "anonymous function declarations");
55145514
const { funcType } = lowerer.lambdaSignature(stmt);
5515-
const local = lowerer.declareLocal(stmt.name, stmt.name.text, funcType, false);
5516-
local.mutable = true;
5515+
const local = lowerer.declareLocal(stmt.name, stmt.name.text, funcType, true);
55175516
const active = [...lowerer.activeStmtLists].reverse().find((entry) => entry.stmts.includes(stmt));
5518-
active?.out.push({ kind: "varDecl", localId: local.id, init: null, loc: locOf(stmt) });
5517+
if (!active) throw new InternalCompilerError("lowerer bug: nested function has no owning statement list");
5518+
active.out.push({ kind: "varDecl", localId: local.id, init: null, loc: locOf(stmt) });
55195519
const init = lowerer.lowerLambda(stmt);
55205520
return { kind: "assign", localId: local.id, value: init, loc: locOf(stmt) };
55215521
}

0 commit comments

Comments
 (0)