diff --git a/packages/compiler/src/frontend/lowering/lower-functions.ts b/packages/compiler/src/frontend/lowering/lower-functions.ts index 6fc083468..8f2c575a4 100644 --- a/packages/compiler/src/frontend/lowering/lower-functions.ts +++ b/packages/compiler/src/frontend/lowering/lower-functions.ts @@ -2,6 +2,7 @@ import { declareTypedArguments } from "./typed-arguments.js"; import { dynUndefinedExpr, varRef } from "../../ir/build.js"; import { everyStmtList, transformStmtList } from "../../ir/traverse.js"; import { bindingNeverReassigned } from "./binding-analysis.js"; +import { declareContextLocal } from "./function-context.js"; import { isErrorFirstErrorType } from "./builtins/arguments.js"; import { InternalCompilerError } from "../../errors.js"; import * as ts from "../ts7/adapter.js"; @@ -786,7 +787,8 @@ function lowerUndefinedResultBody( lowerer.suppressStats = stats; } }; - const slot = lowerer.declareHiddenLocal("%result", DYN); + // Mutable: the body may assign to its result (`if (!out) out = ...`). + const slot = declareContextLocal(lowerer.ctx, "%result", DYN, true, undefined, undefined); const undefinedResult: IrStmt = { kind: "varDecl", localId: slot.id, diff --git a/tests/corpus/6920-zlib-callback-error-result.ts b/tests/corpus/6920-zlib-callback-error-result.ts index cc9128221..5016fa841 100644 --- a/tests/corpus/6920-zlib-callback-error-result.ts +++ b/tests/corpus/6920-zlib-callback-error-result.ts @@ -176,6 +176,19 @@ function namedRetry(): void { }); } +function fallbackResult(): void { + // The body assigns a fallback Buffer to its result. + zlib.inflate(bad, (err, out) => { + if (!out) out = Buffer.from("fallback"); + console.log("fallback", err?.code, typeof out, out.toString()); + zlib.inflate(good, (err2, out2) => { + if (err2) out2 = Buffer.alloc(0); + console.log("fallback nested", err2, out2.length); + step(); + }); + }); +} + function functionValues(next: () => void): void { steps.push( () => zlib.inflate(good, named), @@ -203,6 +216,7 @@ function functionValues(next: () => void): void { () => zlib.inflate(good, captures), nestedForward, namedRetry, + fallbackResult, () => { named(new Error("direct"), Buffer.from("both")); },