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

Abierto
#356 0 comentarios 1 reacción 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@namin ya está trabajando en esto.

Desde el 23/11/2026.

  • #363 de @namin — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
Medio día
Aptitud para principiantes
30/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
TypeScript
Estrellas
113
Forks
13
Merge medio
15 h 35 min
PR fusionados (30 d)
56

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de midspiral/LemmaScript

Todos los issues de midspiral/LemmaScript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.