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

Open
#350 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

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.

Written by the indexing model from the issue text.

Description

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.
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.