A mutating method called on a narrowed optional updates only the match binder, so a false `ensures` can verify
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, 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.
- 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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di midspiral/LemmaScript
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
midspiral/LemmaScript#355 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
In a pure function body, a `const` declared inside an `if` branch stays in scope after the `if`, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 25/100
midspiral/LemmaScript#362 ·
I maintainer di solito rispondono entro 1 giorno
-
A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 15/100
midspiral/LemmaScript#361 ·
I maintainer di solito rispondono entro 1 giorno
-
A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 18/100
midspiral/LemmaScript#360 ·
I maintainer di solito rispondono entro 1 giorno
-
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 verifyForse già presa @namin l’ha presa oggi. Aperta
Difficoltà 3/5 Mezza giornata Idoneità per principianti 30/100
midspiral/LemmaScript#356 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di midspiral/LemmaScript
Issue simili
-
bug done
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
neavo/LinguaGacha#1124 ·
I maintainer di solito rispondono entro 1 giorno
-
[fullsend] E2E: backstage-microsoft-auth — catalogQuery missing error handler for network errorsForse già presa @fullsend-ai-coder l’ha presa oggi. Apertae2e-failure ready-to-code
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 78/100
redhat-developer/rhdh-plugin-export-overlays#4821 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
brylie/mission-island#225 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement from-app needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
nightly-labs/openbot#14986 ·
I maintainer di solito rispondono entro 1 giorno
-
status/need-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
google-gemini/gemini-cli#30412 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno