A `const` or `let` inside a bare `{ }` block stays in scope after the block, so a false `ensures` can verify

Aperta
#361 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
15/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
typescript
Ambito
compilers

Direzione di ricerca

Start in extract.ts, at the Block case of extractStmts, which splices a bare block's statements into the parent. Read forCounterRename/renameRawStmts and renameRawStmt for the existing renaming machinery, and renameSpec for its regex. Done means the repro in the issue no longer verifies the false ensures, and return p after the block refers to the parameter. Linked PR #364 already targets this, so coordinate with it first.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

Extract flattens a bare { ... } block into its parent statement list without renaming its declarations. A const or let inside the block then shadows the outer variable for the rest of the parent block, while in JavaScript it goes out of scope at the closing brace. A false ensures can verify.

Reproduction

//@ backend dafny
//@ ensures \result === 5
export function bareBlock(p: number): number {
  {
    const p = 5;
  }
  return p;
}

tsc --strict accepts this. In JavaScript, bareBlock(3) returns 3, which breaks the ensures.

Generated Dafny:

function bareBlock(p: int): int
{
  var p := 5;
  p
}

lsc check --backend=dafny: 1 verified, 0 errors.

The method lowering behaves the same way. It emits var p := 5; r := (r + p); return p;, and the false ensures verifies.

Two sibling bare blocks that each declare the same name also end up in one Dafny scope.

With a narrowed optional, the shape verifies correctly on main, but only by accident. With #358 applied, it becomes a false proof:

//@ backend dafny
//@ ensures p !== undefined ==> \result === 1
export function bare(p: number | undefined): number {
  if (p !== undefined) {
    {
      const p = 1;
    }
    return p;
  }
  return 1;
}
  • In JavaScript, bare(7) returns 7.
  • On main, the output reads i_p_val after the flattened var p := 1;, which happens to match JavaScript, and Dafny refutes the claim.
  • With #358, substitution stops at the flattened let, which matches the emitted Dafny scope. The output is var p := 1; p, and it verifies: 1 verified, 0 errors.

Checked on main at 2ccd0a5 (0.6.11) with Dafny 4.11.0.

Expected

After the closing brace, reads of p refer to the outer p. Here that means return p returns the parameter.

Likely cause

The Block case in extractStmts (extract.ts, result.push(...extractStmts(s.getStatements()))) splices the block's statements into the parent.

Lowering the block to if (true) { ... } does not fix it, because the pure-body lowering leaks branch-local declarations too (#362).

A likely fix is to alpha-rename each top-level let, const or ghostLet in a bare block that collides with a name visible after the block. The rename would apply to the block's statements before splicing, using the same machinery as forCounterRename/renameRawStmts. That needs three more changes:

  • register the new names in _userNames so later fresh names avoid them
  • add a ghostLet case to renameRawStmt
  • make the renameSpec regex skip field accesses such as s.p

Related

  • #351 (shadowing in narrowing substitution).
  • #358 fixes #351, and with it the narrowed repro above changes from a refuted claim to a false proof.
  • #360 (closure before a same-name declaration) 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.