A closure declared before a same-name `const` or `let` in its block reads the outer variable, so a false `ensures` can verify

Aperta
#360 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@namin ci sta già lavorando.

Dal 23/11/2026.

  • #364 di @namin — aperta

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) returns 5.
  • On main, the output is var f := () => p;, and Dafny rejects it with Function 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

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di midspiral/LemmaScript

Tutte le issue di midspiral/LemmaScript

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.