A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verify

オープン
#360 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@namin がすでに取り組んでいます。

2026年11月23日 から。

  • #364 @namin による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
18/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
javascript, typescript
領域
compilers

調査の方向性

Start with the arrow-function extraction in extract.ts and the Dafny emission in resolve.ts, using the closureBeforeConst repro to see the lambda emitted before the inner const. The issue proposes rejecting this shape with a diagnostic when an arrow body refers to a let or const declared later in an enclosing block, but a pull request (#364) is already open against it. Done means lsc reports an error for the repro or emits Dafny where f() reads the inner p.

索引モデルが issue の本文から書いたものです。

説明

Problem

In JavaScript, a closure declared before a block-scoped const or let of the same name reads the inner declaration when it runs, because the closure runs after the declaration has initialized. The generated Dafny declares the lambda before the inner var, so the lambda reads the outer variable. A false ensures can verify, with or without narrowing.

Reproduction

//@ backend dafny
//@ ensures \result === p
export function closureBeforeConst(p: number, flag: boolean): number {
  if (flag) {
    const f = (): number => p;
    const p = 5;
    return f();
  }
  return p;
}

tsc --strict accepts this. In JavaScript, closureBeforeConst(3, true) returns 5, which breaks the ensures.

Generated Dafny:

function closureBeforeConst(p: int, flag: bool): int
{
  if flag then
    var f := () => p;
    var p := 5;
    f()
  else
    p
}

lsc check --backend=dafny: 1 verified, 0 errors.

The method lowering emits the same order (var f := () => p; var p := 5; r := f();) and also verifies a false ensures.

With a narrowed optional, the same shape is a Dafny type error on main. With #358 applied, it becomes a false proof:

//@ backend dafny
//@ ensures p !== undefined ==> \result === p
export function hoisted(p: number | undefined): number {
  if (p !== undefined) {
    const f = (): number => p;
    const p = 5;
    return f();
  }
  return 0;
}
  • In JavaScript, hoisted(3) returns 5.
  • On main, the output is var f := () => p;, and Dafny rejects it with Function body type mismatch (expected int, got Option<int>).
  • With #358, the output is var f := () => i_p_val; var p := 5; f(). It verifies: 1 verified, 0 errors.

Checked on main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.

Expected

lsc reports an error, or emits Dafny in which f() reads the inner p.

Likely cause

Extract keeps source order and resolve declares names sequentially, so nothing tracks that a closure body refers to a binding that is declared later in the same block. Dafny var scoping then binds the lambda's p to the outer variable.

Changing substitution in transform does not fix this. Leaving the lambda body unsubstituted still reads the outer variable. Skipping the rest of the block after the closure also skips the lets that resolve synthesizes when it inlines a callback with a default parameter, since their initializers read the outer name (resolve.ts concatenates that prefix with the lambda body).

A likely fix is to reject this shape in extract.ts, where arrow functions are extracted. For each identifier in an arrow body, check the ts-morph symbol's declaration. Report an error when that declaration is a let or const variable declaration or binding element outside the arrow, in an enclosing block, that starts after the arrow. Emitting the lambda after the declaration would match JavaScript only when nothing calls the closure in between, so rejection is the simpler correct option.

Related

  • #351 and #353 (narrowing substitution).
  • #358 fixes those, and with it the narrowed repro above changes from a type error to a false proof.
  • #361 (bare-block scoping) and #362 (if-branch scoping).
主要言語
TypeScript
スター
113
フォーク
13
平均マージ
15時間 35分
マージ済み PR(30日)
56

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

midspiral/LemmaScript のほかの issue

midspiral/LemmaScript の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。