A mutating method called on a narrowed optional updates only the match binder, so a false `ensures` can verify

Aperta
#350 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Start by reproducing the pushOnNarrowed snippet with lsc gen --backend=dafny and dafny verify to confirm the false ensures verifies. The write path is in tools/src/transform.ts: transformStmt (someMatch), emitMatchStmt, the expr case near line 1905, and the let/assign cases near lines 1811 and 1875, plus findReassignedNames. Done means the mutation reaches the narrowed variable or the program is rejected, and the pushOnNarrowed, popParam and pushOnDuField cases no longer verify falsely.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

Inside if (a !== undefined), lsc replaces a with the match binder i_a_val. When the block calls a method that mutates a, such as a.push(99), transform lowers the call to i_a_val := (i_a_val + [99]). Dafny accepts this because a pattern variable in a method match is a mutable local, and it discards the new value at the end of the arm. Reads of a after the block see the original sequence. JavaScript sees the pushed element, so a false ensures verifies.

#342 covers plain assignment (a = ...) and field assignment (c.v = 1) inside the narrowed block. Mutating method calls take a different path through transform, and PR #347 does not change it: the reproduction below still verifies on that branch.

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

Reproduction

export function pushOnNarrowed(p: number[] | undefined): number {
  //@ ensures p !== undefined && p.length === 0 ==> \result === 0
  let a = p;
  if (a !== undefined) {
    a.push(99);
  }
  return a !== undefined ? a.length : -1;
}

pushOnNarrowed([]) returns 1 in JavaScript. lsc gen --backend=dafny emits:

method pushOnNarrowed(p: Option<seq<int>>) returns (res: int)
  ensures (match p { case Some(i_p_val) => ((|i_p_val| == 0) ==> (res == 0)) case None => true })
{
  var a := p;
  match a {
    case Some(i_a_val) =>
      i_a_val := (i_a_val + [99]);
    case None =>
  }
  return (match a { case Some(i_a_val) => |i_a_val| case None => -1 });
}

and dafny verify reports Dafny program verifier finished with 1 verified, 0 errors, proving that the result is 0.

A field of a narrowed record is rewritten the same way. For b: Bag | undefined with b.items.push(9) inside if (b !== undefined), lsc emits i_b_val := i_b_val.(items := (i_b_val.items + [9]));, which updates only the binder.

pop and a discriminated-union arm fail the same way:

export function popParam(p: number[] | undefined): number {
  //@ ensures p !== undefined && p.length === 1 ==> \result === 1
  let a = p;
  if (a !== undefined) {
    const x = a.pop();
  }
  return a !== undefined ? a.length : -1;
}

export type Bag = { kind: "full"; items: number[] } | { kind: "empty" };

export function pushOnDuField(b: Bag): number {
  //@ ensures b.kind === "full" && b.items.length === 0 ==> \result === 0
  if (b.kind === "full") {
    b.items.push(99);
  }
  return b.kind === "full" ? b.items.length : -1;
}

popParam emits i_a_val := (if (|i_a_val| > 0) then i_a_val[0..(|i_a_val| - 1)] else i_a_val); and pushOnDuField emits i_b_items := (i_b_items + [99]);. The file verifies (3 verified, 0 errors), while JavaScript returns 0 for popParam([5]) and 1 for pushOnDuField({ kind: "full", items: [] }).

I did not test unshift, sort, set, add or delete. Transform lowers them through the same expr branch as push (tools/src/transform.ts, around line 1905), so I expect them to behave the same. const x = a.shift() inside the narrowed block is rejected on Dafny (Unsupported Dafny method call: .shift() on array), so it cannot produce a false proof there.

Expected

The mutation should reach the narrowed variable, for example a := Some(i_a_val + [99]), with later reads in the arm consistent with it. If lsc cannot represent the write soundly, it should reject the program. It should not assign only the match binder.

Likely cause

In tools/src/transform.ts:

  • transformStmt, case someMatch, calls replacePathInTStmts to replace a with the binder before the arm's statements are lowered. emitMatchStmt does the same for discriminated unions through replaceFieldsInTStmts.
  • The expr case then lowers the mutating call on the already-substituted receiver into an assignment whose target is the binder. buildNestedFieldUpdate handles the field case the same way.
  • pop takes a different route: the let and assign cases (around lines 1811 and 1875) read the array name from the already-substituted receiver. A fix limited to the expr case would leave it broken.
  • Updating only the root (a := Some(i_a_val + [99])) is not enough by itself, because later reads of a in the same arm were already rewritten to i_a_val. #342 and PR #347 face the same problem for plain assignment.
  • findReassignedNames scans if, while, forof and switch bodies but not someMatch or tagMatch bodies, so these writes are not counted as reassignments of a.

Related

  • #342: assignment inside the narrowed block. Same consequence, different statement form.
Lingua principale
TypeScript
Stelle
113
Fork
13
Merge medio
15h 35m
PR unite (30g)
56

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di midspiral/LemmaScript

Tutte le issue di midspiral/LemmaScript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.