A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verify
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 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)returns5. - On
main, the output isvar f := () => p;, and Dafny rejects it withFunction 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
midspiral/LemmaScript のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
midspiral/LemmaScript#355 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
midspiral/LemmaScript#362 ·
メンテナーはふだん 1 日以内に返信
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 15/100
midspiral/LemmaScript#361 ·
メンテナーはふだん 1 日以内に返信
-
An assignment used as an expression, such as `(x = 7);` or `const y = (x = 7)`, is extracted as an equality test, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 3/5 半日 初心者へのやさしさ 30/100
midspiral/LemmaScript#356 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 62/100
midspiral/LemmaScript#354 ·
メンテナーはふだん 1 日以内に返信
midspiral/LemmaScript の issue をすべて見る
似ている issue
-
bug done
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
neavo/LinguaGacha#1124 ·
メンテナーはふだん 1 日以内に返信
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errors対応中かも @fullsend-ai-coder が今日担当しました。 オープンe2e-failure ready-to-code
難易度 2/5 1時間未満 初心者へのやさしさ 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
brylie/mission-island#225 ·
メンテナーはふだん 1 日以内に返信
-
status/need-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
google-gemini/gemini-cli#30412 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信