A closure declared before a same-name `const` or `let` in its block reads the outer variable, 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
- 18/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
Direzione di ricerca
Start with the arrow-function extraction in extract.ts and the Dafny emission in resolve.ts, using the closureBeforeConst repro to see the lambda emitted before the inner const. The issue proposes rejecting this shape with a diagnostic when an arrow body refers to a let or const declared later in an enclosing block, but a pull request (#364) is already open against it. Done means lsc reports an error for the repro or emits Dafny where f() reads the inner p.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Problem
In JavaScript, a closure declared before a block-scoped const or let of the same name reads the inner declaration when it runs, because the closure runs after the declaration has initialized. The generated Dafny declares the lambda before the inner var, so the lambda reads the outer variable. A false ensures can verify, with or without narrowing.
Reproduction
//@ backend dafny
//@ ensures \result === p
export function closureBeforeConst(p: number, flag: boolean): number {
if (flag) {
const f = (): number => p;
const p = 5;
return f();
}
return p;
}
tsc --strict accepts this. In JavaScript, closureBeforeConst(3, true) returns 5, which breaks the ensures.
Generated Dafny:
function closureBeforeConst(p: int, flag: bool): int
{
if flag then
var f := () => p;
var p := 5;
f()
else
p
}
lsc check --backend=dafny: 1 verified, 0 errors.
The method lowering emits the same order (var f := () => p; var p := 5; r := f();) and also verifies a false ensures.
With a narrowed optional, the same shape is a Dafny type error on main. With #358 applied, it becomes a false proof:
//@ backend dafny
//@ ensures p !== undefined ==> \result === p
export function hoisted(p: number | undefined): number {
if (p !== undefined) {
const f = (): number => p;
const p = 5;
return f();
}
return 0;
}
- In JavaScript,
hoisted(3)returns5. - On
main, the output isvar f := () => p;, and Dafny rejects it withFunction body type mismatch (expected int, got Option<int>). - With #358, the output is
var f := () => i_p_val; var p := 5; f(). It verifies:1 verified, 0 errors.
Checked on main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.
Expected
lsc reports an error, or emits Dafny in which f() reads the inner p.
Likely cause
Extract keeps source order and resolve declares names sequentially, so nothing tracks that a closure body refers to a binding that is declared later in the same block. Dafny var scoping then binds the lambda's p to the outer variable.
Changing substitution in transform does not fix this. Leaving the lambda body unsubstituted still reads the outer variable. Skipping the rest of the block after the closure also skips the lets that resolve synthesizes when it inlines a callback with a default parameter, since their initializers read the outer name (resolve.ts concatenates that prefix with the lambda body).
A likely fix is to reject this shape in extract.ts, where arrow functions are extracted. For each identifier in an arrow body, check the ts-morph symbol's declaration. Report an error when that declaration is a let or const variable declaration or binding element outside the arrow, in an enclosing block, that starts after the arrow. Emitting the lambda after the declaration would match JavaScript only when nothing calls the closure in between, so rejection is the simpler correct option.
Related
- #351 and #353 (narrowing substitution).
- #358 fixes those, and with it the narrowed repro above changes from a type error to a false proof.
- #361 (bare-block scoping) and #362 (
if-branch scoping).
- 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
-
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
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 62/100
midspiral/LemmaScript#354 ·
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