Assigning a defined value to an optional does not narrow it for later reads

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

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

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
typescript
領域
compilers

調査の方向性

Start with the narrowing rules documented in TOOLS.md to see how flow-sensitive narrowing is currently expressed, then find the lowering code that emits Some(5) for the assignment and the read that emits return o;. The gap is that assignment-based narrowing is not tracked at the read, so the read lowers to the Option value instead of the unwrapped one. Work through all three forms in the reproduction (plain assignment, the === undefined guard, and ??) and add a regression test per form so dafny verify passes on each.

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

説明

Problem

TypeScript narrows an optional variable after it is assigned a defined value. Three common forms:

let o = p;
o = 5;
return o;   // TypeScript: o is number here
let o = p;
if (o === undefined) { o = 5; }
return o;   // TypeScript: o is number here
let o = p;
o = o ?? 5;
return o;   // TypeScript: o is number here

TypeScript narrows o to number after the assignment, because every path leaves it defined. lsc emits the assignments correctly (o := Some(5) in the first two), but then returns o as an Option. Dafny rejects the output, so no false proof results. None of the narrowing rules in TOOLS.md covers narrowing by assignment.

Reproduced on upstream main at add6ccd (0.6.10).

Reproduction

export function assignOutside(p?: number): number {
  let o = p;
  if (o === undefined) { o = 5; }
  return o;
}

emits:

method assignOutside(p: Option<int>) returns (res: int)
{
  var o := p;
  if (match o { case Some(i_) => false case None => true }) {
    o := Some(5);
  }
  return o;
}

dafny verify reports Method return value mismatch (expected int, got Option<int>).

The other two forms fail the same way: let o = p; o = 5; return o; and let o = p; o = o ?? 5; return o; report the same error. const o = p ?? 5; return o; verifies, because the new variable is declared with the non-optional type.

Expected

After an assignment that leaves x defined on every path, such as x = 5, if (x === undefined) { x = 5; } or x = x ?? 5, a read of x where TypeScript's type is the inner type lowers to the unwrapped value. In the reproduction, replacing return o; with return o.value; by hand verifies, because Dafny knows o.Some? on both paths.

If this stays unsupported, lsc should report it at the read rather than emit Dafny that does not type-check.

Acceptance criteria

  • All three forms above generate Dafny that verifies.
  • A regression test covers each form with a read after the assignment.
主要言語
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 を短くまとめたダイジェスト。