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

Đã đóng
#356 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@namin đang làm issue này rồi.

Từ ngày 23/11/2026.

  • #363 của @namin — đang mở
  • #372 của @namin — đã merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
Nửa ngày
Mức phù hợp với người mới
30/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
typescript
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
TypeScript
Star
113
Fork
13
Merge trung bình
12 giờ 22 phút
Pull request đã merge (30 ngày)
64

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của midspiral/LemmaScript

Tất cả issue của midspiral/LemmaScript

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.