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

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

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

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

2026年11月23日 から。

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

評価

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

調査の方向性

Start in tools/src/extract.ts: the op === "=" branch in extractExpr (around line 516) emits a binop that later becomes ==, and desugarStmtExpr (around line 1350) does not unwrap a ParenthesizedExpression before checking for an assignment. Done means (x = 7); and const y = (x = 7); lower to an assignment or a source-positioned error, and the parenWholeAssign example no longer verifies. Note that pull request #363 is already open against this issue.

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

説明

Problem

When a JavaScript assignment appears in expression position, extract turns it into the IR's equality operator. (x = 7); and const y = (x = 7); therefore emit x == 7, the assignment disappears, and a false ensures verifies.

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

Reproduction

export function parenWholeAssign(p: number): number {
  //@ ensures p === 0 ==> \result === 0
  let x = p;
  (x = 7);
  return x;
}

parenWholeAssign(0) returns 7 in JavaScript. lsc gen --backend=dafny emits:

method parenWholeAssign(p: int) returns (res: int)
  ensures ((p == 0) ==> (res == 0))
{
  var x := p;
  var _ := (x == 7);
  return x;
}

and dafny verify reports 1 verified, 0 errors.

The same happens with an assignment in an initializer:

export function assignInInit(p: number): number {
  //@ ensures p === 0 ==> \result === 0
  let x = p;
  const y = (x = 7);
  return x;
}

emits var y := (x == 7); and verifies (1 verified, 0 errors), while JavaScript returns 7.

if ((x = 7) > 0) emits if ((x == 7) > 0), which Dafny rejects as a type error, so that form fails loudly.

Expected

A parenthesized assignment statement such as (x = 7); should be lowered the same way as x = 7;. An assignment anywhere else inside an expression should either be supported or be rejected with a source-positioned error. It should never become an equality test.

Likely cause

In tools/src/extract.ts:

  • extractExpr handles op === "=" by returning { kind: "binop", op: "=", ... } (around line 516, commented "extract as binop for now"). Later passes treat binop "=" as equality, which the Dafny emitter prints as ==.
  • desugarStmtExpr (line 1350) checks Node.isBinaryExpression(expr) without unwrapping a ParenthesizedExpression, so (x = 7); falls through to an expression statement and reaches the branch above. The for incrementor path also calls desugarStmtExpr.

Related

  • #355: a parenthesized assignment target, (x) = 7, in the same function. That one fails to parse in Dafny. Both need parentheses unwrapped in desugarStmtExpr.
主要言語
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 を短くまとめたダイジェスト。