Narrowing substitution ignores an inner declaration that shadows the narrowed name, so a false `ensures` can verify
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- typescript
- Ambito
- compilers
Direzione di ricerca
The substitution bug lives in replaceVarInTStmts, replaceFieldsInTStmts and replacePathInTStmts in tools/src/transform.ts, which walk the tree with mapTExpr and mapTStmt without tracking bindings. Read renameFreeVar in the same file to see how shadowing is already handled, then reproduce with the shadowLetDirect and shadowDu examples through lsc gen --backend=dafny. Done means the emitted Dafny returns the inner binding in both cases. Note that open PR #358 already targets this issue.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
When lsc narrows a variable, it replaces every later occurrence of the variable's name in the block with the match binder. The replacement does not stop at an inner declaration that shadows the name. Inside if (p !== undefined) { const p = 1; return p; }, the return p refers to the inner constant, but lsc emits the binder for the outer p. A false ensures verifies.
No assignment is involved, so this is separate from #342.
Reproduced on upstream main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.
Reproduction
//@ ensures p === 99 ==> \result === 99
export function shadowLetDirect(p: number | undefined): number {
if (p !== undefined) {
const p = 1;
return p;
}
return 0;
}
shadowLetDirect(99) returns 1 in JavaScript. lsc gen --backend=dafny emits:
function shadowLetDirect(p: Option<int>): int
{
match p {
case Some(i_p_val) =>
var p := 1;
i_p_val
case None =>
0
}
}
and dafny verify reports 1 verified, 0 errors for the lemma that shadowLetDirect(99) == 99.
Discriminated-union narrowing has the same problem:
export type Shape = { kind: "a"; v: number } | { kind: "b"; w: number };
//@ ensures s.kind === "a" && s.v === 99 ==> \result === 99
export function shadowDu(s: Shape): number {
if (s.kind === "a") {
if (true) {
const s: Shape = { kind: "a", v: 1 };
return s.v;
}
}
return 0;
}
emits var s := a(1); return i_s_v; and verifies (2 verified, 0 errors), while JavaScript returns 1 for { kind: "a", v: 99 }.
Expected
Substitution should stop wherever the name is rebound: statements after a let or const of the same name in the same block, for…of loops that bind the name, quantifiers whose bound variable has the name, lambda parameters, and nested match binders.
Likely cause
replaceVarInTStmts, replaceFieldsInTStmts and replacePathInTStmts in tools/src/transform.ts all walk the tree with mapTExpr and mapTStmt, which do not track bindings. renameFreeVar in the same file already handles shadowing and could serve as a model.
Related
- #343: a different problem with repeated loop variable names (the
_x_idx2counter). - The lambda case is currently masked because
mapTExprdoes not enter lambda bodies at all. That is #353; a fix for it needs to respect lambda parameters that shadow the narrowed name.
- 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
-
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
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno