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

Open
#356 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 1 day

@namin is already working on this.

Since Nov 23, 2026.

  • #363 by @namin — open

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
30/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

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.

Written by the indexing model from the issue text.

Description

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.
Dominant language
TypeScript
Stars
113
Forks
13
Avg merge
15h 35m
Merged PRs (30d)
56

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.