`!= null`, `== null`, `!= undefined` and `!(x === undefined)` guards do not narrow an optional
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- typescript
- 領域
- compilers
調査の方向性
The narrowing rules live in the "Narrow Rules" section of TOOLS.md and in the generator at tools/src/lsc.ts, which emits the if conditions for gen --backend=dafny. Reproduce with the snippet in the issue, then extend the guard recognition to != null, != undefined, == null, == undefined and !(e === undefined). Done means the five sources verify under dafny verify and the regression tests cover each position listed. Open PR #345 already targets this, so check it before starting.
索引モデルが issue の本文から書いたものです。
説明
Problem
TypeScript narrows an optional after a loose null check (i != null, i != undefined, an early return on i == null) and after a negated strict check (!(i === undefined)). The narrowing rules in TOOLS.md ("Narrow Rules") recognize !== undefined, === undefined, truthiness and !e, but not these forms. lsc gen exits 0 and passes the unnarrowed Option into an index or a return value. Dafny rejects the output, so no false proof results, but valid TypeScript does not verify, and the error points at generated Dafny instead of the guard that lsc did not recognize.
Reproduced on upstream main at add6ccd (0.6.10).
Reproduction
export function looseIdx(a: number[], i?: number): number {
if (i != null && i >= 0 && i < a.length) return a[i];
return 0;
}
npx tsx tools/src/lsc.ts gen --backend=dafny repro.ts emits:
function looseIdx(a: seq<int>, i: Option<int>): int
{
if (((i != None) && (match i { case Some(i_value) => (i_value >= 0) case None => false })) && (match i { case Some(i_value) => (i_value < |a|) case None => false })) then
a[i]
else
0
}
dafny verify reports incorrect type for selection into seq<int> (got Option<int>). Each comparison in the chain is wrapped in its own match i, which type-checks, but the then branch indexes a with i itself, an Option<int>.
The other forms fail the same way:
| Source | Dafny error |
|---|---|
if (!(i === undefined) && i >= 0 && i < a.length) return a[i]; |
incorrect type for selection into seq<int> (got Option<int>) |
if (i == null) return 0; if (i < a.length && i >= 0) return a[i]; |
incorrect type for selection into seq<int> (got Option<int>) |
if (o != null) return o; return 0; |
Function body type mismatch (expected int, got Option<int>) |
if (o != undefined) return o; return 0; |
Function body type mismatch (expected int, got Option<int>) |
With !== undefined in place of != null, != undefined and !(i === undefined), and === undefined in place of == null, the same functions pass dafny verify.
Expected
For a T | undefined value, e != null and e != undefined (in either operand order) narrow like e !== undefined, e == null and e == undefined narrow like e === undefined, and !(e === undefined) narrows like e !== undefined, in every position where the strict forms narrow. null and undefined both lower to None, so for T | undefined these forms test the same thing. For T | null | undefined see #304.
If a form stays unsupported, lsc should report it at the source position rather than emit Dafny that does not type-check.
Acceptance criteria
- The five sources above generate Dafny that verifies.
- A regression test covers
!= null,!= undefinedand!(e === undefined)in anifstatement, an&&chain and a conditional expression;== nulland== undefinedin an early-returnifand a conditional expression; and the reversed operand order (null != e).
- 主要言語
- 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 日以内に返信
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verify対応中かも @namin が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 18/100
midspiral/LemmaScript#360 ·
メンテナーはふだん 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 日以内に返信
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 日以内に返信