A mutating method called on a narrowed optional updates only the match binder, so a false `ensures` can verify
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, casesomeMatch, callsreplacePathInTStmtsto replaceawith the binder before the arm's statements are lowered.emitMatchStmtdoes the same for discriminated unions throughreplaceFieldsInTStmts.- The
exprcase then lowers the mutating call on the already-substituted receiver into an assignment whose target is the binder.buildNestedFieldUpdatehandles the field case the same way. poptakes a different route: theletandassigncases (around lines 1811 and 1875) read the array name from the already-substituted receiver. A fix limited to theexprcase would leave it broken.- Updating only the root (
a := Some(i_a_val + [99])) is not enough by itself, because later reads ofain the same arm were already rewritten toi_a_val. #342 and PR #347 face the same problem for plain assignment. findReassignedNamesscansif,while,forofandswitchbodies but notsomeMatchortagMatchbodies, so these writes are not counted as reassignments ofa.
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from midspiral/LemmaScript
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
midspiral/LemmaScript#355 · 1 comment ·
Maintainers usually reply within 1 day
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 25/100
midspiral/LemmaScript#362 ·
Maintainers usually reply within 1 day
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 15/100
midspiral/LemmaScript#361 ·
Maintainers usually reply within 1 day
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyPossibly taken @namin claimed this today. Open
Difficulty 4/5 3-5 days Newbie friendliness 18/100
midspiral/LemmaScript#360 ·
Maintainers usually reply within 1 day
-
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 verifyPossibly taken @namin claimed this today. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
midspiral/LemmaScript#356 · 1 reaction ·
Maintainers usually reply within 1 day
All issues in midspiral/LemmaScript
Similar issues
-
bug done
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neavo/LinguaGacha#1124 ·
Maintainers usually reply within 1 day
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsPossibly taken @fullsend-ai-coder claimed this today. Opene2e-failure ready-to-code
Difficulty 2/5 Under an hour Newbie friendliness 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 comment ·
Maintainers usually reply within 1 day
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
brylie/mission-island#225 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google-gemini/gemini-cli#30412 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day